2 /********************************************************************************\
3 * Copyright (C) Carl Taylor (cjtaylor@adepteo.com) *
4 * Copyright (C) Torben Nehmer (torben@nehmer.net) for Code Cleanup *
6 * This program is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU General Public License *
8 * as published by the Free Software Foundation; either version 2 *
9 * of the License, or (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 \********************************************************************************/
21 /// Enable multiple timers to aid profiling of performance over sections of code
35 * Initialise the timer. with the current micro time
37 function Profiler( $output_enabled=false, $trace_enabled=false)
39 $this->description = array();
40 $this->startTime = array();
41 $this->endTime = array();
43 $this->cur_timer = "";
44 $this->stack = array();
47 $this->count = array();
48 $this->running = array();
49 $this->initTime = $this->getMicroTime();
50 $this->output_enabled = $output_enabled;
51 $this->trace_enabled = $trace_enabled;
52 $this->startTimer('unprofiled');
58 * Start an individual timer
59 * This will pause the running timer and place it on a stack.
60 * @param string $name name of the timer
61 * @param string optional $desc description of the timer
63 function startTimer($name, $desc="" ){
64 $this->trace.="start $name\n";
65 $n=array_push( $this->stack, $this->cur_timer );
66 $this->__suspendTimer( $this->stack[$n-1] );
67 $this->startTime[$name] = $this->getMicroTime();
68 $this->cur_timer=$name;
69 $this->description[$name] = $desc;
70 if (!array_key_exists($name,$this->count))
71 $this->count[$name] = 1;
73 $this->count[$name]++;
77 * Stop an individual timer
78 * Restart the timer that was running before this one
79 * @param string $name name of the timer
81 function stopTimer($name){
82 $this->trace.="stop $name\n";
83 $this->endTime[$name] = $this->getMicroTime();
84 if (!array_key_exists($name, $this->running))
85 $this->running[$name] = $this->elapsedTime($name);
87 $this->running[$name] += $this->elapsedTime($name);
88 $this->cur_timer=array_pop($this->stack);
89 $this->__resumeTimer($this->cur_timer);
93 * measure the elapsed time of a timer without stoping the timer if
96 function elapsedTime($name){
97 // This shouldn't happen, but it does once.
98 if (!array_key_exists($name,$this->startTime))
101 if(array_key_exists($name,$this->endTime)){
102 return ($this->endTime[$name] - $this->startTime[$name]);
104 $now=$this->getMicroTime();
105 return ($now - $this->startTime[$name]);
110 * Measure the elapsed time since the profile class was initialised
113 function elapsedOverall(){
114 $oaTime = $this->getMicroTime() - $this->initTime;
119 * print out a log of all the timers that were registered
122 function printTimers($enabled=false)
124 if($this->output_enabled||$enabled){
127 ksort($this->description);
129 $oaTime = $this->getMicroTime() - $this->initTime;
130 echo"============================================================================\n";
131 echo " PROFILER OUTPUT\n";
132 echo"============================================================================\n";
133 print( "Calls Time Routine\n");
134 echo"-----------------------------------------------------------------------------\n";
135 while (list ($key, $val) = each ($this->description)) {
136 $t = $this->elapsedTime($key);
137 $total = $this->running[$key];
138 $count = $this->count[$key];
139 $TimedTotal += $total;
140 $perc = ($total/$oaTime)*100;
142 // $perc=sprintf("%3.2f", $perc );
143 printf( "%3d %3.4f ms (%3.2f %%) %s\n", $count, $total*1000, $perc, $key);
148 $missed=$oaTime-$TimedTotal;
149 $perc = ($missed/$oaTime)*100;
151 // $perc=sprintf("%3.2f", $perc );
152 printf( " %3.4f ms (%3.2f %%) %s\n", $missed*1000,$perc, "Missed");
154 echo"============================================================================\n";
156 printf( " %3.4f ms (%3.2f %%) %s\n", $oaTime*1000,$tot_perc, "OVERALL TIME");
158 echo"============================================================================\n";
164 function printTrace( $enabled=false )
166 if($this->trace_enabled||$enabled){
168 print("Trace\n$this->trace\n\n");
173 /// Internal Use Only Functions
176 * Get the current time as accuratly as possible
179 function getMicroTime(){
180 $tmp=split(" ",microtime());
186 * resume an individual timer
189 function __resumeTimer($name){
190 $this->trace.="resume $name\n";
191 $this->startTime[$name] = $this->getMicroTime();
195 * suspend an individual timer
198 function __suspendTimer($name){
199 $this->trace.="suspend $name\n";
200 $this->endTime[$name] = $this->getMicroTime();
201 if (!array_key_exists($name, $this->running))
202 $this->running[$name] = $this->elapsedTime($name);
204 $this->running[$name] += $this->elapsedTime($name);
208 function profiler_start($name) {
209 if (array_key_exists("midcom_profiler",$GLOBALS))
210 $GLOBALS["midcom_profiler"]->startTimer ($name);
213 function profiler_stop($name) {
214 if (array_key_exists("midcom_profiler",$GLOBALS))
215 $GLOBALS["midcom_profiler"]->stopTimer ($name);