3 // +------------------------------------------------------------------------+
4 // | PEAR :: Benchmark |
5 // +------------------------------------------------------------------------+
6 // | Copyright (c) 2001-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
7 // +------------------------------------------------------------------------+
8 // | This source file is subject to version 3.00 of the PHP License, |
9 // | that is available at http://www.php.net/license/3_0.txt. |
10 // | If you did not receive a copy of the PHP license and are unable to |
11 // | obtain it through the world-wide-web, please send a note to |
12 // | license@php.net so we can mail you a copy immediately. |
13 // +------------------------------------------------------------------------+
15 // $Id: Timer.php,v 1.13 2005/05/24 13:42:06 toggg Exp $
18 require_once 'PEAR.php';
21 * Provides timing and profiling information.
23 * Example 1: Automatic profiling start, stop, and output.
27 * require_once 'Benchmark/Timer.php';
29 * $timer = new Benchmark_Timer(TRUE);
30 * $timer->setMarker('Marker 1');
34 * Example 2: Manual profiling start, stop, and output.
38 * require_once 'Benchmark/Timer.php';
40 * $timer = new Benchmark_Timer();
42 * $timer->setMarker('Marker 1');
45 * $timer->display(); // to output html formated
47 * $profiling = $timer->getProfiling(); // get the profiler info as an associative array
51 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
52 * @author Ludovico Magnocavallo <ludo@sumatrasolutions.com>
53 * @copyright Copyright © 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de>
54 * @license http://www.php.net/license/3_0.txt The PHP License, Version 3.0
55 * @category Benchmarking
58 class Benchmark_Timer
extends PEAR
{
60 * Contains the markers.
65 var $markers = array();
68 * Auto-start and stop timer.
76 * Max marker name length for non-html output.
81 var $maxStringLength = 0;
86 * @param boolean $auto
89 function Benchmark_Timer($auto = FALSE) {
104 function _Benchmark_Timer() {
112 * Set "Start" marker.
114 * @see setMarker(), stop()
118 $this->setMarker('Start');
124 * @see setMarker(), start()
128 $this->setMarker('Stop');
134 * @param string $name Name of the marker to be set.
135 * @see start(), stop()
138 function setMarker($name) {
139 $this->markers
[$name] = $this->_getMicrotime();
143 * Returns the time elapsed betweens two markers.
145 * @param string $start start marker, defaults to "Start"
146 * @param string $end end marker, defaults to "Stop"
147 * @return double $time_elapsed time elapsed between $start and $end
150 function timeElapsed($start = 'Start', $end = 'Stop') {
151 if ($end == 'Stop' && !isset($this->markers
['Stop'])) {
152 $this->markers
['Stop'] = $this->_getMicrotime();
155 if (extension_loaded('bcmath')) {
156 return bcsub($this->markers
[$end], $this->markers
[$start], 6);
158 return $this->markers
[$end] - $this->markers
[$start];
163 * Returns profiling information.
165 * $profiling[x]['name'] = name of marker x
166 * $profiling[x]['time'] = time index of marker x
167 * $profiling[x]['diff'] = execution time from marker x-1 to this marker x
168 * $profiling[x]['total'] = total execution time up to marker x
173 function getProfiling() {
176 $temp = reset($this->markers
);
177 $this->maxStringLength
= 0;
179 foreach ($this->markers
as $marker => $time) {
180 if (extension_loaded('bcmath')) {
181 $diff = bcsub($time, $temp, 6);
182 $total = bcadd($total, $diff, 6);
184 $diff = $time - $temp;
185 $total = $total +
$diff;
188 $result[$i]['name'] = $marker;
189 $result[$i]['time'] = $time;
190 $result[$i]['diff'] = $diff;
191 $result[$i]['total'] = $total;
193 $this->maxStringLength
= (strlen($marker) > $this->maxStringLength ?
strlen($marker) +
1 : $this->maxStringLength
);
199 $result[0]['diff'] = '-';
200 $result[0]['total'] = '-';
201 $this->maxStringLength
= (strlen('total') > $this->maxStringLength ?
strlen('total') : $this->maxStringLength
);
202 $this->maxStringLength +
= 2;
208 * Return formatted profiling information.
210 * @param boolean $showTotal Optionnaly includes total in output, default no
212 * @see getProfiling()
215 function getOutput($showTotal = FALSE)
217 if (function_exists('version_compare') &&
218 version_compare(phpversion(), '4.1', 'ge'))
220 $http = isset($_SERVER['SERVER_PROTOCOL']);
222 global $HTTP_SERVER_VARS;
223 $http = isset($HTTP_SERVER_VARS['SERVER_PROTOCOL']);
226 $total = $this->TimeElapsed();
227 $result = $this->getProfiling();
231 $out = '<table border="1">'."\n";
232 $out .= '<tr><td> </td><td align="center"><b>time index</b></td><td align="center"><b>ex time</b></td><td align="center"><b>%</b></td>'.
234 '<td align="center"><b>elapsed</b></td><td align="center"><b>%</b></td>'
237 $dashes = $out = str_pad("\n",
238 $this->maxStringLength +
($showTotal ?
70 : 45), '-', STR_PAD_LEFT
);
239 $out .= str_pad('marker', $this->maxStringLength
) .
240 str_pad("time index", 22) .
241 str_pad("ex time", 16) .
242 str_pad("perct ", 8) .
243 ($showTotal ?
' '.str_pad("elapsed", 16)."perct" : '')."\n" .
247 foreach ($result as $k => $v) {
248 $perc = (($v['diff'] * 100) / $total);
249 $tperc = (($v['total'] * 100) / $total);
252 $out .= "<tr><td><b>" . $v['name'] .
253 "</b></td><td>" . $v['time'] .
254 "</td><td>" . $v['diff'] .
255 "</td><td align=\"right\">" . number_format($perc, 2, '.', '') .
258 "<td>" . $v['total'] .
259 "</td><td align=\"right\">" .
260 number_format($tperc, 2, '.', '') .
264 $out .= str_pad($v['name'], $this->maxStringLength
, ' ') .
265 str_pad($v['time'], 22) .
266 str_pad($v['diff'], 14) .
267 str_pad(number_format($perc, 2, '.', '')."%",8, ' ', STR_PAD_LEFT
) .
269 str_pad($v['total'], 14) .
270 str_pad(number_format($tperc, 2, '.', '')."%",
271 8, ' ', STR_PAD_LEFT
) : '').
279 $out .= "<tr style='background: silver;'><td><b>total</b></td><td>-</td><td>${total}</td><td>100.00%</td>".($showTotal ?
"<td>-</td><td>-</td>" : "")."</tr>\n";
280 $out .= "</table>\n";
282 $out .= str_pad('total', $this->maxStringLength
);
283 $out .= str_pad('-', 22);
284 $out .= str_pad($total, 15);
293 * Prints the information returned by getOutput().
295 * @param boolean $showTotal Optionnaly includes total in output, default no
299 function display($showTotal = FALSE) {
300 print $this->getOutput($showTotal);
304 * Wrapper for microtime().
310 function _getMicrotime() {
311 $microtime = explode(' ', microtime());
312 return $microtime[1] . substr($microtime[0], 1);