1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
\r
5 * An open source application development framework for PHP 4.3.2 or newer
\r
7 * @package CodeIgniter
\r
8 * @author ExpressionEngine Dev Team
\r
9 * @copyright Copyright (c) 2008, EllisLab, Inc.
\r
10 * @license http://codeigniter.com/user_guide/license.html
\r
11 * @link http://codeigniter.com
\r
12 * @since Version 1.0
\r
16 // ------------------------------------------------------------------------
\r
19 * CodeIgniter Profiler Class
\r
21 * This class enables you to display benchmark, query, and other data
\r
22 * in order to help with debugging and optimization.
\r
24 * Note: At some point it would be good to move all the HTML in this class
\r
25 * into a set of template files in order to allow customization.
\r
27 * @package CodeIgniter
\r
28 * @subpackage Libraries
\r
29 * @category Libraries
\r
30 * @author ExpressionEngine Dev Team
\r
31 * @link http://codeigniter.com/user_guide/general/profiling.html
\r
37 function CI_Profiler()
\r
39 $this->CI =& get_instance();
\r
40 $this->CI->load->language('profiler');
\r
43 // --------------------------------------------------------------------
\r
48 * This function cycles through the entire array of mark points and
\r
49 * matches any two points that are named identically (ending in "_start"
\r
50 * and "_end" respectively). It then compiles the execution times for
\r
51 * all points and returns it as an array
\r
56 function _compile_benchmarks()
\r
59 foreach ($this->CI->benchmark->marker as $key => $val)
\r
61 // We match the "end" marker so that the list ends
\r
62 // up in the order that it was defined
\r
63 if (preg_match("/(.+?)_end/i", $key, $match))
\r
65 if (isset($this->CI->benchmark->marker[$match[1].'_end']) AND isset($this->CI->benchmark->marker[$match[1].'_start']))
\r
67 $profile[$match[1]] = $this->CI->benchmark->elapsed_time($match[1].'_start', $key);
\r
72 // Build a table containing the profile data.
\r
73 // Note: At some point we should turn this into a template that can
\r
74 // be modified. We also might want to make this data available to be logged
\r
77 $output .= '<fieldset style="border:1px solid #990000;padding:6px 10px 10px 10px;margin:0 0 20px 0;background-color:#eee">';
\r
79 $output .= '<legend style="color:#990000;"> '.$this->CI->lang->line('profiler_benchmarks').' </legend>';
\r
81 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
\r
83 foreach ($profile as $key => $val)
\r
85 $key = ucwords(str_replace(array('_', '-'), ' ', $key));
\r
86 $output .= "<tr><td width='50%' style='color:#000;font-weight:bold;background-color:#ddd;'>".$key." </td><td width='50%' style='color:#990000;font-weight:normal;background-color:#ddd;'>".$val."</td></tr>\n";
\r
89 $output .= "</table>\n";
\r
90 $output .= "</fieldset>";
\r
95 // --------------------------------------------------------------------
\r
103 function _compile_queries()
\r
107 // Let's determine which databases are currently connected to
\r
108 foreach (get_object_vars($this->CI) as $CI_object)
\r
110 if ( is_subclass_of(get_class($CI_object), 'CI_DB') )
\r
112 $dbs[] = $CI_object;
\r
116 if (count($dbs) == 0)
\r
119 $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
\r
121 $output .= '<legend style="color:#0000FF;"> '.$this->CI->lang->line('profiler_queries').' </legend>';
\r
123 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
\r
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";
\r
125 $output .= "</table>\n";
\r
126 $output .= "</fieldset>";
\r
131 // Load the text helper so we can highlight the SQL
\r
132 $this->CI->load->helper('text');
\r
134 // Key words we want bolded
\r
135 $highlight = array('SELECT', 'DISTINCT', 'FROM', 'WHERE', 'AND', 'LEFT JOIN', 'ORDER BY', 'GROUP BY', 'LIMIT', 'INSERT', 'INTO', 'VALUES', 'UPDATE', 'OR', 'HAVING', 'OFFSET', 'NOT IN', 'IN', 'LIKE', 'NOT LIKE', 'COUNT', 'MAX', 'MIN', 'ON', 'AS', 'AVG', 'SUM', '(', ')');
\r
139 foreach ($dbs as $db)
\r
141 $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
\r
143 $output .= '<legend style="color:#0000FF;"> '.$this->CI->lang->line('profiler_database').': '.$db->database.' '.$this->CI->lang->line('profiler_queries').': '.count($this->CI->db->queries).' </legend>';
\r
145 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
\r
147 if (count($db->queries) == 0)
\r
149 $output .= "<tr><td width='100%' style='color:#0000FF;font-weight:normal;background-color:#eee;'>".$this->CI->lang->line('profiler_no_queries')."</td></tr>\n";
\r
153 foreach ($db->queries as $key => $val)
\r
155 $time = number_format($db->query_times[$key], 4);
\r
157 $val = highlight_code($val, ENT_QUOTES);
\r
159 foreach ($highlight as $bold)
\r
161 $val = str_replace($bold, '<strong>'.$bold.'</strong>', $val);
\r
164 $output .= "<tr><td width='1%' valign='top' style='color:#990000;font-weight:normal;background-color:#ddd;'>".$time." </td><td style='color:#000;font-weight:normal;background-color:#ddd;'>".$val."</td></tr>\n";
\r
168 $output .= "</table>\n";
\r
169 $output .= "</fieldset>";
\r
177 // --------------------------------------------------------------------
\r
180 * Compile $_GET Data
\r
185 function _compile_get()
\r
188 $output .= '<fieldset style="border:1px solid #cd6e00;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
\r
190 $output .= '<legend style="color:#cd6e00;"> '.$this->CI->lang->line('profiler_get_data').' </legend>';
\r
193 if (count($_GET) == 0)
\r
195 $output .= "<div style='color:#cd6e00;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_get')."</div>";
\r
199 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
\r
201 foreach ($_GET as $key => $val)
\r
203 if ( ! is_numeric($key))
\r
205 $key = "'".$key."'";
\r
208 $output .= "<tr><td width='50%' style='color:#000;background-color:#ddd;'>$_GET[".$key."] </td><td width='50%' style='color:#cd6e00;font-weight:normal;background-color:#ddd;'>";
\r
209 if (is_array($val))
\r
211 $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
\r
215 $output .= htmlspecialchars(stripslashes($val));
\r
217 $output .= "</td></tr>\n";
\r
220 $output .= "</table>\n";
\r
222 $output .= "</fieldset>";
\r
227 // --------------------------------------------------------------------
\r
230 * Compile $_POST Data
\r
235 function _compile_post()
\r
238 $output .= '<fieldset style="border:1px solid #009900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
\r
240 $output .= '<legend style="color:#009900;"> '.$this->CI->lang->line('profiler_post_data').' </legend>';
\r
243 if (count($_POST) == 0)
\r
245 $output .= "<div style='color:#009900;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_post')."</div>";
\r
249 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
\r
251 foreach ($_POST as $key => $val)
\r
253 if ( ! is_numeric($key))
\r
255 $key = "'".$key."'";
\r
258 $output .= "<tr><td width='50%' style='color:#000;background-color:#ddd;'>$_POST[".$key."] </td><td width='50%' style='color:#009900;font-weight:normal;background-color:#ddd;'>";
\r
259 if (is_array($val))
\r
261 $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
\r
265 $output .= htmlspecialchars(stripslashes($val));
\r
267 $output .= "</td></tr>\n";
\r
270 $output .= "</table>\n";
\r
272 $output .= "</fieldset>";
\r
277 // --------------------------------------------------------------------
\r
280 * Show query string
\r
285 function _compile_uri_string()
\r
288 $output .= '<fieldset style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
\r
290 $output .= '<legend style="color:#000;"> '.$this->CI->lang->line('profiler_uri_string').' </legend>';
\r
293 if ($this->CI->uri->uri_string == '')
\r
295 $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_uri')."</div>";
\r
299 $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->uri->uri_string."</div>";
\r
302 $output .= "</fieldset>";
\r
307 // --------------------------------------------------------------------
\r
310 * Show the controller and function that were called
\r
315 function _compile_controller_info()
\r
318 $output .= '<fieldset style="border:1px solid #995300;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
\r
320 $output .= '<legend style="color:#995300;"> '.$this->CI->lang->line('profiler_controller_info').' </legend>';
\r
323 $output .= "<div style='color:#995300;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->router->fetch_class()."/".$this->CI->router->fetch_method()."</div>";
\r
326 $output .= "</fieldset>";
\r
330 // --------------------------------------------------------------------
\r
333 * Compile memory usage
\r
335 * Display total used memory
\r
340 function _compile_memory_usage()
\r
343 $output .= '<fieldset style="border:1px solid #5a0099;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
\r
345 $output .= '<legend style="color:#5a0099;"> '.$this->CI->lang->line('profiler_memory_usage').' </legend>';
\r
348 if (function_exists('memory_get_usage') && ($usage = memory_get_usage()) != '')
\r
350 $output .= "<div style='color:#5a0099;font-weight:normal;padding:4px 0 4px 0'>".number_format($usage).' bytes</div>';
\r
354 $output .= "<div style='color:#5a0099;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_memory_usage')."</div>";
\r
357 $output .= "</fieldset>";
\r
362 // --------------------------------------------------------------------
\r
372 $output = "<div id='codeigniter_profiler' style='clear:both;background-color:#fff;padding:10px;'>";
\r
374 $output .= $this->_compile_uri_string();
\r
375 $output .= $this->_compile_controller_info();
\r
376 $output .= $this->_compile_memory_usage();
\r
377 $output .= $this->_compile_benchmarks();
\r
378 $output .= $this->_compile_get();
\r
379 $output .= $this->_compile_post();
\r
380 $output .= $this->_compile_queries();
\r
382 $output .= '</div>';
\r
389 // END CI_Profiler class
\r
391 /* End of file Profiler.php */
\r
392 /* Location: ./system/libraries/Profiler.php */