Return to Snippet

Revision: 14185
at May 28, 2009 09:12 by birdspider


Updated Code
<?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);
	}
}
?>

Revision: 14184
at May 28, 2009 09:11 by birdspider


Updated Code
<?php
// simple profiler class
// by Patrik Plihal
// digiconcept

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);
	}
}
?>

Revision: 14183
at May 28, 2009 09:09 by birdspider


Updated Code
<?php
// simple profiler class
// by Patrik Plihal
// digiconcept

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 "ns":
			$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);
	}
}
?>

Revision: 14182
at May 22, 2009 05:30 by birdspider


Initial Code
<?php
// simple profiler class
// by birdspider <nemora at gmx TOD at>
// GPL 
// as-is

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>';

	function __construct($lazy = false){
		if(!$lazy)
			$this->capture('start');
	}

	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 sec %10s %%\n",$this->timestamps[$i]->name,round($delta,5),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,5)." sec</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);
	}
}
?>

Initial URL

                                

Initial Description
Rather simple profiler for > php5, if you want to find out where the heck those 0.5 sec function time goes into

Initial Title
Simple Profiler

Initial Tags
php

Initial Language
PHP