PluginProbe
ShiftController Employee Shift Scheduling / 4.9.72
ShiftController Employee Shift Scheduling v4.9.72
4.9.97 4.9.96 4.9.95 4.9.74 4.9.75 4.9.76 4.9.77 4.9.78 4.9.84 4.9.85 4.9.87 4.9.91 4.9.92 trunk 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 3.2.4 All 38 releases
shiftcontroller / hc3 / profiler.php

profiler.php in ShiftController Employee Shift Scheduling 4.9.72, at hc3/profiler.php

393 lines 13.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php if (! defined('ABSPATH')) exit; // Exit if accessed directly
2 class HC3_Profiler
3 {
4 private $benchmark = NULL;
5 protected $_available_sections = array(
6 'benchmarks',
7 'get',
8 'memory_usage',
9 'post',
10 'uri_string',
11 'controller_info',
12 'queries',
13 'http_headers',
14 'session_data',
15 'config'
16 );
17
18 protected $_query_toggle_count = 25;
19 protected $dbs = array();
20
21 public $_compile_benchmarks;
22 public $_compile_get;
23 public $_compile_memory_usage;
24 public $_compile_post;
25 public $_compile_uri_string;
26 public $_compile_controller_info;
27 public $_compile_queries;
28 public $_compile_http_headers;
29 public $_compile_session_data;
30 public $_compile_config;
31
32 public function add_db( $db )
33 {
34 $this->dbs[] = $db;
35 }
36
37 public function __construct()
38 {
39 $this->benchmark = new HC3_Profiler_Benchmark;
40
41 // default all sections to display
42 foreach ($this->_available_sections as $section){
43 if ( ! isset($config[$section])){
44 $this->{'_compile_' . $section} = TRUE;
45 }
46 }
47
48 // $this->set_sections($config);
49
50 // if wordpress
51 if( defined('WPINC') ){
52 global $wpdb;
53 $this->add_db( $wpdb );
54 }
55 }
56
57 function mark($name)
58 {
59 $this->benchmark->mark($name);
60 return $this;
61 }
62
63 public function set_sections($config)
64 {
65 foreach ($config as $method => $enable){
66 if (in_array($method, $this->_available_sections)){
67 $this->{'_compile_' . $method} = ($enable !== FALSE) ? TRUE : FALSE;
68 }
69 }
70 }
71
72 protected function _compile_benchmarks()
73 {
74 $profile = array();
75
76 foreach ( $this->benchmark->marker as $key => $val){
77 // We match the "end" marker so that the list ends
78 // up in the order that it was defined
79 if (preg_match("/(.+?)_end/i", $key, $match)){
80 if (isset($this->benchmark->marker[$match[1].'_end']) AND isset($this->benchmark->marker[$match[1].'_start'])){
81 $profile[$match[1]] = $this->benchmark->elapsed_time($match[1].'_start', $key);
82 }
83 }
84 }
85
86 $output = "\n\n";
87 $output .= '<fieldset id="ci_profiler_benchmarks" style="border:1px solid #900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
88 $output .= "\n";
89 $output .= '<legend style="color:#900;">&nbsp;&nbsp;'.'profiler_benchmarks'.'&nbsp;&nbsp;</legend>';
90 $output .= "\n";
91 $output .= "\n\n<table style='width:100%'>\n";
92
93 foreach ($profile as $key => $val){
94 $key = ucwords(str_replace(array('_', '-'), ' ', $key));
95 $output .= "<tr><td style='padding:5px;width:50%;color:#000;font-weight:bold;background-color:#ddd;'>".$key."&nbsp;&nbsp;</td><td style='padding:5px;width:50%;color:#900;font-weight:normal;background-color:#ddd;'>".$val."</td></tr>\n";
96 }
97
98 $output .= "</table>\n";
99 $output .= "</fieldset>";
100
101 return $output;
102 }
103
104 protected function _compile_queries()
105 {
106 $dbs = $this->dbs;
107
108 if( (count($dbs) == 0) && defined('NTS_DEVELOPMENT3') ){
109 global $wpdb;
110 $dbs[] = $wpdb;
111 }
112
113 if (count($dbs) == 0){
114 $output = "\n\n";
115 $output .= '<fieldset id="ci_profiler_queries" style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
116 $output .= "\n";
117 $output .= '<legend style="color:#0000FF;">&nbsp;&nbsp;'.'profiler_queries'.'&nbsp;&nbsp;</legend>';
118 $output .= "\n";
119 $output .= "\n\n<table style='border:none; width:100%;'>\n";
120 $output .="<tr><td style='width:100%;color:#0000FF;font-weight:normal;background-color:#eee;padding:5px'>".'profiler_no_db'."</td></tr>\n";
121 $output .= "</table>\n";
122 $output .= "</fieldset>";
123
124 return $output;
125 }
126
127 // Key words we want bolded
128 $highlight = array('SELECT', 'DISTINCT', 'FROM', 'WHERE', 'AND', 'LEFT&nbsp;JOIN', 'ORDER&nbsp;BY', 'GROUP&nbsp;BY', 'LIMIT', 'INSERT', 'INTO', 'VALUES', 'UPDATE', 'OR&nbsp;', 'HAVING', 'OFFSET', 'NOT&nbsp;IN', 'IN', 'LIKE', 'NOT&nbsp;LIKE', 'COUNT', 'MAX', 'MIN', 'ON', 'AS', 'AVG', 'SUM', '(', ')');
129
130 $output = "\n\n";
131
132 $count = 0;
133
134 foreach ($dbs as $db){
135 $queries = method_exists($db, 'queries') ? $db->queries() : $db->queries;
136 if( ! $queries ) $queries = array();
137
138 $count++;
139
140 $hide_queries = (count($queries) > $this->_query_toggle_count) ? ' display:none' : '';
141
142 $show_hide_js = '(<span style="cursor: pointer;" onclick="var s=document.getElementById(\'ci_profiler_queries_db_'.$count.'\').style;s.display=s.display==\'none\'?\'\':\'none\';this.innerHTML=this.innerHTML==\''.'profiler_section_hide'.'\'?\''.'profiler_section_show'.'\':\''.'profiler_section_hide'.'\';">'.'profiler_section_hide'.'</span>)';
143
144 if ($hide_queries != ''){
145 $show_hide_js = '(<span style="cursor: pointer;" onclick="var s=document.getElementById(\'ci_profiler_queries_db_'.$count.'\').style;s.display=s.display==\'none\'?\'\':\'none\';this.innerHTML=this.innerHTML==\''.'profiler_section_show'.'\'?\''.'profiler_section_hide'.'\':\''.'profiler_section_show'.'\';">'.'profiler_section_show'.'</span>)';
146 }
147
148 $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
149 $output .= "\n";
150 $output .= '<legend style="color:#0000FF;">&nbsp;&nbsp;'.'profiler_queries'.': '.count($queries).'&nbsp;&nbsp;'.$show_hide_js.'</legend>';
151 $output .= "\n";
152 $output .= "\n\n<table style='width:100%;{$hide_queries}' id='ci_profiler_queries_db_{$count}'>\n";
153
154 if (count($queries) == 0){
155 $output .= "<tr><td style='width:100%;color:#0000FF;font-weight:normal;background-color:#eee;padding:5px;'>".'profiler_no_queries'."</td></tr>\n";
156 }
157 else {
158 foreach ($queries as $key => $val){
159 list( $val, $time ) = $val;
160 $time = number_format( $time, 4);
161 $output .= "<tr><td style='padding:5px; vertical-align: top;width:1%;color:#900;font-weight:normal;background-color:#ddd;'>".$time."&nbsp;&nbsp;</td><td style='padding:5px; color:#000;font-weight:normal;background-color:#ddd;'>".$val."</td></tr>\n";
162 }
163 }
164
165 $output .= "</table>\n";
166 $output .= "</fieldset>";
167
168 }
169
170 return $output;
171 }
172
173 protected function _compile_get()
174 {
175 $output = "\n\n";
176 $output .= '<fieldset id="ci_profiler_get" style="border:1px solid #cd6e00;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
177 $output .= "\n";
178 $output .= '<legend style="color:#cd6e00;">&nbsp;&nbsp;'.'profiler_get_data'.'&nbsp;&nbsp;</legend>';
179 $output .= "\n";
180
181 if (count($_GET) == 0){
182 $output .= "<div style='color:#cd6e00;font-weight:normal;padding:4px 0 4px 0'>".'profiler_no_get'."</div>";
183 }
184 else {
185 $output .= "\n\n<table style='width:100%; border:none'>\n";
186
187 foreach ($_GET as $key => $val){
188 if ( ! is_numeric($key)){
189 $key = "'".$key."'";
190 }
191
192 $output .= "<tr><td style='width:50%;color:#000;background-color:#ddd;padding:5px'>&#36;_GET[".$key."]&nbsp;&nbsp; </td><td style='width:50%;padding:5px;color:#cd6e00;font-weight:normal;background-color:#ddd;'>";
193 if (is_array($val)){
194 $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
195 }
196 else {
197 $output .= htmlspecialchars(stripslashes($val));
198 }
199 $output .= "</td></tr>\n";
200 }
201
202 $output .= "</table>\n";
203 }
204 $output .= "</fieldset>";
205
206 return $output;
207 }
208
209 protected function _compile_post()
210 {
211 $output = "\n\n";
212 $output .= '<fieldset id="ci_profiler_post" style="border:1px solid #009900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
213 $output .= "\n";
214 $output .= '<legend style="color:#009900;">&nbsp;&nbsp;'.'profiler_post_data'.'&nbsp;&nbsp;</legend>';
215 $output .= "\n";
216
217 if (count($_POST) == 0){
218 $output .= "<div style='color:#009900;font-weight:normal;padding:4px 0 4px 0'>".'profiler_no_post'."</div>";
219 }
220 else {
221 $output .= "\n\n<table style='width:100%'>\n";
222
223 foreach ($_POST as $key => $val){
224 if ( ! is_numeric($key)){
225 $key = "'".$key."'";
226 }
227
228 $output .= "<tr><td style='width:50%;padding:5px;color:#000;background-color:#ddd;'>&#36;_POST[".$key."]&nbsp;&nbsp; </td><td style='width:50%;padding:5px;color:#009900;font-weight:normal;background-color:#ddd;'>";
229 if (is_array($val)){
230 $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, TRUE))) . "</pre>";
231 }
232 else {
233 $output .= htmlspecialchars(stripslashes($val));
234 }
235 $output .= "</td></tr>\n";
236 }
237
238 $output .= "</table>\n";
239 }
240 $output .= "</fieldset>";
241
242 return $output;
243 }
244
245 protected function _compile_uri_string()
246 {
247 return;
248 $output = "\n\n";
249 $output .= '<fieldset id="ci_profiler_uri_string" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
250 $output .= "\n";
251 $output .= '<legend style="color:#000;">&nbsp;&nbsp;'.'profiler_uri_string'.'&nbsp;&nbsp;</legend>';
252 $output .= "\n";
253
254 if ($this->CI->uri->uri_string == ''){
255 $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".'profiler_no_uri'."</div>";
256 }
257 else {
258 $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->uri->uri_string."</div>";
259 }
260
261 $output .= "</fieldset>";
262 return $output;
263 }
264
265 protected function _compile_controller_info()
266 {
267 return;
268 $output = "\n\n";
269 $output .= '<fieldset id="ci_profiler_controller_info" style="border:1px solid #995300;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
270 $output .= "\n";
271 $output .= '<legend style="color:#995300;">&nbsp;&nbsp;'.'profiler_controller_info'.'&nbsp;&nbsp;</legend>';
272 $output .= "\n";
273
274 $output .= "<div style='color:#995300;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->router->fetch_class()."/".$this->CI->router->fetch_method()."</div>";
275
276 $output .= "</fieldset>";
277
278 return $output;
279 }
280
281 protected function _compile_memory_usage()
282 {
283 $output = "\n\n";
284 $output .= '<fieldset id="ci_profiler_memory_usage" style="border:1px solid #5a0099;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
285 $output .= "\n";
286 $output .= '<legend style="color:#5a0099;">&nbsp;&nbsp;'.'profiler_memory_usage'.'&nbsp;&nbsp;</legend>';
287 $output .= "\n";
288
289 if (function_exists('memory_get_usage') && ($usage = memory_get_usage()) != ''){
290 $output .= "<div style='color:#5a0099;font-weight:normal;padding:4px 0 4px 0'>".number_format($usage).' bytes</div>';
291 }
292 else {
293 $output .= "<div style='color:#5a0099;font-weight:normal;padding:4px 0 4px 0'>".'profiler_no_memory'."</div>";
294 }
295
296 $output .= "</fieldset>";
297 return $output;
298 }
299
300 protected function _compile_http_headers()
301 {
302 $output = "\n\n";
303 $output .= '<fieldset id="ci_profiler_http_headers" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
304 $output .= "\n";
305 $output .= '<legend style="color:#000;">&nbsp;&nbsp;'.'profiler_headers'.'&nbsp;&nbsp;(<span style="cursor: pointer;" onclick="var s=document.getElementById(\'ci_profiler_httpheaders_table\').style;s.display=s.display==\'none\'?\'\':\'none\';this.innerHTML=this.innerHTML==\''.'profiler_section_show'.'\'?\''.'profiler_section_hide'.'\':\''.'profiler_section_show'.'\';">'.'profiler_section_show'.'</span>)</legend>';
306 $output .= "\n";
307
308 $output .= "\n\n<table style='width:100%;display:none' id='ci_profiler_httpheaders_table'>\n";
309
310 foreach (array('HTTP_ACCEPT', 'HTTP_USER_AGENT', 'HTTP_CONNECTION', 'SERVER_PORT', 'SERVER_NAME', 'REMOTE_ADDR', 'SERVER_SOFTWARE', 'HTTP_ACCEPT_LANGUAGE', 'SCRIPT_NAME', 'REQUEST_METHOD',' HTTP_HOST', 'REMOTE_HOST', 'CONTENT_TYPE', 'SERVER_PROTOCOL', 'QUERY_STRING', 'HTTP_ACCEPT_ENCODING', 'HTTP_X_FORWARDED_FOR') as $header){
311 $val = (isset($_SERVER[$header])) ? $_SERVER[$header] : '';
312 $output .= "<tr><td style='vertical-align: top;width:50%;padding:5px;color:#900;background-color:#ddd;'>".$header."&nbsp;&nbsp;</td><td style='width:50%;padding:5px;color:#000;background-color:#ddd;'>".$val."</td></tr>\n";
313 }
314
315 $output .= "</table>\n";
316 $output .= "</fieldset>";
317
318 return $output;
319 }
320
321 protected function _compile_config()
322 {
323 return;
324 }
325
326 private function _compile_session_data()
327 {
328 return;
329 }
330
331 public function render( $content )
332 {
333 $out = $content . $this->run();
334 return $out;
335 }
336
337 public function run()
338 {
339 $output = "<div class='hc-xs-hide' id='codeigniter_profiler' style='clear:both;background-color:#fff;padding:10px; margin-top: 5em;'>";
340 $fields_displayed = 0;
341
342 foreach ($this->_available_sections as $section){
343 if ($this->{'_compile_' . $section} !== FALSE){
344 $func = "_compile_{$section}";
345 $output .= $this->{$func}();
346 $fields_displayed++;
347 }
348 }
349
350 if ($fields_displayed == 0){
351 $output .= '<p style="border:1px solid #5a0099;padding:10px;margin:20px 0;background-color:#eee">'.'profiler_no_profiles'.'</p>';
352 }
353
354 $output .= '</div>';
355 return $output;
356 }
357 }
358
359 class HC3_Profiler_Benchmark
360 {
361 var $marker = array();
362
363 function mark($name)
364 {
365 $this->marker[$name] = microtime();
366 }
367
368 function elapsed_time($point1 = '', $point2 = '', $decimals = 4)
369 {
370 if ($point1 == ''){
371 return '{elapsed_time}';
372 }
373
374 if ( ! isset($this->marker[$point1])){
375 return '';
376 }
377
378 if ( ! isset($this->marker[$point2])){
379 $this->marker[$point2] = microtime();
380 }
381
382 list($sm, $ss) = explode(' ', $this->marker[$point1]);
383 list($em, $es) = explode(' ', $this->marker[$point2]);
384
385 return number_format(($em + $es) - ($sm + $ss), $decimals);
386 }
387
388 function memory_usage()
389 {
390 return '{memory_usage}';
391 }
392 }
393