|
Posted by Chung Leong on 12/18/05 04:14
Here is a simple profiler I wrote. It uses a tick function to measure
the time expired between each PHP operation and debug_back_trace() to
find out where it occurs. Kinda neat as you don't have to make much
modification. Just include it at the beginning of the script, then at
the end, call __profile__('get') to obtain the profile data--an
associative array of percentages keyed by function names.
profiler.php:
<?php
function __profiler__($cmd = false) {
static $log, $last_time, $total;
list($usec, $sec) = explode(" ", microtime());
$now = (float) $usec + (float) $sec;
if($cmd) {
if($cmd == 'get') {
unregister_tick_function('__profile__');
foreach($log as $function => $time) {
if($function != '__profile__') {
$by_function[$function] = round($time / $total * 100, 2);
}
}
arsort($by_function);
return $by_function;
}
else if($cmd == 'init') {
$last_time = $now;
return;
}
}
$delta = $now - $last_time;
$last_time = $now;
$trace = debug_backtrace();
$caller = $trace[1]['function'];
@$log[$caller] += $delta;
$total += $delta;
}
__profiler__('init');
register_tick_function('__profiler__');
declare(ticks=1);
?>
[Back to original message]
|