fix: quick fix to enforce support of x509 database connection on install (#6157)
[openemr.git] / gacl / profiler.inc.php
blobec59825b74ad9335ec1c688e2934a8b5d7932cf7
1 <?php
2 /********************************************************************************\
3 * Copyright (C) Carl Taylor (cjtaylor@adepteo.com) *
4 * Copyright (C) Torben Nehmer (torben@nehmer.net) for Code Cleanup *
5 * *
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. *
10 * *
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. *
15 * *
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
22 class Profiler {
23 var $description;
24 var $startTime;
25 var $endTime;
26 var $initTime;
27 var $cur_timer;
28 var $stack;
29 var $trail;
30 var $trace;
31 var $count;
32 var $running;
34 /**
35 * Initialise the timer. with the current micro time
37 function __construct( $output_enabled=false, $trace_enabled=false)
39 $this->description = array();
40 $this->startTime = array();
41 $this->endTime = array();
42 $this->initTime = 0;
43 $this->cur_timer = "";
44 $this->stack = array();
45 $this->trail = "";
46 $this->trace = "";
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');
55 // Public Methods
57 /**
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;
72 else
73 $this->count[$name]++;
76 /**
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);
86 else
87 $this->running[$name] += $this->elapsedTime($name);
88 $this->cur_timer=array_pop($this->stack);
89 $this->__resumeTimer($this->cur_timer);
92 /**
93 * measure the elapsed time of a timer without stoping the timer if
94 * it is still running
96 function elapsedTime($name){
97 // This shouldn't happen, but it does once.
98 if (!array_key_exists($name,$this->startTime))
99 return 0;
101 if(array_key_exists($name,$this->endTime)){
102 return ($this->endTime[$name] - $this->startTime[$name]);
103 } else {
104 $now=$this->getMicroTime();
105 return ($now - $this->startTime[$name]);
107 }//end start_time
110 * Measure the elapsed time since the profile class was initialised
113 function elapsedOverall(){
114 $oaTime = $this->getMicroTime() - $this->initTime;
115 return($oaTime);
116 }//end start_time
119 * print out a log of all the timers that were registered
122 function printTimers($enabled=false)
124 if($this->output_enabled||$enabled){
125 $TimedTotal = 0;
126 $tot_perc = 0;
127 ksort($this->description);
128 print("<pre>\n");
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;
141 $tot_perc+=$perc;
142 // $perc=sprintf("%3.2f", $perc );
143 printf( "%3d %3.4f ms (%3.2f %%) %s\n", $count, $total*1000, $perc, $key);
146 echo "\n";
148 $missed=$oaTime-$TimedTotal;
149 $perc = ($missed/$oaTime)*100;
150 $tot_perc+=$perc;
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";
160 print("</pre>");
164 function printTrace( $enabled=false )
166 if($this->trace_enabled||$enabled){
167 print("<pre>");
168 print("Trace\n$this->trace\n\n");
169 print("</pre>");
173 /// Internal Use Only Functions
176 * Get the current time as accuratly as possible
179 function getMicroTime(){
180 $tmp=explode(" ",microtime());
181 $rt=$tmp[0]+$tmp[1];
182 return $rt;
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);
203 else
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);