Re: Benchmark_Timer patch

From: Date: Thu, 24 Jan 2002 10:26:17 +0000
Subject: Re: Benchmark_Timer patch
References: 1 2 3  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-4138@lists.php.net to get a copy of this message
Nice comments, Tomas. Here is a new patch with: - auto profiling on request only ($timer =& new Benchmark_Timer(true);) - get_output method that differentiates between HTTP and command line output Hope you like it =) L. ----- Original Message ----- From: "Tomas V.V.Cox" <cox@idecnet.com> To: "Ludovico Magnocavallo" <ludo@sumatrasolutions.com> Cc: "PEAR DEV" <pear-dev@lists.php.net> Sent: Wednesday, January 23, 2002 9:45 PM Subject: Re: [PEAR-DEV] Benchmark_Timer patch I like the idea but have few problems with it: 1) I need to setMarker's 2) I offently (always) don't use web scripts for benchmarks Perhaps your stuff could be done so: $timer =& new Benchmark_Timer('auto'); Also I would like to see pretty_format detecting if the script has been lauched from the web or not to show html or not. Plus instead of "pretty_format" I would name the function "print" :-) Tomas V.V.Cox El mié, 23-01-2002 a las 17:15, Ludovico Magnocavallo escribió: > diff -u patch [..]

--- Timer_orig.php Thu Jan 24 10:16:16 2002 +++ Timer.php Thu Jan 24 11:23:54 2002 @@ -26,35 +26,140 @@ * * Timing Script Execution, Generating Profiling Information * - * Example: + * Example with automatic profiling start, stop, and output: * - * $timer = new Benchmark_Timer; + * $timer =& new Benchmark_Timer(true); + * $timer->setMarker('Marker 1'); + * + * Example without automatic profiling: + * + * $timer =& new Benchmark_Timer(); * * $timer->start(); * $timer->setMarker('Marker 1'); * $timer->stop(); * * $profiling = $timer->getProfiling(); + * echo($profiling->get_output()); + * + * Contributors: + * - Ludovico Magnocavallo <ludo@sumatrasolutions.com> + * auto profiling and get_output() method * * @author Sebastian Bergmann <sb@sebastian-bergmann.de> * @version $Revision: 1.1 $ * @access public */ -class Benchmark_Timer { +require_once('PEAR.php'); + +class Benchmark_Timer extends PEAR { /** * Contains the markers * * @var array - * @access public + * @access private */ var $markers = array(); + + /** + * Auto-start and stop profiling? + * + * @var boolean + * @access private + */ + var $auto = false; /** + * Max marker name length for non-html output + * + * @var integer + * @access private + */ + var $strlen_max = 0; + + /** + * Constructor, starts profiling recording + * + * @access public + */ + function Benchmark_Timer($auto = false) { + $this->PEAR(); + if ($auto) { + $this->auto = $auto; + $this->start(); + } + } + + /** + * Destructor, stops profiling recording + * + * @access private + */ + function _Benchmark_Timer() { + if ($this->auto) { + $this->stop(); + echo($this->get_output()); + } + } + + /** + * Return formatted profiling information. + * + * @see getProfiling() + * @access public + */ + function get_output() { + preg_match('/^([0-9]+)\.([0-9]+)\.([0-9]+)$/', phpversion(), $m); + if ((integer)$m[2] >= 1) { + $http = isset($_SERVER['SERVER_PROTOCOL']); + } else { + global $HTTP_SERVER_VARS; + $http = isset($HTTP_SERVER_VARS['SERVER_PROTOCOL']); + } + $total = $this->TimeElapsed(); + $result = $this->getProfiling(); + if ($http) { + $out = "<table border=1>\n"; + $out .= "<tr><td>&nbsp;</td><td><b>time index</b></td><td><b>ex time</b></td><td><b>%</b></td></tr>\n"; + } else { + $dashes = $out = str_pad("\n", ($this->strlen_max + 52), '-', STR_PAD_LEFT); + $out .= str_pad('marker', $this->strlen_max); + $out .= str_pad("time index", 22); + $out .= str_pad("ex time", 22); + $out .= "perct\n"; + $out .= $dashes; + } + foreach ($result as $k => $v) { + $perc = (($v['diff'] * 100) / $total); + if ($http) { + $out .= "<tr><td><b>" . $v['name'] . "</b></td><td>" .. $v['time'] . "</td><td>" . $v['diff'] . "</td><td>" . number_format($perc, 2, '.', '') . "%</td></tr>\n"; + } else { + $out .= str_pad($v['name'], $this->strlen_max, ' '); + $out .= str_pad($v['time'], 22); + $out .= str_pad($v['diff'], 22); + $out .= str_pad(number_format($perc, 2, '.', '') . "%\n", 8, ' ', STR_PAD_LEFT); + } + $out .= $dashes; + } + if ($http) { + $out .= "<tr style='background: silver;'><td><b>total</b></td><td>-</td><td>${total}</td><td>100.00%</td></tr>\n"; + $out .= "</table>\n"; + } else { + $out .= str_pad('total', $this->strlen_max); + $out .= str_pad('-', 22); + $out .= str_pad($total, 22); + $out .= "100.00%\n"; + $out .= $dashes; + } + return $out; + } + + /** * Set "Start" marker. * * @see setMarker(), stop() - * @access public + * @access private */ function start() { $this->setMarker('Start'); @@ -64,7 +169,7 @@ * Set "Stop" marker. * * @see setMarker(), start() - * @access public + * @access private */ function stop() { $this->setMarker('Stop'); @@ -115,16 +220,12 @@ $result = array(); foreach ($this->markers as $marker => $time) { - if ($marker == 'Start') { - $diff = '-'; + if (extension_loaded('bcmath')) { + $diff = bcsub($time, $temp, 6); + $total = bcadd($total, $diff, 6); } else { - if (extension_loaded('bcmath')) { - $diff = bcsub($time, $temp, 6); - $total = bcadd($total, $diff, 6); - } else { - $diff = $time - $temp; - $total = $total + $diff; - } + $diff = $time - $temp; + $total = $total + $diff; } $result[$i]['name'] = $marker; @@ -132,10 +233,14 @@ $result[$i]['diff'] = $diff; $result[$i]['total'] = $total; + $this->strlen_max = (strlen($marker) > $this->strlen_max ? strlen($marker) + 1 : $this->strlen_max); + $temp = $time; $i++; } - + $result[0]['diff'] = '-'; + $this->strlen_max = (strlen('total') > $this->strlen_max ? strlen('total') : $this->strlen_max); + $this->strlen_max += 4; return $result; } }
« previous php.pear.dev (#4138) next »