[ Index ] |
|
Code source de PHP PEAR 1.4.5 |
1 <?php 2 // 3 // +------------------------------------------------------------------------+ 4 // | PEAR :: Benchmark | 5 // +------------------------------------------------------------------------+ 6 // | Copyright (c) 2001-2006 Sebastian Bergmann <sb@sebastian-bergmann.de>. | 7 // +------------------------------------------------------------------------+ 8 // | This source file is subject to the New BSD license, That is bundled | 9 // | with this package in the file LICENSE, and is available through | 10 // | the world-wide-web at | 11 // | http://www.opensource.org/licenses/bsd-license.php | 12 // | If you did not receive a copy of the new BSDlicense and are unable | 13 // | to obtain it through the world-wide-web, please send a note to | 14 // | license@php.net so we can mail you a copy immediately. | 15 // +------------------------------------------------------------------------+ 16 // 17 // $Id: Timer.php,v 1.16 2006/03/01 13:41:39 matthias Exp $ 18 // 19 20 require_once 'PEAR.php'; 21 22 /** 23 * Provides timing and profiling information. 24 * 25 * Example 1: Automatic profiling start, stop, and output. 26 * 27 * <code> 28 * <?php 29 * require_once 'Benchmark/Timer.php'; 30 * 31 * $timer = new Benchmark_Timer(TRUE); 32 * $timer->setMarker('Marker 1'); 33 * ?> 34 * </code> 35 * 36 * Example 2: Manual profiling start, stop, and output. 37 * 38 * <code> 39 * <?php 40 * require_once 'Benchmark/Timer.php'; 41 * 42 * $timer = new Benchmark_Timer(); 43 * $timer->start(); 44 * $timer->setMarker('Marker 1'); 45 * $timer->stop(); 46 * 47 * $timer->display(); // to output html formated 48 * // AND/OR : 49 * $profiling = $timer->getProfiling(); // get the profiler info as an associative array 50 * ?> 51 * </code> 52 * 53 * @author Sebastian Bergmann <sb@sebastian-bergmann.de> 54 * @author Ludovico Magnocavallo <ludo@sumatrasolutions.com> 55 * @copyright Copyright © 2002-2005 Sebastian Bergmann <sb@sebastian-bergmann.de> 56 * @license http://www.php.net/license/3_0.txt The PHP License, Version 3.0 57 * @category Benchmarking 58 * @package Benchmark 59 */ 60 class Benchmark_Timer extends PEAR { 61 /** 62 * Contains the markers. 63 * 64 * @var array 65 * @access private 66 */ 67 var $markers = array(); 68 69 /** 70 * Auto-start and stop timer. 71 * 72 * @var boolean 73 * @access private 74 */ 75 var $auto = FALSE; 76 77 /** 78 * Max marker name length for non-html output. 79 * 80 * @var integer 81 * @access private 82 */ 83 var $maxStringLength = 0; 84 85 /** 86 * Constructor. 87 * 88 * @param boolean $auto 89 * @access public 90 */ 91 function Benchmark_Timer($auto = FALSE) { 92 $this->auto = $auto; 93 94 if ($this->auto) { 95 $this->start(); 96 } 97 98 $this->PEAR(); 99 } 100 101 /** 102 * Destructor. 103 * 104 * @access private 105 */ 106 function _Benchmark_Timer() { 107 if ($this->auto) { 108 $this->stop(); 109 $this->display(); 110 } 111 } 112 113 /** 114 * Set "Start" marker. 115 * 116 * @see setMarker(), stop() 117 * @access public 118 */ 119 function start() { 120 $this->setMarker('Start'); 121 } 122 123 /** 124 * Set "Stop" marker. 125 * 126 * @see setMarker(), start() 127 * @access public 128 */ 129 function stop() { 130 $this->setMarker('Stop'); 131 } 132 133 /** 134 * Set marker. 135 * 136 * @param string $name Name of the marker to be set. 137 * @see start(), stop() 138 * @access public 139 */ 140 function setMarker($name) { 141 $this->markers[$name] = $this->_getMicrotime(); 142 } 143 144 /** 145 * Returns the time elapsed betweens two markers. 146 * 147 * @param string $start start marker, defaults to "Start" 148 * @param string $end end marker, defaults to "Stop" 149 * @return double $time_elapsed time elapsed between $start and $end 150 * @access public 151 */ 152 function timeElapsed($start = 'Start', $end = 'Stop') { 153 if ($end == 'Stop' && !isset($this->markers['Stop'])) { 154 $this->markers['Stop'] = $this->_getMicrotime(); 155 } 156 157 if (extension_loaded('bcmath')) { 158 return bcsub($this->markers[$end], $this->markers[$start], 6); 159 } else { 160 return $this->markers[$end] - $this->markers[$start]; 161 } 162 } 163 164 /** 165 * Returns profiling information. 166 * 167 * $profiling[x]['name'] = name of marker x 168 * $profiling[x]['time'] = time index of marker x 169 * $profiling[x]['diff'] = execution time from marker x-1 to this marker x 170 * $profiling[x]['total'] = total execution time up to marker x 171 * 172 * @return array 173 * @access public 174 */ 175 function getProfiling() { 176 $i = $total = 0; 177 $result = array(); 178 $temp = reset($this->markers); 179 $this->maxStringLength = 0; 180 181 foreach ($this->markers as $marker => $time) { 182 if (extension_loaded('bcmath')) { 183 $diff = bcsub($time, $temp, 6); 184 $total = bcadd($total, $diff, 6); 185 } else { 186 $diff = $time - $temp; 187 $total = $total + $diff; 188 } 189 190 $result[$i]['name'] = $marker; 191 $result[$i]['time'] = $time; 192 $result[$i]['diff'] = $diff; 193 $result[$i]['total'] = $total; 194 195 $this->maxStringLength = (strlen($marker) > $this->maxStringLength ? strlen($marker) + 1 : $this->maxStringLength); 196 197 $temp = $time; 198 $i++; 199 } 200 201 $result[0]['diff'] = '-'; 202 $result[0]['total'] = '-'; 203 $this->maxStringLength = (strlen('total') > $this->maxStringLength ? strlen('total') : $this->maxStringLength); 204 $this->maxStringLength += 2; 205 206 return $result; 207 } 208 209 /** 210 * Return formatted profiling information. 211 * 212 * @param boolean $showTotal Optionnaly includes total in output, default no 213 * @param string $format output format (auto, plain or html), default auto 214 * @return string 215 * @see getProfiling() 216 * @access public 217 */ 218 function getOutput($showTotal = FALSE, $format = 'auto') { 219 if ($format == 'auto') { 220 if (function_exists('version_compare') && 221 version_compare(phpversion(), '4.1', 'ge')) 222 { 223 $format = isset($_SERVER['SERVER_PROTOCOL']) ? 'html' : 'plain'; 224 } else { 225 global $HTTP_SERVER_VARS; 226 $format = isset($HTTP_SERVER_VARS['SERVER_PROTOCOL']) ? 'html' : 'plain'; 227 } 228 } 229 230 $total = $this->TimeElapsed(); 231 $result = $this->getProfiling(); 232 $dashes = ''; 233 234 if ($format == 'html') { 235 $out = '<table border="1">'."\n"; 236 $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>'. 237 ($showTotal ? 238 '<td align="center"><b>elapsed</b></td><td align="center"><b>%</b></td>' 239 : '')."</tr>\n"; 240 } else { 241 $dashes = $out = str_pad("\n", 242 $this->maxStringLength + ($showTotal ? 70 : 45), '-', STR_PAD_LEFT); 243 $out .= str_pad('marker', $this->maxStringLength) . 244 str_pad("time index", 22) . 245 str_pad("ex time", 16) . 246 str_pad("perct ", 8) . 247 ($showTotal ? ' '.str_pad("elapsed", 16)."perct" : '')."\n" . 248 $dashes; 249 } 250 251 foreach ($result as $k => $v) { 252 $perc = (($v['diff'] * 100) / $total); 253 $tperc = (($v['total'] * 100) / $total); 254 255 if ($format == 'html') { 256 $out .= "<tr><td><b>" . $v['name'] . 257 "</b></td><td>" . $v['time'] . 258 "</td><td>" . $v['diff'] . 259 "</td><td align=\"right\">" . number_format($perc, 2, '.', '') . 260 "%</td>". 261 ($showTotal ? 262 "<td>" . $v['total'] . 263 "</td><td align=\"right\">" . 264 number_format($tperc, 2, '.', '') . 265 "%</td>" : ''). 266 "</tr>\n"; 267 } else { 268 $out .= str_pad($v['name'], $this->maxStringLength, ' ') . 269 str_pad($v['time'], 22) . 270 str_pad($v['diff'], 14) . 271 str_pad(number_format($perc, 2, '.', '')."%",8, ' ', STR_PAD_LEFT) . 272 ($showTotal ? ' '. 273 str_pad($v['total'], 14) . 274 str_pad(number_format($tperc, 2, '.', '')."%", 275 8, ' ', STR_PAD_LEFT) : ''). 276 "\n"; 277 } 278 279 $out .= $dashes; 280 } 281 282 if ($format == 'html') { 283 $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"; 284 $out .= "</table>\n"; 285 } else { 286 $out .= str_pad('total', $this->maxStringLength); 287 $out .= str_pad('-', 22); 288 $out .= str_pad($total, 15); 289 $out .= "100.00%\n"; 290 $out .= $dashes; 291 } 292 293 return $out; 294 } 295 296 /** 297 * Prints the information returned by getOutput(). 298 * 299 * @param boolean $showTotal Optionnaly includes total in output, default no 300 * @param string $format output format (auto, plain or html), default auto 301 * @see getOutput() 302 * @access public 303 */ 304 function display($showTotal = FALSE, $format = 'auto') { 305 print $this->getOutput($showTotal, $format); 306 } 307 308 /** 309 * Wrapper for microtime(). 310 * 311 * @return float 312 * @access private 313 * @since 1.3.0 314 */ 315 function _getMicrotime() { 316 $microtime = explode(' ', microtime()); 317 return $microtime[1] . substr($microtime[0], 1); 318 } 319 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 14:08:00 2007 | par Balluche grâce à PHPXref 0.7 |