/** * Если */ public function hookPreload() { if (!config('cache.guests')) { return; } $uri = $this->router->getUri(); $uri or $uri = 'index'; if (empty($_POST) && empty($_GET) && NULL == session('uid') && ($response = cache('pagecache/' . $uri))) { bench('done'); $bench = bench(); $data = humanize_bench($bench['done']); exit($response . '<!-- ' . round($data['time'], 3) . ' ' . $data['memory'] . '-->'); } }
<?php $points = bench(); // Специально задаем нулевой элемент, потому что счёт ведется от единицы $db_bench = array(); $total_time = 0; $memory = 0; foreach ($points as $key => $point) { if (0 === strpos($key, 'db.query')) { $memory += $point['memory']; $point = humanize_bench($point); $db_bench[] = $point; $total_time += $point['time']; } } echo t('<b>База данных:</b> %d <i class="icon icon-time"></i> %.3f<i class="icon icon-leaf"></i>%s', sizeof($queries), $total_time, File::fromBytes($memory)); ?> <?php if ($queries) { ?> <a id="db-trace-queries-handler" class="btn btn-mini"><i class="icon icon-eye-open"></i></a> <div id="db-trace-queries" class="well" style="display:none;"> <?php foreach ($queries as $key => $query) { ?> <?php echo icon('time') . ' ' . round($db_bench[$key]['time'], 4) . ' ' . icon('leaf') . ' ' . $db_bench[$key]['memory']; ?> <?php echo '<code class="prettyprint lang-sql">' . $query . '</code>'; ?>
$count = $items; $start = microtime(true); do { $win->enqueue(1); } while (--$count > 0); $elapsed = microtime(true) - $start; $meps = $items / $elapsed / 10000000.0; printf("%s,%s,%d,%.3f,%.3f\n", $type, $name, $size, $elapsed, $meps); } $fns = array("noop" => new React\EEP\Util\Noop(), "count" => new React\EEP\Stats\Count(), "sum" => new React\EEP\Stats\Sum(), "min" => new React\EEP\Stats\Min(), "max" => new React\EEP\Stats\Max(), "mean" => new React\EEP\Stats\Mean(), "vars" => new React\EEP\Stats\Variance(), "stdevs" => new React\EEP\Stats\Stdev()); $sizes = array(2, 4, 8, 16, 32, 64, 128, 256, 512); $items = 100000; // 100k :( echo "Tumbling\n"; foreach ($fns as $name => $fn) { head("Tumbling", $name); foreach ($sizes as $size) { $tumbling = new React\EEP\Window\Tumbling($fn, $size); bench('tumbling', $name, $tumbling, $size, $items); } foot(); } echo "Sliding\n"; foreach ($fns as $name => $fn) { head("Sliding", $name); foreach ($sizes as $size) { $tumbling = new React\EEP\Window\Sliding($fn, $size); bench('sliding', $name, $tumbling, $size, $items); } foot(); }
<div class="well"> <?php event('dev.trace'); ?> <?php $bench = bench(); $data = humanize_bench($bench['done']); ?> <p> <?php echo t('<b>Система:</b> ') . icon('time') . ' ' . round($data['time'], 3) . ' ' . icon('leaf') . $data['memory']; ?> </div>
$tab = $r->multi() ->hset('h', 'a', 'plop') ->hgetall('h') ->exec(); assert($tab === array(0, array('a' => 'plop', 'b' => 'y', 'c' => 'z'))); $tab = $r->multi() ->hset('h', 'a', 'plop') ->hgetall('h') ->exec(); assert($tab === array(0, array('a' => 'plop', 'b' => 'y', 'c' => 'z'))); */ $r->delete('key'); assert($r->setnx('key', 'val') === TRUE); assert($r->setnx('key', 'val') === FALSE); // close $r->close(); printf("\n"); } function bench() { try { // test("Redis", '127.0.0.1', 6379); test("HiRedis", '127.0.0.1', 6379); // test("HiRedis", '/tmp/redis.sock'); } catch (Exception $e) { var_dump($e); } } bench();
bson_decode($bsonEncoded); }); } if (function_exists('msgpack_pack')) { $msgPack = msgpack_pack($value); $benchmark->add('msgpack_unpack', function () use(&$msgPack) { msgpack_unpack($msgPack); }); } if (function_exists('igbinary_unserialize')) { $igbinarySerialized = igbinary_serialize($value); $benchmark->add('igbinary_unserialize', function () use(&$igbinarySerialized) { igbinary_unserialize($igbinarySerialized); }); } $benchmark->setCount($n); $benchmark->run(); } echo "Unserialize Array " . PHP_EOL; bench(['test' => 1, 'test2' => true, 'test3' => false, 'test4' => 123456789, 'test5' => 123456789.0, 'test6' => [1, 2, 3, 4, 5, 6, 7, 8, 9]]); $class = new \stdClass(); $class->property1 = true; $class->property2 = false; $class->property3 = "test string"; $class->property4 = 1; $class->property5 = 1.0; echo PHP_EOL . "Unserialize stdClass " . PHP_EOL; bench($class); echo PHP_EOL . "Unserialize Array with 10000 integer(s)" . PHP_EOL; bench(range(1, 10000), 1000);
/** * @expectedException InvalidArgumentException */ public function testBenchArg2Exception() { bench(function () { }, 'a'); }
<body> <div> <?php $times = 12; $a = json_decode(file_get_contents('5000.txt')); $original = json_encode($a); write('<h1>JSON test [homogeneous collection length: ' . count($a) . ']</h1>'); $b = bench(create_function('', '$GLOBALS[\'json\']=json_encode($GLOBALS[\'a\']);'), $times); write('<pre>' . $json . '</pre>'); write('<pre>Array to JSON String : ' . $b->avg . ' milliseconds</pre>'); write('<pre>JSON String to Array : ' . bench(create_function('', 'return json_decode($GLOBALS[\'json\']);'), $times)->avg . ' milliseconds</pre>'); write('<pre>JSON String length : ' . strlen($json) . '</pre>'); write('<pre>JSON String gzip : ' . size(strlen(gzencode($json, 9, FORCE_GZIP))) . '</pre>'); $b = bench(create_function('', '$GLOBALS[\'comp\']=json_hbest($GLOBALS[\'a\']);'), $times); write('<h1>JSON.hpack best compression level is ' . $comp . '</h1>'); write('<pre>' . $b->avg . ' ms to retrieve the best option (one time operation)</pre>'); for ($comp = 0; $comp < 4; ++$comp) { write('<h1>JSON.hpack compression level ' . ($comp === 4 ? ' 4 as "best option"' : $comp) . ' test</h1>'); $b = bench(create_function('', '$GLOBALS[\'json\']=json_encode(json_hpack($GLOBALS[\'a\'], ' . $comp . '));'), $times); write('<pre>' . $json . '</pre>'); write('<pre>Array to JSON String via hpack: ' . $b->avg . ' milliseconds</pre>'); $b = bench(create_function('', '$GLOBALS[\'tmp\']=json_hunpack(json_decode($GLOBALS[\'json\']));'), $times); write('<pre>JSON String via hpack to Array : ' . $b->avg . ' milliseconds</pre>'); write('<pre>JSON String length : ' . strlen($json) . '</pre>'); write('<pre>JSON String gzip : ' . size(strlen(gzencode($json, 9, FORCE_GZIP))) . '</pre>'); write('<pre>were hpack/hunpack reliable : ' . (json_encode($tmp) === $original ? 'true' : 'false') . '</pre>'); } ?> </div> </body> </html>
/** * Add final point and show calculations for system benchmark */ public function finish() { bench('done'); $template = new Template('Dev/templates/results'); append('footer', $template); }
function importPayloadToDataSource($data_source_id, $xml, $harvestID = '', $debug = false, $mode = 'HARVEST') { ob_start(); $this->output->enable_profiler(FALSE); $this->load->model('registry_object/registry_objects', 'ro'); $this->load->model('registry_object/registry_objects', 'oldRo'); $this->load->model('registry_object/rifcs', 'rifcs'); $this->load->model('data_source/data_sources', 'ds'); $imput = $xml; bench(0); $timewaiting = 0; $record_count = 0; $reg_obj_count = 0; $duplicate_record_count = 0; // An array of record ids created in this harvest (used in two-phase harvesting) $harvested_record_ids = array(); gc_enable(); // XXX: COMMENTME if ($harvestID == '') { $harvestID = "MANUAL-" . time(); } /* PHASE ONE => HARVEST THE RECORD IN ISOLATION */ $data_source = $this->ds->getByID($data_source_id); try { // Validate $this->validateRIFCSXML($xml); $sxml = $this->getSimpleXMLFromString($xml); $status = $this->getDefaultRecordStatusForDataSource($data_source); foreach ($sxml->xpath('//registryObject') as $registryObject) { // Determine the registry object class $reg_obj_count++; $ro_class = NULL; if ($mode == 'HARVEST') { foreach ($this->ro->valid_classes as $class) { if (property_exists($registryObject, $class)) { $ro_class = $class; } foreach ($registryObject->{$class} as $ro_xml) { // Flag records that are duplicates within this harvest and choose not to harvest them again (repeated keys in single harvest are dumb!) $reharvest = true; if ($oldRo = $this->oldRo->getByKey((string) $registryObject->key)) { $oldharvestID = $oldRo->getAttribute("harvest_id"); if ($oldharvestID == $harvestID) { $reharvest = false; } // XXX: Record ownership, reject if record already exists within the registry } if ($reharvest) { // XXX: Record owner should only be system if this is a harvest? $record_owner = "SYSTEM"; // Create a frame instance of the registryObject $ro = $this->ro->create($data_source, (string) $registryObject->key, $ro_class, "", $status, "defaultSlug", $record_owner, $harvestID); $ro->created_who = $record_owner; $ro->data_source_key = $data_source->key; $ro->group = (string) $registryObject['group']; $ro->setAttribute("harvest_id", $harvestID); // Order is important here! $changed = $ro->updateXML($registryObject->asXML()); // Generate the list and display titles first, then the SLUG $ro->updateTitles(); $ro->generateSlug(); $ro->processIdentifiers(); // Save all our attributes to the object $ro->save($changed); // Add this record to our counts, etc. $harvested_record_ids[] = $ro->id; $record_count++; // Memory management... unset($ro); } else { // XXX: Verbose message? $duplicate_record_count++; } } } } } // Clean up our memory objects... unset($sxml); unset($xml); gc_collect_cycles(); } catch (Exception $e) { $data_source->append_log("UNABLE TO HARVEST FROM THIS DATA SOURCE" . NL . $e->getMessage() . NL, "importer", "HARVEST_ERROR"); throw new Exception("UNABLE TO HARVEST FROM THIS DATA SOURCE" . NL . $e->getMessage() . NL); } /* PHASE TWO - ENRICH THE HARVESTED RECORD IN LIGHT OF ITS CONTEXT */ try { // Only enrich records received in this harvest foreach ($harvested_record_ids as $ro_id) { $ro = $this->ro->getByID($ro_id); $ro->addRelationships(); // XXX: re-enrich records which are related to this one $ro->update_quality_metadata(); // spatial resooultion, center, coords in enrich? $ro->determineSpatialExtents(); // vocab indexing resolution // Generate extrif $ro->enrich(); unset($ro); clean_cycles(); } gc_collect_cycles(); } catch (Exception $e) { $data_source->append_log("UNABLE TO HARVEST FROM THIS DATA SOURCE" . NL . $e->getMessage() . NL, "importer", "HARVEST_ERROR"); throw new Exception("UNABLE TO HARVEST FROM THIS DATA SOURCE" . NL . $e->getMessage() . NL); } // Index the datasource we just harvested?? XXX: Should this just index the records enriched? //$this->indexDS($data_source_id); echo (double) bench(0) - (double) $timewaiting . " seconds to harvest " . NL; echo $reg_obj_count . " received " . NL . $record_count . " records inserted " . NL; if ($duplicate_record_count > 0) { echo $duplicate_record_count . " records ignored" . NL; } if ($reg_obj_count == 0) { //echo "INPUT " .$imput; echo "DONE WITH ERRORS" . NL; } else { echo "DONE" . NL; } return ob_get_clean(); }
/** * Обновление данных * * @param string $table Имя таблицы * @param array $ Массив полей и значений * @param string $where Условия обновления */ public function update($table, $data = array(), $where = array()) { $query = 'UPDATE ' . $this->tableName($table, 'table') . ' SET '; $it = $data instanceof Core_ArrayObject ? $data->getInnerIterator() : new ArrayIterator($data); $it = new CachingIterator($it); foreach ($it as $key => $value) { $query .= $key . ' = :' . $key; if ($it->hasNext()) { $query .= ', '; } } if ($where) { $this->where($where); $query .= ' WHERE ' . $this->chain['WHERE']; } $this->autoclear && $this->clear(); $PDOStatement = $this->PDO->prepare($query); $exec_data = array(); foreach ($data as $key => $value) { $exec_data[':' . $key] = $value; } try { $i = $this->log(str_replace(array_keys($exec_data), array_values($exec_data), (string) $PDOStatement->queryString)); bench('db.query.' . $i . '.start'); if ($PDOStatement->execute($exec_data)) { bench('db.query.' . $i . '.end'); return TRUE; } else { $this->setError($PDOStatement); } } catch (PDOException $e) { $this->error($e->getMessage()); } return FALSE; }
public function transfer_file() { $this->log(1, __FUNCTION__, "Beginning to transfer file content from the Internet"); $this->dretry = 0; $this->tsize = 0; $this->tcount = 0; bench("start"); while (!feof($this->server_fp)) { while (($data = fread($this->server_fp, $this->chunksize)) === FALSE) { $this->dretry++; $this->log(1, __FUNCTION__, "Cannot read URL request {$this->cache_request}, retry #{$dretry} " . $this->original_url); $info = stream_get_meta_data($this->server_fp); $info = implode(";", $info); if ($this->dretry > $this->retry_limit) { $this->logdie(2, __FUNCTION__, "Cannot read URL, metadata: " . $info); } } $datalen = strlen($data); $this->tsize += $datalen; $this->tcount++; if ($this->tcount == 1) { //first packet $header = substr($data, 0, 20); if (substr($header, 0, 4) == chr(0x12) . chr(00) . chr(03) . chr(0x4b)) { //header missing, try to fix it $data = "FLV" . chr(0x1) . chr(0x5) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x9) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . $data; $header = substr($data, 0, 20); $this->log(1, __FUNCTION__, "Fixing header"); } $header_printable = preg_replace('/[\\x00-\\x1F\\x80-\\xFF]/', '', $header); // unpredictable header for some (unknown?) formats // if (!in_array($header_printable,$this->known_formats)) // $this->logdie(2,__FUNCTION__,"Invalid header for request {$this->cache_request} PRINTABLE [{$header_printable}] HEXDUMP ".$this->hexdump($header)); } // remove stalled transfers if (empty($data)) { $this->log(2, __FUNCTION__, "Empty content reached at pos [{$this->tsize}] packet [{$this->tcount}] file [{$this->cache_filename}] request [{$this->cache_request}]"); $this->close_cache_file(); return true; } // print data to client echo $data; // To cache file, if cache file pointer is ok if ($this->cache_fp) { if (fwrite($this->cache_fp, $data) === FALSE) { $this->log(2, __FUNCTION__, "Cannot write cache file: [{$this->cache_filename}]"); $this->stop_caching(); return false; } if ($this->tcount / 10 == ROUND($this->tcount / 10)) { // update temp transfers every 10th packet, prevents query floods $this->temporary_transfer($this->cache_request, $this->temp_cache_filename, "NOW()", $this->server_reply_headers['Content-Length'], $this->tsize, $this->tcount, $this->client_ip); } } //if ($this->cache_fp) { } //while (!feof($this->server_fp)) { $transfer_time = bench(); if ($this->cache_fp) { $this->close_temporary_transfer($this->cache_request); } $this->log(1, __FUNCTION__, "File content for request {$this->cache_request} fully transferred, {$this->tsize} bytes"); if ($transfer_time < 0) { $transfer_time = 1; } $this->troughput_internet = ROUND($tsize / ($transfer_time / 1000)); }
if (strpos($point, '.end')) { $point = substr($point, 0, strpos($point, '.end')); if (isset($points[$point . '.start'])) { $start_time = $points[$point . '.start']['time']; $start_memory = $points[$point . '.start']['memory']; unset($points[$point . '.start']); } } elseif (strpos($point, '.start')) { $start_time = 0; $start_memory = 0; } isset($start_time) or $start_time = IGNITE; isset($start_memory) or $start_memory = START_MEMORY; isset($points[$point]) or $points[$point] = array('time' => microtime(TRUE) - $start_time, 'memory' => memory_get_usage() - $start_memory); } bench('ignite'); define('COGEAR', '2.2'); define('DS', DIRECTORY_SEPARATOR); define('PS', PATH_SEPARATOR); define('EXT', '.php'); define('ROOT', realpath(dirname(__FILE__))); // Для мультисайтинга — на будущее define('SITE', ROOT); define('CACHE', ROOT . DS . 'cache'); define('GEARS', ROOT . DS . 'gears'); define('THEMES', ROOT . DS . 'themes'); define('TEMP', ROOT . DS . 'temp'); define('LANG', 'lang'); define('UPLOADS', ROOT . DS . 'uploads'); define('PHP_FILE_PREFIX', '<?php ' . "\n"); ini_set('display_errors', 1);
} echo ' Merged loading: ', microtime(true) - $t, PHP_EOL; $t = microtime(true); for ($i = 0; $i < $repeats; $i++) { $cached_loader->load(); } echo ' Cached loading: ', microtime(true) - $t, PHP_EOL; echo PHP_EOL; } function cleanup() { $merged_file = __DIR__ . '/config_merged.php'; $cached_file = __DIR__ . '/config_cached.php'; if (file_exists($merged_file)) { unlink($merged_file); } if (file_exists($cached_file)) { unlink($cached_file); } } cleanup(); bench(1); cleanup(); bench(100); cleanup(); bench(1000); cleanup(); bench(10000); cleanup(); bench(100000);