blob: 3b189027f0cbacf78055558428c23340972f14c0 [file] [log] [blame]
Derek Allardc2f90e22007-04-03 11:17:44 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
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
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, 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 {
105 $output = "\n\n";
106 $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
107 $output .= "\n";
108 $output .= '<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').'&nbsp;&nbsp;</legend>';
109 $output .= "\n";
110
111 if ( ! class_exists('CI_DB_driver'))
112 {
113 $output .= "<div style='color:#0000FF;font-weight:normal;padding:4px 0 0 0;'>".$this->CI->lang->line('profiler_no_db')."</div>";
114 }
115 else
116 {
117 if (count($this->CI->db->queries) == 0)
118 {
119 $output .= "<div style='color:#0000FF;font-weight:normal;padding:4px 0 4px 0;'>".$this->CI->lang->line('profiler_no_queries')."</div>";
120 }
121 else
122 {
123 foreach ($this->CI->db->queries as $val)
124 {
125 $output .= '<div style="padding:3px;margin:12px 0 12px 0;background-color:#ddd;color:#000">';
Derek Jones9687c492007-07-11 21:43:23 +0000126 $output .= htmlspecialchars($val, ENT_QUOTES);
Derek Allardc2f90e22007-04-03 11:17:44 +0000127 $output .= "</div>\n";
128 }
129 }
130 }
131
132 $output .= "</fieldset>";
133
134 return $output;
135 }
136
137 // --------------------------------------------------------------------
138
139 /**
Derek Jonesd087ef82008-01-18 21:41:23 +0000140 * Compile $_GET Data
141 *
142 * @access private
143 * @return string
144 */
145 function _compile_get()
146 {
147 $output = "\n\n";
148 $output .= '<fieldset style="border:1px solid #cd6e00;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
149 $output .= "\n";
150 $output .= '<legend style="color:#cd6e00;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_get_data').'&nbsp;&nbsp;</legend>';
151 $output .= "\n";
152
153 if (count($_GET) == 0)
154 {
155 $output .= "<div style='color:#cd6e00;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_get')."</div>";
156 }
157 else
158 {
159 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
160
161 foreach ($_GET as $key => $val)
162 {
163 if ( ! is_numeric($key))
164 {
165 $key = "'".$key."'";
166 }
167
168 $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;'>";
169 if (is_array($val))
170 {
171 $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
172 }
173 else
174 {
175 $output .= htmlspecialchars(stripslashes($val));
176 }
177 $output .= "</td></tr>\n";
178 }
179
180 $output .= "</table>\n";
181 }
182 $output .= "</fieldset>";
183
184 return $output;
185 }
186
187 // --------------------------------------------------------------------
188
189 /**
Derek Allardc2f90e22007-04-03 11:17:44 +0000190 * Compile $_POST Data
191 *
192 * @access private
193 * @return string
194 */
195 function _compile_post()
196 {
197 $output = "\n\n";
198 $output .= '<fieldset style="border:1px solid #009900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
199 $output .= "\n";
200 $output .= '<legend style="color:#009900;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_post_data').'&nbsp;&nbsp;</legend>';
201 $output .= "\n";
202
203 if (count($_POST) == 0)
204 {
205 $output .= "<div style='color:#009900;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_post')."</div>";
206 }
207 else
208 {
209 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
210
211 foreach ($_POST as $key => $val)
212 {
213 if ( ! is_numeric($key))
214 {
215 $key = "'".$key."'";
216 }
217
218// $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;'>".htmlspecialchars(stripslashes($val))."</td></tr>\n";
Derek Jones9687c492007-07-11 21:43:23 +0000219 $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 +0000220 if (is_array($val))
221 {
222 $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
223 }
224 else
225 {
226 $output .= htmlspecialchars(stripslashes($val));
227 }
228 $output .= "</td></tr>\n";
229 }
230
231 $output .= "</table>\n";
232 }
233 $output .= "</fieldset>";
234
235 return $output;
236 }
237
238 // --------------------------------------------------------------------
239
240 /**
Derek Jonesd087ef82008-01-18 21:41:23 +0000241 * Show query string
242 *
243 * @access private
244 * @return string
245 */
246 function _compile_uri_string()
247 {
248 $output = "\n\n";
249 $output .= '<fieldset 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;'.$this->CI->lang->line('profiler_uri_string').'&nbsp;&nbsp;</legend>';
252 $output .= "\n";
253
254 if ($this->CI->uri->uri_string == '')
255 {
256 $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_uri')."</div>";
257 }
258 else
259 {
260 $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->uri->uri_string."</div>";
261 }
262
263 $output .= "</fieldset>";
264
265 return $output;
266 }
267
268 // --------------------------------------------------------------------
269
270 /**
271 * Compile memory usage
272 *
273 * Display total used memory
274 *
275 * @access public
276 * @return string
277 */
278 function _compile_memory_usage()
279 {
280 $output = "\n\n";
281 $output .= '<fieldset style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
282 $output .= "\n";
283 $output .= '<legend style="color:#000;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_memory_usage').'&nbsp;&nbsp;</legend>';
284 $output .= "\n";
285
286 if (function_exists('memory_get_usage') && ($usage = memory_get_usage()) != '')
287 {
288 $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".number_format($usage).' bytes</div>';
289 }
290 else
291 {
292 $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_memory_usage')."</div>";
293 }
294
295 $output .= "</fieldset>";
296
297 return $output;
298 }
299
300 // --------------------------------------------------------------------
301
302 /**
Derek Allardc2f90e22007-04-03 11:17:44 +0000303 * Run the Profiler
304 *
305 * @access private
306 * @return string
307 */
308 function run($output = '')
309 {
310 $output = '<br clear="all" />';
311 $output .= "<div style='background-color:#fff;padding:10px;'>";
Derek Jonesd087ef82008-01-18 21:41:23 +0000312
313 $output .= $this->_compile_memory_usage();
314 $output .= $this->_compile_benchmarks();
315 $output .= $this->_compile_uri_string();
316 $output .= $this->_compile_get();
Derek Allardc2f90e22007-04-03 11:17:44 +0000317 $output .= $this->_compile_post();
318 $output .= $this->_compile_queries();
319
320 $output .= '</div>';
321
322 return $output;
323 }
324
325}
326
327// END CI_Profiler class
admind13ff072006-10-03 03:40:45 +0000328?>