public function testByteCount1() { $this->assertEquals(byte_count(1024 * 2), '2 KiB'); $this->assertEquals(byte_count(1024 * 1024 * 2), '2 MiB'); $this->assertEquals(byte_count(1024 * 1024 * 1024 * 2), '2 GiB'); $this->assertEquals(byte_count(1024 * 1024 * 1024 * 1024 * 2), '2 TiB'); }
<?php namespace cd; if (!$temp instanceof TempStoreRedis) { return; } $config = $temp->getServerConfig(); //d($config); $info = $temp->getServerInfo(); //d($info); echo 'Redis ' . $info['redis_version'] . ', ' . $info['redis_mode'] . ' mode<br/>'; echo 'at <b>' . $config['bind'] . ':' . $config['port'] . '</b><br/>'; echo 'Uptime <b>' . elapsed_seconds($info['uptime_in_seconds']) . '</b><br/>'; echo 'Connected clients: <b>' . $info['connected_clients'] . '</b><br/>'; echo 'Used memory: <b>' . byte_count($info['used_memory']) . '</b><br/>';
switch ($this->owner) { case 'show': // child = id $f = File::get($this->child); if (!$f) { die('MECKLPSP'); } echo '<h1>Photo details for ' . $f->name . '</h1>'; //d($f); $size = getimagesize(File::getUploadPath($this->child)); // d($size); echo 'Name: ' . $f->name . '<br/>'; echo 'Uploaded: ' . ago($f->time_uploaded) . ' by ' . $f->uploader . '<br/>'; echo 'Resolution: ' . $size[0] . 'x' . $size[1] . '<br/>'; echo 'Size: ' . byte_count($f->size) . '<br/>'; echo '<br/>'; // shows the photo $a = new XhtmlComponentA(); $a->href = getThumbUrl($f->id, 0, 0); $a->rel = 'lightbox'; $a->content = showThumb($f->id, $f->name, 150, 150); echo $a->render(); $lb = new YuiLightbox(); echo $lb->render() . '<br/>'; if ($session->id && $session->id != $f->uploader) { echo '» ' . ahref('u/report/photo/' . $f->id, 'Report photo') . '<br/>'; } if ($session->id && $session->id == $f->uploader) { echo '» ' . ahref('u/photo/rotate/' . $f->id . '/90', 'Rotate left') . '<br/>'; echo '» ' . ahref('u/photo/rotate/' . $f->id . '/270', 'Rotate right') . '<br/>';
if (!$temp instanceof TempStoreMemcached) { return; } $pool = $temp->getServerStats(); if (!$pool) { echo 'No server configured'; return; } foreach ($pool as $host => $stat) { $pct = 0; if ($stat['limit_maxbytes']) { $pct = $stat['bytes'] / $stat['limit_maxbytes']; } echo 'Read: <b>' . byte_count($stat['bytes_read']) . '</b><br/>'; echo 'Written: <b>' . byte_count($stat['bytes_written']) . '</b><br/>'; echo 'Used memory: <b>' . byte_count($stat['bytes']) . '</b>' . ' (<b>' . round($pct * 100, 1) . '%</b>' . ' of <b>' . byte_count($stat['limit_maxbytes']) . '</b>)' . '<br/>'; echo '<br/>'; echo 'Get: <b>' . $stat['get_hits'] . '</b> hits, <b>' . $stat['get_misses'] . '</b> misses<br/>'; echo 'Cmd: <b>' . $stat['cmd_get'] . '</b> get, <b>' . $stat['cmd_set'] . '</b> set<br/>'; echo '<br/>'; echo 'Currently <b>' . $stat['curr_items'] . '</b> items, <b>' . $stat['curr_connections'] . '</b> connections<br/>'; echo 'Total <b>' . $stat['total_items'] . '</b> items, <b>' . $stat['total_connections'] . '</b> connections<br/>'; echo '<br/>'; // ??? echo 'Evictions: <b>' . $stat['evictions'] . '</b><br/>'; echo 'Threads: <b>' . $stat['threads'] . '</b><br/>'; echo '<br/>'; echo 'Server: <b>' . $host . '</b><br/>'; echo 'Software: <b>memcached ' . $stat['version'] . '</b><br/>'; echo 'Local time: <b>' . sql_datetime($stat['time']) . '</b><br/>'; echo 'Uptime: <b>' . elapsed_seconds($stat['uptime']) . '</b><br/>';
function length_of_string($string) { // never ever do this. NOT EVER $string = eval('return ' . $string . ';'); return strlen($string); } function byte_count($string) { return strlen($string); } // Tests $tests = ['""' => [2, 0], '"abc"' => [5, 3], '"aaa\\"aaa"' => [10, 7], '"\\x27"' => [6, 1]]; foreach ($tests as $string => $values) { $count = byte_count($string); if ($count != $values[0]) { throw new Exception("Assertion Failed, byte_count of '{$string}' was {$count}, expected {$values[0]}"); } $count = length_of_string($string); if ($count != $values[1]) { throw new Exception("Assertion Failed, length_of_string of '{$string}' was {$count}, expected {$values[1]}"); } } $input = file('input.txt', FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES); $chars = 0; $bytes = 0; foreach ($input as $string) { $chars += length_of_string($string); $bytes += byte_count($string); } echo $bytes - $chars, PHP_EOL;
return strlen($string); } function new_byte_count($string) { $string = str_replace('\\"', 'S', $string); $string = str_replace('\\', '\\\\', $string); $string = str_replace('"', '\\"', $string); $string = str_replace('S', '\\\\\\"', $string); $string = "\"{$string}\""; return strlen($string); } // Tests $tests = ['""' => [2, 6], '"abc"' => [5, 9], '"aaa\\"aaa"' => [10, 16], '"\\x27"' => [6, 11]]; foreach ($tests as $string => $values) { $count = byte_count($string); if ($count != $values[0]) { throw new Exception("Assertion Failed, byte_count of '{$string}' was {$count}, expected {$values[0]}"); } $count = new_byte_count($string); if ($count != $values[1]) { throw new Exception("Assertion Failed, new_byte_count of '{$string}' was {$count}, expected {$values[1]}"); } } $input = file('input.txt', FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES); $newBytes = 0; $oldBytes = 0; foreach ($input as $string) { $oldBytes += byte_count($string); $newBytes += new_byte_count($string); } echo $newBytes - $oldBytes, PHP_EOL;