PHP 简单的Profiler
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 简单的Profiler相关的知识,希望对你有一定的参考价值。
<?php
// simple profiler class
// by Patrik Plihal
class myProfiler{
var $timestamps = array();
var $cnt = 0; // in fact count(%timestamps)+1
var $style = '<style type="text/css">.profiler{width: 420px; font-size: 11px; border: solid 1px #45A} .tar{text-align:right;} th{background-color: #D9D9D9 !important} .names{width: 150px;} .time1{width: 120px;background-color: #AAD} .time2{width: 120px;background-color: #DDA}</style>';
var $factor;
var $unit;
function __construct($unit="s",$lazy = false){
if(!$lazy)
$this->capture('start');
$this->unit = $unit;
switch($unit){
case "ms":
$this->factor = 1000;
break;
case "µs":
$this->factor = 1000000;
break;
default:
$this->factor = 1;
break;
}
}
function capture($desc){
$this->timestamps []= new myTimeStamp((empty($desc) ? $this->cnt : $desc));
$this->cnt++;
}
function getReport(){
if($this->cnt < 2) return "";
$rep = "\n capturepoint | absolute relative\n";
$profiletime = $this->timestamps[$this->cnt-1]->time - $this->timestamps[0]->time;
$lasttime = $this->timestamps[0]->time;
for($i = 1; $i < $this->cnt; $i++){
$delta = $this->timestamps[$i]->time - $lasttime;
$rep .= sprintf("[ %12s ] : %10s %3s %10s %%\n",$this->timestamps[$i]->name,round($delta*$this->factor,5),$this->unit,round(($delta/$profiletime),3)*100); //$delta,($delta/$wholetime))
$lasttime = $this->timestamps[$i]->time;
}
$rep .= sprintf("[ %12s ] | %10s sec %10s %%\n","summary",round($profiletime,5),100); //$delta,($delta/$wholetime))
return $rep."\n\n";
}
function getHTMLReport(){
if($this->cnt < 2) return "";
$rep = $this->style."<div><table class=\"profiler\" border=0>\n";
$rep .= "<thead><tr><th class=\"names tar\">capturepoint</th>\n<th class=\"time1 tar\">absolute</th><th class=\"time2 tar\">relative</th></tr></thead><tbody>\n";
$profiletime = max($this->timestamps[$this->cnt-1]->time - $this->timestamps[0]->time,0.0000001);
$lasttime = $this->timestamps[0]->time;
for($i = 1; $i < $this->cnt; $i++){
$delta = $this->timestamps[$i]->time - $lasttime;
$rep .= "<tr><td class=\"names tar\">".$this->timestamps[$i]->name."</td><td class=\"time1 tar\">".round($delta*$this->factor,5)." {$this->unit}</td><td class=\"time2 tar\">".(round($delta/$profiletime,3)*100)." %</td></tr>\n";
$lasttime = $this->timestamps[$i]->time;
}
$rep .= "</tbody><tfoot><tr><th class=\"names tar\">summary</th>\n<th class=\"time1 tar\">".round($profiletime,5)." sec</th><th class=\"time2 tar\">100 %</th></tr></tfoot>\n";
return $rep."</table></div>\n";
}
function test(){
print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
print '<html><head></head><body>';
foreach(range(1,5) as $i){
usleep(250000*$i);
$this->capture(($i/((float)4)));
}
print $this->getHTMLReport();
print '</body></html>';
}
}
class myTimeStamp{
var $name;
var $time;
function __construct($n,$t = false){
$this->name = $n;
$this->time = (empty($t) ? microtime(true) : $t);
}
}
?>
以上是关于PHP 简单的Profiler的主要内容,如果未能解决你的问题,请参考以下文章
PHP:在运行时更改 xdebug.profiler_output_name 选项