function smarty_timings(){ $GLOBALS['timings']['smarty_timings_out'] = microtime_ms(); echo "<table class=\"debugtimings\" border=\"1\" align=\"center\">\n"; echo "<tr>\n"; echo "<th>Item</th>"; echo "<th>Count</th>"; echo "<th>Time</th>"; echo "</tr>\n"; # we add this one last so it goes at the bottom of the list $GLOBALS['timing_keys']['smarty_comp'] = 'Templates Compiled'; foreach ($GLOBALS['timing_keys'] as $k => $v){ $c = intval($GLOBALS['timings']["{$k}_count"]); $t = intval($GLOBALS['timings']["{$k}_time"]); echo "<tr><td>$v</td><td>$c</td><td>$t ms</td></tr>\n"; } $map2 = array( array("Startup & Libraries", $GLOBALS['timings']['init_end'] - $GLOBALS['timings']['execution_start']), array("Page Execution", $GLOBALS['timings']['smarty_start_output'] - $GLOBALS['timings']['init_end']), array("Smarty Output", $GLOBALS['timings']['smarty_timings_out'] - $GLOBALS['timings']['smarty_start_output']), array("<b>Total</b>", $GLOBALS['timings']['smarty_timings_out'] - $GLOBALS['timings']['execution_start']), ); foreach ($map2 as $a){ echo "<tr><td colspan=\"2\">$a[0]</td><td>$a[1] ms</td></tr>\n"; } echo "</table>"; }
function loadlib($name) { if ($GLOBALS['loaded_libs'][$name]) { return; } $GLOBALS['loaded_libs'][$name] = 1; $start = microtime_ms(); $fq_name = _loadlib_enpathify("lib_{$name}.php"); include $fq_name; $end = microtime_ms(); $time = $end - $start; $GLOBALS['timings']['loadlib_count'] += 1; $GLOBALS['timings']['loadlib_time'] += $time; # $GLOBALS['timing_keys']["loadlib_{$name}"] = "lib_{$name}"; # $GLOBALS['timings']["loadlib_{$name}_count"] = 1; # $GLOBALS['timings']["loadlib_{$name}_time"] = $time; }
function xstart() { $Directory = "/home/artica-postfix/squid/StatsApplicance/BEREKLEY"; if (!is_dir($Directory)) { return; } if (!($handle = opendir($Directory))) { return; } while (false !== ($fileZ = readdir($handle))) { if ($fileZ == ".") { continue; } if ($fileZ == "..") { continue; } $path = "{$Directory}/{$fileZ}"; $lockfile = "{$Directory}/{$fileZ}.LCK"; events("parse_stats(): Scanning {$path}"); $t1 = microtime_float(); if (preg_match("#^(.+?)-UserAuthDB\\.db#", $fileZ, $re)) { if (is_file($lockfile)) { continue; } @file_put_contents($lockfile, time()); parse_userauthdb($path, $re[1], true); events("{$fileZ} " . microtime_ms($t1)); @unlink($path); @unlink($lockfile); continue; } if (preg_match("#^(.+?)-[0-9]+_QUOTASIZE\\.db#", $fileZ, $re)) { if (is_file($lockfile)) { continue; } @file_put_contents($lockfile, time()); ParseDB_FILE($path, $re[1], true); events("{$fileZ} " . microtime_ms($t1)); @unlink($lockfile); @unlink($path); continue; } } }
function smarty_timings() { $GLOBALS['timings']['smarty_timings_out'] = microtime_ms(); echo "<div class=\"admin-timings-wrapper\">\n"; echo "<table class=\"admin-timings\">\n"; # we add this one last so it goes at the bottom of the list $GLOBALS['timing_keys']['smarty_comp'] = 'Templates Compiled'; foreach ($GLOBALS['timing_keys'] as $k => $v) { $c = intval($GLOBALS['timings']["{$k}_count"]); $t = intval($GLOBALS['timings']["{$k}_time"]); echo "<tr><td>{$v}</td><td class=\"tar\">{$c}</td><td class=\"tar\">{$t} ms</td></tr>\n"; } $map2 = array(array("Startup & Libraries", $GLOBALS['timings']['init_end'] - $GLOBALS['timings']['execution_start']), array("Page Execution", $GLOBALS['timings']['smarty_start_output'] - $GLOBALS['timings']['init_end']), array("Smarty Output", $GLOBALS['timings']['smarty_timings_out'] - $GLOBALS['timings']['smarty_start_output']), array("<b>Total</b>", $GLOBALS['timings']['smarty_timings_out'] - $GLOBALS['timings']['execution_start'])); foreach ($map2 as $a) { echo "<tr><td colspan=\"2\">{$a['0']}</td><td class=\"tar\">{$a['1']} ms</td></tr>\n"; } echo "</table>\n"; echo "</div>\n"; }
function cache_memcache_connect() { # # existing connection? # if ($GLOBALS['_cache_memcache_conn']) { return $GLOBALS['_cache_memcache_conn']; } # # set up a new one # $start = microtime_ms(); $memcache = new Memcache(); if (!$memcache) { log_error("Failed to create Memcache object"); return null; } foreach ($GLOBALS['cfg']['memcache_pool'] as $bucket) { @$memcache->addServer($bucket['host'], $bucket['port']); } $stats = $memcache->getExtendedStats(); foreach ($GLOBALS['cfg']['memcache_pool'] as $bucket) { $key = implode(":", array_values($bucket)); if (!isset($stats[$key])) { log_error("Failed to connect to {$key}"); return null; } if (!$stats[$key]['accepting_conns']) { log_error("{$key} is not accepting connections"); return null; } } $end = microtime_ms(); $time = $end - $start; log_notice("cache", "connect to memcache {$host}:{$port} ({$time}ms)"); $GLOBALS['timings']['memcache_conns_count']++; $GLOBALS['timings']['memcache_conns_time'] += $time; $GLOBALS['_cache_memcache_conn'] = $memcache; return $memcache; }
function cache_memcache_connect() { if (!isset($GLOBALS['remote_cache_conns']['memcache'])) { $host = $GLOBALS['cfg']['memcache_host']; $port = $GLOBALS['cfg']['memcache_port']; $start = microtime_ms(); $memcache = new Memcache(); if (!$memcache->connect($host, $port)) { $memcache = null; } if (!$memcache) { log_fatal("Connection to memcache {$host}:{$port} failed"); } $end = microtime_ms(); $time = $end - $start; log_notice("cache", "connect to memcache {$host}:{$port} ({$time}ms)"); $GLOBALS['remote_cache_conns']['memcache'] = $memcache; $GLOBALS['timings']['memcache_conns_count']++; $GLOBALS['timings']['memcache_conns_time'] += $time; } return $GLOBALS['remote_cache_conns']['memcache']; }
function evaluate() { $isWin = strpos(PHP_OS, 'WIN') === 0; $start = 0; $end = 0; $time = 0; $returnString = ''; //=== read flags ===// //--- band width check ---// if ($this->typeFlags->getFlagByName('bandWidth')) { if (file_exists($this->sourcePath)) { unlink($this->sourcePath); } $start = microtime_ms(); //... execute bandwidth sensor ...// if (empty($this->targetPath)) { trigger_error('Target path is empty.', E_USER_WARNING); $time = -1; } else { if (empty($this->sourcePath)) { trigger_error('Source path is empty.', E_USER_WARNING); $time = -1; } else { copy($this->targetPath, $this->sourcePath); //Attention: "target" will be copied to "source"! } } //...............// $end = microtime_ms(); if (file_exists($this->sourcePath)) { unlink($this->sourcePath); } $time += $end - $start; } //--- database server check ---// if ($this->typeFlags->getFlagByName('sql')) { if ($this->typeFlags->getFlagByName('sensorPage') || $this->sourcePath) { //... use extern sensor ...// if ($returnTime > 0) { $time += $returnTime; } else { if ($time > 0) { $time *= -1; } else { $time = -1; } } //...............// } else { $start = microtime_ms(); //... execute SQL sensor...// $connection = mysql_connect($this->targetPath, $this->username, $this->password); if ($this->subpath) { $success = mysql_select_db($this->subpath); $query = 'SHOW TABLES FROM `' . $this->subpath . '`'; } else { $query = 'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES'; } $success = mysql_query($query); //...............// $end = microtime_ms(); if ($time < 0) { $success = false; } $time = abs($time) + $end - $start; if (!$success) { $time *= -1; } } } else { if ($this->typeFlags->getFlagByName('sensorPage')) { if (!$this->sourcePath && !$this->targetPath) { $start = microtime_ms(); //... execute PHP-sensor ...// for ($i = 0; $i < 10000; $i++) { //slow down $magic = 1; for ($j = 0; $j <= 5; $j++) { $magic *= pow($j, $j); if ($j === 3) { $note = "A Mala has {$magic} beads."; } } $magic -= 24 * 60 * 60 * 1000; if ($end - $start > $timeout) { $timeouted = true; break; } } //...............// $end = microtime_ms(); $time += $end - $start; } else { //... use extern sensor ...// $returnTime = $this->evaluateExtern(); if ($returnTime > 0) { $time += $returnTime; } else { if ($time > 0) { $time *= -1; } else { $time = -1; } } //...............// } } else { if ($this->typeFlags->getFlagByName('ping')) { if ($this->sourcePath && $this->targetPath) { $returnTime = $this->evaluateExtern(); if ($returnTime > 0) { $time += $returnTime; } else { if ($time > 0) { $time *= -1; } else { $time = -1; } } } else { $path = $this->targetPath ? $this->targetPath : $this->sourcePath; $pingCount = $this->subpath; if (!is_numeric($pings) || $pings < 1 || $pings > 100) { $pingCount = 1; } $result = exec('ping -' . ($isWin ? 'n' : 'c') . ' ' . $pingCount . ' ' . $path); if ($isWin) { $averageTime = substr($result, strrpos($result, ' ') + 1, -2); } else { $posOfEqualSign = strpos($result, '='); $posOfFirstSlashAfterEqual = strpos($result, '=', $posOfEqualSign); $posOfSecondSlashAfterEqual = strpos($result, '=', $posOfFirstSlashAfterEqual); $averageTime = substr($result, $posOfFirstSlashAfterEqual + 1, $posOfSecondSlashAfterEqual - $posOfFirstSlashAfterEqual - 1); } if (is_numeric($averageTime)) { $time += round($averageTime); } else { if ($time > 0) { $time *= 1; } else { if ($time == 0) { $time = -1; } } } } } else { if (!$this->typeFlags->getFlagByName('bandWidth')) { $returnTime = $this->evaluateExtern(); if ($returnTime > 0) { $time += $returnTime; } else { if ($time > 0) { $time *= -1; } else { $time = -1; } } } } } } return $time; }
function _db_fetch($sql, $cluster, $k=null){ $ret = _db_query($sql, $cluster, $k); if (!$ret['ok']) return $ret; $out = $ret; $out['ok'] = 1; $out['rows'] = array(); unset($out['result']); $start = microtime_ms(); $count = 0; while ($row = mysql_fetch_array($ret['result'], MYSQL_ASSOC)){ $out['rows'][] = $row; $count++; } $end = microtime_ms(); $GLOBALS['timings']['db_rows_count'] += $count; $GLOBALS['timings']['db_rows_time'] += $end-$start; return $out; }
<?php ################################################################# # HEY LOOK! RUNNING CODE!!! $start = microtime_ms(); loadlib("api_config"); api_config_init(); $end = microtime_ms(); $time = $end - $start; $GLOBALS['timing_keys']["api_init"] = "API init"; $GLOBALS['timings']['api_init_count'] += 1; $GLOBALS['timings']['api_init_time'] += $time; ################################################################# loadlib("api_output"); loadlib("api_log"); loadlib("api_auth"); loadlib("api_keys"); loadlib("api_keys_utils"); loadlib("api_throttle"); loadlib("api_utils"); ################################################################# function api_dispatch($method) { if (!$GLOBALS['cfg']['enable_feature_api']) { api_output_error(999, 'API disabled'); } # Necessary? Also causes PHP 5.5 to freak out # with older versions of lib_filter... # (20140122/straup) $method = filter_strict($method); $api_key = request_str("api_key");
function _http_request($ch, $url, $more = array()) { $start = microtime_ms(); $raw = curl_exec($ch); $info = curl_getinfo($ch); if ($err = curl_error($ch)) { $info['curl_error'] = $err; } $end = microtime_ms(); curl_close($ch); $GLOBALS['timings']['http_count']++; $GLOBALS['timings']['http_time'] += $end - $start; return _http_parse_response($raw, $info); }
try_nq(); try_qq(); } # # do iiiit # $num = 10000; $t1 = microtime_ms(); for ($i = 0; $i < $num; $i++) { try_nq(); } $t2 = microtime_ms(); for ($i = 0; $i < $num; $i++) { try_qq(); } $t3 = microtime_ms(); echo "Time taken to run 8 assignements " . number_format($num) . " times, using 28 key names:<br /><br />\n"; echo "bare-word keys: " . number_format($t2 - $t1) . " ms<br />\n"; echo "quoted keys: " . number_format($t3 - $t2) . " ms<br />\n"; function try_nq() { $a = array(); $a[b] = array(); $a[b][c] = array(); $a[b][c][d] = array(); $a[b][c][d][e] = array(); $a[b][c][d][e][f] = array(); $a[b][c][d][e][f][g] = array(); $a[b][c][d][e][f][g][h] = 1; return $a; }
function dots_import_dots(&$user, &$sheet, &$dots, $more = array()) { $received = 0; $processed = 0; $errors = array(); $search = array(); $extras = array(); $lookup = array(); $timings = array(0 => 0); $start_all = microtime_ms() / 1000; # As in: don't update DotsSearch inline but save # all the inserts and do them at at end. $more['buffer_search_inserts'] = 1; $more['buffer_extras_inserts'] = 1; $more['buffer_lookup_inserts'] = 1; foreach ($dots as $dot) { $received++; $start = microtime_ms() / 1000; $rsp = dots_create_dot($user, $sheet, $dot, $more); $end = microtime_ms() / 1000; $timings[$received] = $end - $start; if (!$rsp['ok']) { $rsp['record'] = $received; $errors[] = $rsp; continue; } # See this? We're checking for errors from the derived from # hooks that happen in _import_dot. We just want to trap them # so that we can display feedback to the user, which is to # say: If the code's gotten this far it means the dot has # been added to the database (20110311/straup) if (isset($rsp['derived']) && !$rsp['derived']['ok']) { $rsp['derived']['record'] = $received; $errors[] = $rsp['derived']; # carry on } if (isset($rsp['search'])) { $search[] = $rsp['search']; } if (isset($rsp['extras'])) { $extras = array_merge($extras, $rsp['extras']); } if (isset($rsp['lookup'])) { $lookup[] = $rsp['lookup']; } $processed++; } # # Buffered inserts (at some point this might/should # be handed to an offline tasks system) # if (count($lookup)) { $lookup_rsp = dots_lookup_add_lots_of_dots($lookup); if (!$lookup_rsp) { # What then ? } } # See notes in lib_dots_search about needing to merge these # two calls in to a single wrapper that calls a single "add # to search" wrapper call that takes care of whether or not # extras are stored in their own table or denormalized. # (20110502/straup) if (count($search)) { $search_rsp = dots_search_add_lots_of_dots($search); if (!$search_rsp) { # What then ? } } if (count($extras)) { $extras_rsp = dots_search_extras_add_lots_of_extras($extras); if (!$extras_rsp) { # What then ? } } # $end_all = microtime_ms() / 1000; $timings[0] = $end_all - $start_all; $ok = $processed ? 1 : 0; return array('ok' => $ok, 'errors' => &$errors, 'timings' => &$timings, 'dots_received' => $received, 'dots_processed' => $processed); }
/** * compile the template * * @param string $resource_name * @param string $compile_path * @return boolean */ function _compile_resource($resource_name, $compile_path) { $start = microtime_ms(); $ret = $this->_compile_resource_real($resource_name, $compile_path); $end = microtime_ms(); $GLOBALS['timings']['smarty_comp_count']++; $GLOBALS['timings']['smarty_comp_time'] += $end - $start; log_notice("smarty", "Compiling {$resource_name}", $end - $start); return $ret; }
function http_get($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); # Get around error 417 curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_TIMEOUT, $GLOBALS['cfg']['http_timeout']); curl_setopt($ch, CURLINFO_HEADER_OUT, true); curl_setopt($ch, CURLOPT_HEADER, true); # # execute request # $start = microtime_ms(); $raw = curl_exec($ch); $info = curl_getinfo($ch); $end = microtime_ms(); curl_close($ch); $GLOBALS['timings']['http_count']++; $GLOBALS['timings']['http_time'] += $end-$start; # # parse request & response # list($head, $body) = explode("\r\n\r\n", $raw, 2); list($head_out, $body_out) = explode("\r\n\r\n", $info['request_header'], 2); unset($info['request_header']); $headers_in = http_parse_headers($head, '_status'); $headers_out = http_parse_headers($head_out, '_request'); log_notice("http", "GET $url", $end-$start); # # return # if ($info['http_code'] != "200"){ return array( 'ok' => 0, 'error' => 'http_failed', 'code' => $info['http_code'], 'url' => $url, 'info' => $info, 'req_headers' => $headers_out, 'headers' => $headers_in, 'body' => $body, ); } return array( 'ok' => 1, 'url' => $url, 'info' => $info, 'req_headers' => $headers_out, 'headers' => $headers_in, 'body' => $body, ); }
$out = array('ok' => 0, 'error' => $msg); if ($code) { $out['error_code'] = $code; } return $out; } # # Smarty stuff # $GLOBALS['error'] = array(); $GLOBALS['smarty']->assign_by_ref('error', $error); # # Hey look! Running code! Note that db_init will try # to automatically connect to the db_main database # (unless you've disable the 'auto_connect' flag) and # will blow its brains out if there's a problem. # db_init(); if ($this_is_webpage) { login_check_login(); } if (StrToLower($_SERVER['HTTP_X_MOZ']) == 'prefetch') { if (!$GLOBALS['cfg']['allow_precache']) { error_403(); } } # # this timer stores the end of core library loading # $GLOBALS['timings']['init_end'] = microtime_ms();
function db_ping($cluster, $shard = null) { $cluster_key = _db_cluster_key($cluster, $shard); if (is_resource($GLOBALS['db_conns'][$cluster_key])) { $start = microtime_ms(); $ret = @mysql_ping($GLOBALS['db_conns'][$cluster_key]); $end = microtime_ms(); log_notice('db', "DB-{$cluster_key}: Ping", $end - $start); return $ret; } return FALSE; }
function api_config_init_site_keys() { loadlib("api_keys"); loadlib("api_oauth2_access_tokens"); $start = microtime_ms(); login_check_login(); $token = api_oauth2_access_tokens_fetch_site_token($GLOBALS['cfg']['user']); $GLOBALS['smarty']->assign_by_ref("site_token", $token['access_token']); $end = microtime_ms(); $time = $end - $start; $GLOBALS['timing_keys']['user_init'] = 'WWW setup'; $GLOBALS['timings']['user_init_count'] = 1; $GLOBALS['timings']['user_init_time'] = $time; }