blob: 980cf09cc9e23cf8c2f77de80099e5594bec643b [file] [log] [blame]
Derek Jones0b59f272008-05-13 04:22:33 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allardc2f90e22007-04-03 11:17:44 +00002/**
Derek Allardd2df9bc2007-04-15 17:41:17 +00003 * CodeIgniter
Derek Allardc2f90e22007-04-03 11:17:44 +00004 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Rick Ellisd6778fb2008-10-01 01:34:52 +00009 * @copyright Copyright (c) 2008, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allardc2f90e22007-04-03 11:17:44 +000012 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
Derek Allardd2df9bc2007-04-15 17:41:17 +000019 * CodeIgniter Profiler Class
Derek Allardc2f90e22007-04-03 11:17:44 +000020 *
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
Derek Allard3d879d52008-01-18 19:41:32 +000030 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000031 * @link http://codeigniter.com/user_guide/general/profiling.html
Derek Allardc2f90e22007-04-03 11:17:44 +000032 */
33class CI_Profiler {
34
35 var $CI;
36
37 function CI_Profiler()
38 {
39 $this->CI =& get_instance();
40 $this->CI->load->language('profiler');
41 }
42
43 // --------------------------------------------------------------------
44
45 /**
46 * Auto Profiler
47 *
48 * This function cycles through the entire array of mark points and
49 * matches any two points that are named identically (ending in "_start"
50 * and "_end" respectively). It then compiles the execution times for
51 * all points and returns it as an array
52 *
53 * @access private
54 * @return array
55 */
56 function _compile_benchmarks()
57 {
58 $profile = array();
59 foreach ($this->CI->benchmark->marker as $key => $val)
60 {
61 // We match the "end" marker so that the list ends
62 // up in the order that it was defined
63 if (preg_match("/(.+?)_end/i", $key, $match))
64 {
65 if (isset($this->CI->benchmark->marker[$match[1].'_end']) AND isset($this->CI->benchmark->marker[$match[1].'_start']))
66 {
67 $profile[$match[1]] = $this->CI->benchmark->elapsed_time($match[1].'_start', $key);
68 }
69 }
70 }
71
72 // Build a table containing the profile data.
73 // Note: At some point we should turn this into a template that can
74 // be modified. We also might want to make this data available to be logged
75
76 $output = "\n\n";
77 $output .= '<fieldset style="border:1px solid #990000;padding:6px 10px 10px 10px;margin:0 0 20px 0;background-color:#eee">';
78 $output .= "\n";
79 $output .= '<legend style="color:#990000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_benchmarks').'&nbsp;&nbsp;</legend>';
80 $output .= "\n";
81 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
82
83 foreach ($profile as $key => $val)
84 {
85 $key = ucwords(str_replace(array('_', '-'), ' ', $key));
86 $output .= "<tr><td width='50%' style='color:#000;font-weight:bold;background-color:#ddd;'>".$key."&nbsp;&nbsp;</td><td width='50%' style='color:#990000;font-weight:normal;background-color:#ddd;'>".$val."</td></tr>\n";
87 }
88
89 $output .= "</table>\n";
90 $output .= "</fieldset>";
91
92 return $output;
93 }
94
95 // --------------------------------------------------------------------
96
97 /**
98 * Compile Queries
99 *
100 * @access private
101 * @return string
102 */
103 function _compile_queries()
104 {
Derek Jones0fcb7e62008-10-17 14:35:38 +0000105 $dbs = array();
106
Rick Ellis271e1992008-10-17 07:59:24 +0000107 // Let's determine which databases are currently connected to
108 foreach (get_object_vars($this->CI) as $CI_object)
Derek Allardc2f90e22007-04-03 11:17:44 +0000109 {
Rick Ellis271e1992008-10-17 07:59:24 +0000110 if ( is_subclass_of(get_class($CI_object), 'CI_DB') )
111 {
112 $dbs[] = $CI_object;
113 }
114 }
115
116 if (count($dbs) == 0)
117 {
118 $output = "\n\n";
119 $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
120 $output .= "\n";
Derek Jones56e9fa52008-01-23 17:26:37 +0000121 $output .= '<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').'&nbsp;&nbsp;</legend>';
122 $output .= "\n";
123 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
124 $output .="<tr><td width='100%' style='color:#0000FF;font-weight:normal;background-color:#eee;'>".$this->CI->lang->line('profiler_no_db')."</td></tr>\n";
Rick Ellis271e1992008-10-17 07:59:24 +0000125 $output .= "</table>\n";
126 $output .= "</fieldset>";
127
128 return $output;
129 }
130
131 $output = "\n\n";
132
133 foreach ($dbs as $db)
Derek Allardc2f90e22007-04-03 11:17:44 +0000134 {
Rick Ellis271e1992008-10-17 07:59:24 +0000135 $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
136 $output .= "\n";
137 $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>';
Derek Jones56e9fa52008-01-23 17:26:37 +0000138 $output .= "\n";
139 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
Rick Ellis271e1992008-10-17 07:59:24 +0000140
141 if (count($db->queries) == 0)
Derek Allardc2f90e22007-04-03 11:17:44 +0000142 {
Derek Jones56e9fa52008-01-23 17:26:37 +0000143 $output .= "<tr><td width='100%' style='color:#0000FF;font-weight:normal;background-color:#eee;'>".$this->CI->lang->line('profiler_no_queries')."</td></tr>\n";
Derek Allardc2f90e22007-04-03 11:17:44 +0000144 }
145 else
146 {
Derek Jonesb35c3f52008-02-08 20:48:23 +0000147 $highlight = array('SELECT', 'FROM', 'WHERE', 'AND', 'LEFT JOIN', 'ORDER BY', 'LIMIT', 'INSERT', 'INTO', 'VALUES', 'UPDATE', 'OR');
Derek Jones56e9fa52008-01-23 17:26:37 +0000148
Rick Ellis271e1992008-10-17 07:59:24 +0000149 foreach ($db->queries as $key => $val)
Derek Allardc2f90e22007-04-03 11:17:44 +0000150 {
Derek Jones56e9fa52008-01-23 17:26:37 +0000151 $val = htmlspecialchars($val, ENT_QUOTES);
Rick Ellis271e1992008-10-17 07:59:24 +0000152
153 $time = number_format($db->query_times[$key], 4);
Derek Jones56e9fa52008-01-23 17:26:37 +0000154
155 foreach ($highlight as $bold)
156 {
157 $val = str_replace($bold, '<strong>'.$bold.'</strong>', $val);
158 }
159
160 $output .= "<tr><td width='1%' valign='top' style='color:#990000;font-weight:normal;background-color:#ddd;'>".$time."&nbsp;&nbsp;</td><td style='color:#000;font-weight:normal;background-color:#ddd;'>".$val."</td></tr>\n";
161 }
Derek Allardc2f90e22007-04-03 11:17:44 +0000162 }
Rick Ellis271e1992008-10-17 07:59:24 +0000163
164 $output .= "</table>\n";
165 $output .= "</fieldset>";
166
Derek Allardc2f90e22007-04-03 11:17:44 +0000167 }
168
Derek Allardc2f90e22007-04-03 11:17:44 +0000169 return $output;
170 }
Derek Jones56e9fa52008-01-23 17:26:37 +0000171
Derek Allardc2f90e22007-04-03 11:17:44 +0000172
173 // --------------------------------------------------------------------
174
175 /**
Derek Jonesd087ef82008-01-18 21:41:23 +0000176 * Compile $_GET Data
177 *
178 * @access private
179 * @return string
180 */
181 function _compile_get()
182 {
183 $output = "\n\n";
184 $output .= '<fieldset style="border:1px solid #cd6e00;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
185 $output .= "\n";
186 $output .= '<legend style="color:#cd6e00;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_get_data').'&nbsp;&nbsp;</legend>';
187 $output .= "\n";
188
189 if (count($_GET) == 0)
190 {
191 $output .= "<div style='color:#cd6e00;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_get')."</div>";
192 }
193 else
194 {
195 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
196
197 foreach ($_GET as $key => $val)
198 {
Derek Jones0b59f272008-05-13 04:22:33 +0000199 if ( ! is_numeric($key))
Derek Jonesd087ef82008-01-18 21:41:23 +0000200 {
201 $key = "'".$key."'";
202 }
203
204 $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;'>";
205 if (is_array($val))
206 {
207 $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
208 }
209 else
210 {
211 $output .= htmlspecialchars(stripslashes($val));
212 }
213 $output .= "</td></tr>\n";
214 }
215
216 $output .= "</table>\n";
217 }
218 $output .= "</fieldset>";
219
220 return $output;
221 }
222
223 // --------------------------------------------------------------------
224
225 /**
Derek Allardc2f90e22007-04-03 11:17:44 +0000226 * Compile $_POST Data
227 *
228 * @access private
229 * @return string
230 */
231 function _compile_post()
232 {
233 $output = "\n\n";
234 $output .= '<fieldset style="border:1px solid #009900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
235 $output .= "\n";
236 $output .= '<legend style="color:#009900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_post_data').'&nbsp;&nbsp;</legend>';
237 $output .= "\n";
238
239 if (count($_POST) == 0)
240 {
241 $output .= "<div style='color:#009900;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_post')."</div>";
242 }
243 else
244 {
245 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
246
247 foreach ($_POST as $key => $val)
248 {
Derek Jones0b59f272008-05-13 04:22:33 +0000249 if ( ! is_numeric($key))
Derek Allardc2f90e22007-04-03 11:17:44 +0000250 {
251 $key = "'".$key."'";
252 }
253
Derek Jones9687c492007-07-11 21:43:23 +0000254 $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;'>";
Derek Allardc2f90e22007-04-03 11:17:44 +0000255 if (is_array($val))
256 {
257 $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
258 }
259 else
260 {
261 $output .= htmlspecialchars(stripslashes($val));
262 }
263 $output .= "</td></tr>\n";
264 }
265
266 $output .= "</table>\n";
267 }
268 $output .= "</fieldset>";
269
270 return $output;
271 }
272
273 // --------------------------------------------------------------------
274
275 /**
Derek Jonesd087ef82008-01-18 21:41:23 +0000276 * Show query string
277 *
278 * @access private
279 * @return string
280 */
281 function _compile_uri_string()
282 {
283 $output = "\n\n";
284 $output .= '<fieldset style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
285 $output .= "\n";
286 $output .= '<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_uri_string').'&nbsp;&nbsp;</legend>';
287 $output .= "\n";
288
289 if ($this->CI->uri->uri_string == '')
290 {
291 $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_uri')."</div>";
292 }
293 else
294 {
295 $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->uri->uri_string."</div>";
296 }
297
298 $output .= "</fieldset>";
299
300 return $output;
301 }
302
303 // --------------------------------------------------------------------
304
305 /**
Rick Ellised1f1ae2008-09-21 19:34:01 +0000306 * Show the controller and function that were called
307 *
308 * @access private
309 * @return string
310 */
311 function _compile_controller_info()
312 {
313 $output = "\n\n";
314 $output .= '<fieldset style="border:1px solid #995300;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
315 $output .= "\n";
316 $output .= '<legend style="color:#995300;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_controller_info').'&nbsp;&nbsp;</legend>';
317 $output .= "\n";
318
319 $output .= "<div style='color:#995300;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->router->fetch_class()."/".$this->CI->router->fetch_method()."</div>";
320
321
322 $output .= "</fieldset>";
323
324 return $output;
325 }
326 // --------------------------------------------------------------------
327
328 /**
Derek Jonesd087ef82008-01-18 21:41:23 +0000329 * Compile memory usage
330 *
331 * Display total used memory
332 *
333 * @access public
334 * @return string
335 */
336 function _compile_memory_usage()
337 {
338 $output = "\n\n";
Rick Ellised1f1ae2008-09-21 19:34:01 +0000339 $output .= '<fieldset style="border:1px solid #5a0099;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
Derek Jonesd087ef82008-01-18 21:41:23 +0000340 $output .= "\n";
Rick Ellised1f1ae2008-09-21 19:34:01 +0000341 $output .= '<legend style="color:#5a0099;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_memory_usage').'&nbsp;&nbsp;</legend>';
Derek Jonesd087ef82008-01-18 21:41:23 +0000342 $output .= "\n";
343
344 if (function_exists('memory_get_usage') && ($usage = memory_get_usage()) != '')
345 {
Rick Ellised1f1ae2008-09-21 19:34:01 +0000346 $output .= "<div style='color:#5a0099;font-weight:normal;padding:4px 0 4px 0'>".number_format($usage).' bytes</div>';
Derek Jonesd087ef82008-01-18 21:41:23 +0000347 }
348 else
349 {
Rick Ellised1f1ae2008-09-21 19:34:01 +0000350 $output .= "<div style='color:#5a0099;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_memory_usage')."</div>";
Derek Jonesd087ef82008-01-18 21:41:23 +0000351 }
352
353 $output .= "</fieldset>";
354
355 return $output;
356 }
357
358 // --------------------------------------------------------------------
359
360 /**
Derek Allardc2f90e22007-04-03 11:17:44 +0000361 * Run the Profiler
362 *
363 * @access private
364 * @return string
365 */
Derek Allard31438fe2008-01-29 01:07:06 +0000366 function run()
Derek Allardc2f90e22007-04-03 11:17:44 +0000367 {
Rick Ellised1f1ae2008-09-21 19:34:01 +0000368 $output = '<br clear="all" />';
369 $output .= "<div style='background-color:#fff;padding:10px;'>";
370
371 $output .= $this->_compile_uri_string();
372 $output .= $this->_compile_controller_info();
Derek Jonesd087ef82008-01-18 21:41:23 +0000373 $output .= $this->_compile_memory_usage();
374 $output .= $this->_compile_benchmarks();
Derek Jonesd087ef82008-01-18 21:41:23 +0000375 $output .= $this->_compile_get();
Derek Allardc2f90e22007-04-03 11:17:44 +0000376 $output .= $this->_compile_post();
377 $output .= $this->_compile_queries();
378
379 $output .= '</div>';
380
381 return $output;
382 }
383
384}
385
386// END CI_Profiler class
Derek Jones0b59f272008-05-13 04:22:33 +0000387
388/* End of file Profiler.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000389/* Location: ./system/libraries/Profiler.php */