/** * Tick handler. * Make use of declare(ticks=1); to log execution time. */ public function tickHandler() { $time = microtime(true); if (self::$lastTickTime === null) { self::$lastTickTime = $time; } $dt = $time - self::$lastTickTime; if (memory_get_peak_usage(true) > self::$_maxMemory - 1048576) { // 1 Mb reserved throw new Exception('memory limit exceeded'); } if (self::$_writeLog) { $trace = debug_backtrace(); foreach ($trace as $i => $t) { $method = 'main'; if (isset($t['function'])) { $method = $t['function']; if (isset($t['class'])) { $method = $t['class'] . '::' . $method; } } if ($i == 1) { if (!isset(self::$_callStatistics[$method])) { self::$_callStatistics[$method] = 0; } self::$_callStatistics[$method] += $dt; } if (!isset(self::$_callMaxStatistics[$method])) { self::$_callMaxStatistics[$method] = 0; } self::$_callMaxStatistics[$method] += $dt; } /* if (isset($trace[1])){ $t = $trace[1]; $method = 'main'; if (isset($t['function'])){ $method = $t['function']; if (isset($t['class'])){ $method = $t['class'].'::'.$method; } } }else{ $method = 'main'; } if (!isset(self::$_callStatistics[$method])){ self::$_callStatistics[$method] = 0; } self::$_callStatistics[$method] += $dt; */ if ($dt > yProfiler::LONG_TIME) { // FIXME $this->_benchmarkLog[] = array($dt, $trace); } // connection_aborted() // memory_get_usage() // } self::$lastTickTime = microtime(true); }
public function html() { $h = '<div class="kanon-profiler">'; if (function_exists('xdebug_get_profiler_filename')) { $h .= '<div>XDebug: ' . xdebug_get_profiler_filename() . '</div>'; } $h .= '<div>Total queries: ' . count($this->_sql) . '</div>'; $totalSqlTime = 0; foreach ($this->_sql as $sqlInfo) { $totalSqlTime += $sqlInfo['time']; } $h .= '<div>Total runtime: ' . number_format(microtime(true) - self::$_enableTime, 6, '.', '') . '</div>'; $h .= '<div>Total time: ' . number_format($totalSqlTime, 6, '.', '') . '</div>'; $h .= '<table width="100%" class="sql">'; foreach ($this->_sql as $sqlInfo) { $h .= '<tr><td>'; $h .= '<strong style="font-weight: normal;color: #ddd;">' . htmlspecialchars($sqlInfo['sql']) . '</strong>'; $h .= '<div>'; if ($sqlInfo['time'] > 0.01) { $h .= '<span style="color: #f00">'; } $h .= 'Time: ' . number_format($sqlInfo['time'], 6, '.', ''); if ($sqlInfo['time'] > 0.01) { $h .= '</span>'; } $h .= ' Start: ' . number_format($sqlInfo['start'] - self::$_enableTime, 6, '.', ''); if ($traceInfo = $this->_getTraceController($sqlInfo['trace'])) { $h .= ' ' . $traceInfo['class'] . $traceInfo['type'] . $traceInfo['function'] . '() at line ' . $traceInfo['line']; } $i = 0; $skip = array('profiler', 'mysqlDriver', 'storageDriver', 'modelStorage', 'modelExpression', 'modelQueryBuilder', 'modelResultSet'); foreach ($sqlInfo['trace'] as $traceInfo) { if (in_array($traceInfo['class'], $skip)) { array_shift($sqlInfo['trace']); } else { break; } } foreach ($sqlInfo['trace'] as $traceInfo) { $i++; $h .= '<div style="line-height: 1em;font-size:10px;">'; $h .= '#' . $i . ' ' . $traceInfo['class'] . $traceInfo['type'] . $traceInfo['function'] . '() at line ' . $traceInfo['line']; $h .= '</div>'; } $h .= '</div>'; $h .= '</td></tr>'; } $h .= '</table>'; $h .= yProfiler::html(); return $h . '</div>'; }