Fixing content type ordering when content_type is not defined.
[akelos.git] / vendor / pear / Benchmark / Timer.php
blob6a40ecf00f0de090940c5bcec865a443f626bdf5
1 <?php
2 //
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';
20 /**
21 * Provides timing and profiling information.
23 * Example 1: Automatic profiling start, stop, and output.
25 * <code>
26 * <?php
27 * require_once 'Benchmark/Timer.php';
29 * $timer = new Benchmark_Timer(TRUE);
30 * $timer->setMarker('Marker 1');
31 * ?>
32 * </code>
34 * Example 2: Manual profiling start, stop, and output.
36 * <code>
37 * <?php
38 * require_once 'Benchmark/Timer.php';
40 * $timer = new Benchmark_Timer();
41 * $timer->start();
42 * $timer->setMarker('Marker 1');
43 * $timer->stop();
45 * $timer->display(); // to output html formated
46 * // AND/OR :
47 * $profiling = $timer->getProfiling(); // get the profiler info as an associative array
48 * ?>
49 * </code>
51 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
52 * @author Ludovico Magnocavallo <ludo@sumatrasolutions.com>
53 * @copyright Copyright &copy; 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
56 * @package Benchmark
58 class Benchmark_Timer extends PEAR {
59 /**
60 * Contains the markers.
62 * @var array
63 * @access private
65 var $markers = array();
67 /**
68 * Auto-start and stop timer.
70 * @var boolean
71 * @access private
73 var $auto = FALSE;
75 /**
76 * Max marker name length for non-html output.
78 * @var integer
79 * @access private
81 var $maxStringLength = 0;
83 /**
84 * Constructor.
86 * @param boolean $auto
87 * @access public
89 function Benchmark_Timer($auto = FALSE) {
90 $this->auto = $auto;
92 if ($this->auto) {
93 $this->start();
96 $this->PEAR();
99 /**
100 * Destructor.
102 * @access private
104 function _Benchmark_Timer() {
105 if ($this->auto) {
106 $this->stop();
107 $this->display();
112 * Set "Start" marker.
114 * @see setMarker(), stop()
115 * @access public
117 function start() {
118 $this->setMarker('Start');
122 * Set "Stop" marker.
124 * @see setMarker(), start()
125 * @access public
127 function stop() {
128 $this->setMarker('Stop');
132 * Set marker.
134 * @param string $name Name of the marker to be set.
135 * @see start(), stop()
136 * @access public
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
148 * @access public
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);
157 } else {
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
170 * @return array
171 * @access public
173 function getProfiling() {
174 $i = $total = 0;
175 $result = array();
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);
183 } else {
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);
195 $temp = $time;
196 $i++;
199 $result[0]['diff'] = '-';
200 $result[0]['total'] = '-';
201 $this->maxStringLength = (strlen('total') > $this->maxStringLength ? strlen('total') : $this->maxStringLength);
202 $this->maxStringLength += 2;
204 return $result;
208 * Return formatted profiling information.
210 * @param boolean $showTotal Optionnaly includes total in output, default no
211 * @return string
212 * @see getProfiling()
213 * @access public
215 function getOutput($showTotal = FALSE)
217 if (function_exists('version_compare') &&
218 version_compare(phpversion(), '4.1', 'ge'))
220 $http = isset($_SERVER['SERVER_PROTOCOL']);
221 } else {
222 global $HTTP_SERVER_VARS;
223 $http = isset($HTTP_SERVER_VARS['SERVER_PROTOCOL']);
226 $total = $this->TimeElapsed();
227 $result = $this->getProfiling();
228 $dashes = '';
230 if ($http) {
231 $out = '<table border="1">'."\n";
232 $out .= '<tr><td>&nbsp;</td><td align="center"><b>time index</b></td><td align="center"><b>ex time</b></td><td align="center"><b>%</b></td>'.
233 ($showTotal ?
234 '<td align="center"><b>elapsed</b></td><td align="center"><b>%</b></td>'
235 : '')."</tr>\n";
236 } else {
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" .
244 $dashes;
247 foreach ($result as $k => $v) {
248 $perc = (($v['diff'] * 100) / $total);
249 $tperc = (($v['total'] * 100) / $total);
251 if ($http) {
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, '.', '') .
256 "%</td>".
257 ($showTotal ?
258 "<td>" . $v['total'] .
259 "</td><td align=\"right\">" .
260 number_format($tperc, 2, '.', '') .
261 "%</td>" : '').
262 "</tr>\n";
263 } else {
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) .
268 ($showTotal ? ' '.
269 str_pad($v['total'], 14) .
270 str_pad(number_format($tperc, 2, '.', '')."%",
271 8, ' ', STR_PAD_LEFT) : '').
272 "\n";
275 $out .= $dashes;
278 if ($http) {
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";
281 } else {
282 $out .= str_pad('total', $this->maxStringLength);
283 $out .= str_pad('-', 22);
284 $out .= str_pad($total, 15);
285 $out .= "100.00%\n";
286 $out .= $dashes;
289 return $out;
293 * Prints the information returned by getOutput().
295 * @param boolean $showTotal Optionnaly includes total in output, default no
296 * @see getOutput()
297 * @access public
299 function display($showTotal = FALSE) {
300 print $this->getOutput($showTotal);
304 * Wrapper for microtime().
306 * @return float
307 * @access private
308 * @since 1.3.0
310 function _getMicrotime() {
311 $microtime = explode(' ', microtime());
312 return $microtime[1] . substr($microtime[0], 1);