blob: 49a6774c1873438064cc8975b5e06990cb94115b [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter Profiler Class
20 *
21 * This class enables you to display benchmark, query, and other data
22 * in order to help with debugging and optimization.
23 *
24 * Note: At some point it would be good to move all the HTML in this class
25 * into a set of template files in order to allow customization.
26 *
27 * @package CodeIgniter
28 * @subpackage Libraries
29 * @category Libraries
30 * @author ExpressionEngine Dev Team
31 * @link http://codeigniter.com/user_guide/general/profiling.html
32 */
33class CI_Profiler {
34
35 var $CI;
36
Derek Jonesee71c802010-03-10 10:05:05 -060037 var $_available_sections = array(
38 'benchmarks',
Derek Jonesee71c802010-03-10 10:05:05 -060039 'get',
Derek Jonesee71c802010-03-10 10:05:05 -060040 'memory_usage',
41 'post',
Greg Akere6026832010-04-29 13:41:39 -050042 'uri_string',
43 'controller_info',
Derek Jonesee71c802010-03-10 10:05:05 -060044 'queries',
Greg Akere6026832010-04-29 13:41:39 -050045 'http_headers',
46 'config'
Derek Jonesee71c802010-03-10 10:05:05 -060047 );
48
49 function CI_Profiler($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000050 {
51 $this->CI =& get_instance();
52 $this->CI->load->language('profiler');
Derek Jonesee71c802010-03-10 10:05:05 -060053
54 // default all sections to display
55 foreach ($this->_available_sections as $section)
56 {
57 if ( ! isset($config[$section]))
58 {
59 $this->_compile_{$section} = TRUE;
60 }
61 }
62
63 $this->set_sections($config);
Derek Allard2067d1a2008-11-13 22:59:24 +000064 }
65
66 // --------------------------------------------------------------------
67
68 /**
Derek Jonesee71c802010-03-10 10:05:05 -060069 * Set Sections
70 *
71 * Sets the private _compile_* properties to enable/disable Profiler sections
72 *
73 * @access public
74 * @param mixed
75 * @return void
76 */
77 function set_sections($config)
78 {
79 foreach ($config as $method => $enable)
80 {
81 if (in_array($method, $this->_available_sections))
82 {
83 $this->_compile_{$method} = ($enable !== FALSE) ? TRUE : FALSE;
84 }
85 }
86 }
87
88 // --------------------------------------------------------------------
89
90 /**
Derek Allard2067d1a2008-11-13 22:59:24 +000091 * Auto Profiler
92 *
93 * This function cycles through the entire array of mark points and
94 * matches any two points that are named identically (ending in "_start"
95 * and "_end" respectively). It then compiles the execution times for
96 * all points and returns it as an array
Derek Jonesee71c802010-03-10 10:05:05 -060097 * @PHP4 - all methods should be declared private
Derek Allard2067d1a2008-11-13 22:59:24 +000098 * @access private
99 * @return array
100 */
101 function _compile_benchmarks()
102 {
103 $profile = array();
104 foreach ($this->CI->benchmark->marker as $key => $val)
105 {
106 // We match the "end" marker so that the list ends
107 // up in the order that it was defined
108 if (preg_match("/(.+?)_end/i", $key, $match))
109 {
110 if (isset($this->CI->benchmark->marker[$match[1].'_end']) AND isset($this->CI->benchmark->marker[$match[1].'_start']))
111 {
112 $profile[$match[1]] = $this->CI->benchmark->elapsed_time($match[1].'_start', $key);
113 }
114 }
115 }
116
117 // Build a table containing the profile data.
118 // Note: At some point we should turn this into a template that can
119 // be modified. We also might want to make this data available to be logged
120
121 $output = "\n\n";
Derek Allard76af4092010-01-16 19:20:49 +0000122 $output .= '<fieldset style="border:1px solid #900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 $output .= "\n";
Derek Allard76af4092010-01-16 19:20:49 +0000124 $output .= '<legend style="color:#900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_benchmarks').'&nbsp;&nbsp;</legend>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 $output .= "\n";
126 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
127
128 foreach ($profile as $key => $val)
129 {
130 $key = ucwords(str_replace(array('_', '-'), ' ', $key));
Derek Allard76af4092010-01-16 19:20:49 +0000131 $output .= "<tr><td width='50%' style='color:#000;font-weight:bold;background-color:#ddd;'>".$key."&nbsp;&nbsp;</td><td width='50%' style='color:#900;font-weight:normal;background-color:#ddd;'>".$val."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 }
133
134 $output .= "</table>\n";
135 $output .= "</fieldset>";
136
137 return $output;
138 }
139
140 // --------------------------------------------------------------------
141
142 /**
143 * Compile Queries
144 *
145 * @access private
146 * @return string
147 */
148 function _compile_queries()
149 {
150 $dbs = array();
Derek Jonesf0a9b332009-07-29 14:19:18 +0000151
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 // Let's determine which databases are currently connected to
153 foreach (get_object_vars($this->CI) as $CI_object)
154 {
Derek Jonesf0a9b332009-07-29 14:19:18 +0000155 if (is_object($CI_object) && is_subclass_of(get_class($CI_object), 'CI_DB') )
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 {
157 $dbs[] = $CI_object;
158 }
159 }
160
161 if (count($dbs) == 0)
162 {
163 $output = "\n\n";
164 $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
165 $output .= "\n";
166 $output .= '<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').'&nbsp;&nbsp;</legend>';
167 $output .= "\n";
168 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
169 $output .="<tr><td width='100%' style='color:#0000FF;font-weight:normal;background-color:#eee;'>".$this->CI->lang->line('profiler_no_db')."</td></tr>\n";
170 $output .= "</table>\n";
171 $output .= "</fieldset>";
172
173 return $output;
174 }
175
176 // Load the text helper so we can highlight the SQL
177 $this->CI->load->helper('text');
178
179 // Key words we want bolded
180 $highlight = array('SELECT', 'DISTINCT', 'FROM', 'WHERE', 'AND', 'LEFT&nbsp;JOIN', 'ORDER&nbsp;BY', 'GROUP&nbsp;BY', 'LIMIT', 'INSERT', 'INTO', 'VALUES', 'UPDATE', 'OR', 'HAVING', 'OFFSET', 'NOT&nbsp;IN', 'IN', 'LIKE', 'NOT&nbsp;LIKE', 'COUNT', 'MAX', 'MIN', 'ON', 'AS', 'AVG', 'SUM', '(', ')');
181
182 $output = "\n\n";
183
184 foreach ($dbs as $db)
185 {
186 $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
187 $output .= "\n";
188 $output .= '<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_database').':&nbsp; '.$db->database.'&nbsp;&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').': '.count($this->CI->db->queries).'&nbsp;&nbsp;&nbsp;</legend>';
189 $output .= "\n";
190 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
191
192 if (count($db->queries) == 0)
193 {
194 $output .= "<tr><td width='100%' style='color:#0000FF;font-weight:normal;background-color:#eee;'>".$this->CI->lang->line('profiler_no_queries')."</td></tr>\n";
195 }
196 else
197 {
198 foreach ($db->queries as $key => $val)
199 {
200 $time = number_format($db->query_times[$key], 4);
201
202 $val = highlight_code($val, ENT_QUOTES);
203
204 foreach ($highlight as $bold)
205 {
206 $val = str_replace($bold, '<strong>'.$bold.'</strong>', $val);
207 }
208
Derek Allard76af4092010-01-16 19:20:49 +0000209 $output .= "<tr><td width='1%' valign='top' style='color:#900;font-weight:normal;background-color:#ddd;'>".$time."&nbsp;&nbsp;</td><td style='color:#000;font-weight:normal;background-color:#ddd;'>".$val."</td></tr>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 }
211 }
212
213 $output .= "</table>\n";
214 $output .= "</fieldset>";
215
216 }
217
218 return $output;
219 }
220
221
222 // --------------------------------------------------------------------
223
224 /**
225 * Compile $_GET Data
226 *
227 * @access private
228 * @return string
229 */
230 function _compile_get()
231 {
232 $output = "\n\n";
233 $output .= '<fieldset style="border:1px solid #cd6e00;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
234 $output .= "\n";
235 $output .= '<legend style="color:#cd6e00;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_get_data').'&nbsp;&nbsp;</legend>';
236 $output .= "\n";
237
238 if (count($_GET) == 0)
239 {
240 $output .= "<div style='color:#cd6e00;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_get')."</div>";
241 }
242 else
243 {
244 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
245
246 foreach ($_GET as $key => $val)
247 {
248 if ( ! is_numeric($key))
249 {
250 $key = "'".$key."'";
251 }
252
253 $output .= "<tr><td width='50%' style='color:#000;background-color:#ddd;'>&#36;_GET[".$key."]&nbsp;&nbsp; </td><td width='50%' style='color:#cd6e00;font-weight:normal;background-color:#ddd;'>";
254 if (is_array($val))
255 {
256 $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
257 }
258 else
259 {
260 $output .= htmlspecialchars(stripslashes($val));
261 }
262 $output .= "</td></tr>\n";
263 }
264
265 $output .= "</table>\n";
266 }
267 $output .= "</fieldset>";
268
269 return $output;
270 }
271
272 // --------------------------------------------------------------------
273
274 /**
275 * Compile $_POST Data
276 *
277 * @access private
278 * @return string
279 */
280 function _compile_post()
281 {
282 $output = "\n\n";
283 $output .= '<fieldset style="border:1px solid #009900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
284 $output .= "\n";
285 $output .= '<legend style="color:#009900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_post_data').'&nbsp;&nbsp;</legend>';
286 $output .= "\n";
287
288 if (count($_POST) == 0)
289 {
290 $output .= "<div style='color:#009900;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_post')."</div>";
291 }
292 else
293 {
294 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
295
296 foreach ($_POST as $key => $val)
297 {
298 if ( ! is_numeric($key))
299 {
300 $key = "'".$key."'";
301 }
302
303 $output .= "<tr><td width='50%' style='color:#000;background-color:#ddd;'>&#36;_POST[".$key."]&nbsp;&nbsp; </td><td width='50%' style='color:#009900;font-weight:normal;background-color:#ddd;'>";
304 if (is_array($val))
305 {
306 $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
307 }
308 else
309 {
310 $output .= htmlspecialchars(stripslashes($val));
311 }
312 $output .= "</td></tr>\n";
313 }
314
315 $output .= "</table>\n";
316 }
317 $output .= "</fieldset>";
318
319 return $output;
320 }
321
322 // --------------------------------------------------------------------
323
324 /**
325 * Show query string
326 *
327 * @access private
328 * @return string
329 */
330 function _compile_uri_string()
331 {
332 $output = "\n\n";
333 $output .= '<fieldset style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
334 $output .= "\n";
335 $output .= '<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_uri_string').'&nbsp;&nbsp;</legend>';
336 $output .= "\n";
337
338 if ($this->CI->uri->uri_string == '')
339 {
340 $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_uri')."</div>";
341 }
342 else
343 {
344 $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->uri->uri_string."</div>";
345 }
346
347 $output .= "</fieldset>";
348
349 return $output;
350 }
351
352 // --------------------------------------------------------------------
353
354 /**
355 * Show the controller and function that were called
356 *
357 * @access private
358 * @return string
359 */
360 function _compile_controller_info()
361 {
362 $output = "\n\n";
363 $output .= '<fieldset style="border:1px solid #995300;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
364 $output .= "\n";
365 $output .= '<legend style="color:#995300;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_controller_info').'&nbsp;&nbsp;</legend>';
366 $output .= "\n";
367
368 $output .= "<div style='color:#995300;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->router->fetch_class()."/".$this->CI->router->fetch_method()."</div>";
369
370
371 $output .= "</fieldset>";
372
373 return $output;
374 }
Derek Allard76af4092010-01-16 19:20:49 +0000375
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 // --------------------------------------------------------------------
377
378 /**
379 * Compile memory usage
380 *
381 * Display total used memory
382 *
383 * @access public
384 * @return string
385 */
386 function _compile_memory_usage()
387 {
388 $output = "\n\n";
389 $output .= '<fieldset style="border:1px solid #5a0099;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
390 $output .= "\n";
391 $output .= '<legend style="color:#5a0099;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_memory_usage').'&nbsp;&nbsp;</legend>';
392 $output .= "\n";
393
394 if (function_exists('memory_get_usage') && ($usage = memory_get_usage()) != '')
395 {
396 $output .= "<div style='color:#5a0099;font-weight:normal;padding:4px 0 4px 0'>".number_format($usage).' bytes</div>';
397 }
398 else
399 {
Derek Allard76af4092010-01-16 19:20:49 +0000400 $output .= "<div style='color:#5a0099;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_memory_usage')."</div>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 }
402
403 $output .= "</fieldset>";
404
405 return $output;
406 }
407
408 // --------------------------------------------------------------------
409
410 /**
Derek Allard76af4092010-01-16 19:20:49 +0000411 * Compile header information
412 *
413 * Lists HTTP headers
414 *
415 * @access public
416 * @return string
417 */
418 function _compile_http_headers()
419 {
420 $output = "\n\n";
421 $output .= '<fieldset style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
422 $output .= "\n";
423 $output .= '<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_headers').'&nbsp;&nbsp;</legend>';
424 $output .= "\n";
425
426 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
427
Derek Jones079303e2010-03-02 18:16:23 -0600428 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)
Derek Allard76af4092010-01-16 19:20:49 +0000429 {
430 $val = (isset($_SERVER[$header])) ? $_SERVER[$header] : '';
431 $output .= "<tr><td valign='top' style='color:#900;background-color:#ddd;'>".$header."&nbsp;&nbsp;</td><td style='color:#000;background-color:#ddd;'>".$val."</td></tr>\n";
432 }
433
434 $output .= "</table>\n";
435 $output .= "</fieldset>";
436
Derek Allard76af4092010-01-16 19:20:49 +0000437 return $output;
438 }
439
440 // --------------------------------------------------------------------
441
442 /**
443 * Compile config information
444 *
445 * Lists developer config variables
446 *
447 * @access public
448 * @return string
449 */
450 function _compile_config()
451 {
452 $output = "\n\n";
453 $output .= '<fieldset style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
454 $output .= "\n";
455 $output .= '<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_config').'&nbsp;&nbsp;</legend>';
456 $output .= "\n";
457
458 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
459
460 foreach($this->CI->config->config as $config=>$val)
461 {
Greg Akere6026832010-04-29 13:41:39 -0500462 if (is_array($val))
463 {
464 $val = print_r($val, TRUE);
465 }
466
Derek Allard76af4092010-01-16 19:20:49 +0000467 $output .= "<tr><td valign='top' style='color:#900;background-color:#ddd;'>".$config."&nbsp;&nbsp;</td><td style='color:#000;background-color:#ddd;'>".$val."</td></tr>\n";
468 }
469
470 $output .= "</table>\n";
471 $output .= "</fieldset>";
472
Derek Allard76af4092010-01-16 19:20:49 +0000473 return $output;
474 }
475
476 // --------------------------------------------------------------------
477
478 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 * Run the Profiler
480 *
481 * @access private
482 * @return string
483 */
484 function run()
485 {
486 $output = "<div id='codeigniter_profiler' style='clear:both;background-color:#fff;padding:10px;'>";
Derek Jonesee71c802010-03-10 10:05:05 -0600487 $fields_displayed = 0;
488
489 foreach ($this->_available_sections as $section)
490 {
491 if ($this->_compile_{$section} !== FALSE)
492 {
493 $func = "_compile_{$section}";
494 $output .= $this->{$func}();
495 $fields_displayed++;
496 }
497 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000498
Derek Jonesee71c802010-03-10 10:05:05 -0600499 if ($fields_displayed == 0)
500 {
501 $output .= '<p style="border:1px solid #5a0099;padding:10px;margin:20px 0;background-color:#eee">'.$this->CI->lang->line('profiler_no_profiles').'</p>';
502 }
503
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 $output .= '</div>';
505
506 return $output;
507 }
508
509}
510
511// END CI_Profiler class
512
513/* End of file Profiler.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000514/* Location: ./system/libraries/Profiler.php */