1 |
<?php |
2 |
// |
3 |
// +----------------------------------------------------------------------+ |
4 |
// | PHP version 4.0 | |
5 |
// +----------------------------------------------------------------------+ |
6 |
// | Copyright (c) 1997-2001 The PHP Group | |
7 |
// +----------------------------------------------------------------------+ |
8 |
// | This source file is subject to version 2.02 of the PHP license, | |
9 |
// | that is bundled with this package in the file LICENSE, and is | |
10 |
// | available at through the world-wide-web at | |
11 |
// | http://www.php.net/license/2_02.txt. | |
12 |
// | If you did not receive a copy of the PHP license and are unable to | |
13 |
// | 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 |
// | Authors: Sebastian Bergmann <sb@sebastian-bergmann.de> | |
17 |
// +----------------------------------------------------------------------+ |
18 |
// |
19 |
// $Id: Timer.php,v 1.8 2001/03/03 06:35:25 sbergmann Exp $ |
20 |
// |
21 |
|
22 |
/** |
23 |
* Benchmark::Timer |
24 |
* |
25 |
* Purpose: |
26 |
* |
27 |
* Timing Script Execution, Generating Profiling Information |
28 |
* |
29 |
* Example: |
30 |
* |
31 |
* $timer = new Benchmark_Timer; |
32 |
* |
33 |
* $timer->start(); |
34 |
* $timer->setMarker('Marker 1'); |
35 |
* $timer->stop(); |
36 |
* |
37 |
* $profiling = $timer->getProfiling(); |
38 |
* |
39 |
* @author Sebastian Bergmann <sb@sebastian-bergmann.de> |
40 |
* @version $Revision: 1.8 $ |
41 |
* @access public |
42 |
*/ |
43 |
class Benchmark_Timer |
44 |
{ |
45 |
/** |
46 |
* Contains the markers |
47 |
* |
48 |
* @var array |
49 |
* @access public |
50 |
*/ |
51 |
var $markers = array(); |
52 |
|
53 |
/** |
54 |
* Set "Start" marker. |
55 |
* |
56 |
* @see setMarker(), stop() |
57 |
* @access public |
58 |
*/ |
59 |
function start() |
60 |
{ |
61 |
$this->setMarker('Start'); |
62 |
} |
63 |
|
64 |
/** |
65 |
* Set "Stop" marker. |
66 |
* |
67 |
* @see setMarker(), start() |
68 |
* @access public |
69 |
*/ |
70 |
function stop() |
71 |
{ |
72 |
$this->setMarker('Stop'); |
73 |
} |
74 |
|
75 |
/** |
76 |
* Set marker. |
77 |
* |
78 |
* @param string name of the marker to be set |
79 |
* @see start(), stop() |
80 |
* @access public |
81 |
*/ |
82 |
function setMarker($name) |
83 |
{ |
84 |
$microtime = explode(' ', microtime()); |
85 |
$this->markers[$name] = $microtime[1] . substr($microtime[0], 1); |
86 |
} |
87 |
|
88 |
/** |
89 |
* Returns the time elapsed betweens two markers. |
90 |
* |
91 |
* @param string $start start marker, defaults to "Start" |
92 |
* @param string $end end marker, defaults to "Stop" |
93 |
* @return double $time_elapsed time elapsed between $start and $end |
94 |
* @access public |
95 |
*/ |
96 |
function timeElapsed($start = 'Start', $end = 'Stop') |
97 |
{ |
98 |
if (extension_loaded('bcmath')) { |
99 |
return bcsub($this->markers[$end], $this->markers[$start], 6); |
100 |
} else { |
101 |
return $this->markers[$end] - $this->markers[$start]; |
102 |
} |
103 |
} |
104 |
|
105 |
/** |
106 |
* Returns profiling information. |
107 |
* |
108 |
* $profiling[x]['name'] = name of marker x |
109 |
* $profiling[x]['time'] = time index of marker x |
110 |
* $profiling[x]['diff'] = execution time from marker x-1 to this marker x |
111 |
* $profiling[x]['total'] = total execution time up to marker x |
112 |
* |
113 |
* @return array $profiling |
114 |
* @access public |
115 |
*/ |
116 |
function getProfiling() |
117 |
{ |
118 |
$i = 0; |
119 |
$total = 0; |
120 |
$result = array(); |
121 |
|
122 |
foreach ($this->markers as $marker => $time) { |
123 |
if ($marker == 'Start') { |
124 |
$diff = '-'; |
125 |
} else { |
126 |
if (extension_loaded('bcmath')) { |
127 |
$diff = bcsub($time, $temp, 6); |
128 |
$total = bcadd($total, $diff, 6); |
129 |
} else { |
130 |
$diff = $time - $temp; |
131 |
$total = $total + $diff; |
132 |
} |
133 |
} |
134 |
|
135 |
$result[$i]['name'] = $marker; |
136 |
$result[$i]['time'] = $time; |
137 |
$result[$i]['diff'] = $diff; |
138 |
$result[$i]['total'] = $total; |
139 |
|
140 |
$temp = $time; |
141 |
$i++; |
142 |
} |
143 |
|
144 |
return $result; |
145 |
} |
146 |
} |
147 |
?> |