1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 4.3.2 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
19 * CodeIgniter Profiler Class
21 * This class enables you to display benchmark, query, and other data
22 * in order to help with debugging and optimization.
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.
27 * @package CodeIgniter
28 * @subpackage Libraries
30 * @author ExpressionEngine Dev Team
31 * @link http://codeigniter.com/user_guide/general/profiling.html
37 function CI_Profiler()
39 $this->CI =& get_instance();
40 $this->CI->load->language('profiler');
43 // --------------------------------------------------------------------
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
56 function _compile_benchmarks()
59 foreach ($this->CI->benchmark->marker as $key => $val)
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))
65 if (isset($this->CI->benchmark->marker[$match[1].'_end']) AND isset($this->CI->benchmark->marker[$match[1].'_start']))
67 $profile[$match[1]] = $this->CI->benchmark->elapsed_time($match[1].'_start', $key);
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
77 $output .= '<fieldset style="border:1px solid #990000;padding:6px 10px 10px 10px;margin:0 0 20px 0;background-color:#eee">';
79 $output .= '<legend style="color:#990000;"> '.$this->CI->lang->line('profiler_benchmarks').' </legend>';
81 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
83 foreach ($profile as $key => $val)
85 $key = ucwords(str_replace(array('_', '-'), ' ', $key));
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";
89 $output .= "</table>\n";
90 $output .= "</fieldset>";
95 // --------------------------------------------------------------------
103 function _compile_queries()
107 // Let's determine which databases are currently connected to
108 foreach (get_object_vars($this->CI) as $CI_object)
110 if (is_object($CI_object) && is_subclass_of(get_class($CI_object), 'CI_DB') )
116 if (count($dbs) == 0)
119 $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
121 $output .= '<legend style="color:#0000FF;"> '.$this->CI->lang->line('profiler_queries').' </legend>';
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";
125 $output .= "</table>\n";
126 $output .= "</fieldset>";
131 // Load the text helper so we can highlight the SQL
132 $this->CI->load->helper('text');
134 // Key words we want bolded
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', '(', ')');
139 foreach ($dbs as $db)
141 $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
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>';
145 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
147 if (count($db->queries) == 0)
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";
153 foreach ($db->queries as $key => $val)
155 $time = number_format($db->query_times[$key], 4);
157 $val = highlight_code($val, ENT_QUOTES);
159 foreach ($highlight as $bold)
161 $val = str_replace($bold, '<strong>'.$bold.'</strong>', $val);
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";
168 $output .= "</table>\n";
169 $output .= "</fieldset>";
177 // --------------------------------------------------------------------
185 function _compile_get()
188 $output .= '<fieldset style="border:1px solid #cd6e00;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
190 $output .= '<legend style="color:#cd6e00;"> '.$this->CI->lang->line('profiler_get_data').' </legend>';
193 if (count($_GET) == 0)
195 $output .= "<div style='color:#cd6e00;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_get')."</div>";
199 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
201 foreach ($_GET as $key => $val)
203 if ( ! is_numeric($key))
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;'>";
211 $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
215 $output .= htmlspecialchars(stripslashes($val));
217 $output .= "</td></tr>\n";
220 $output .= "</table>\n";
222 $output .= "</fieldset>";
227 // --------------------------------------------------------------------
230 * Compile $_POST Data
235 function _compile_post()
238 $output .= '<fieldset style="border:1px solid #009900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
240 $output .= '<legend style="color:#009900;"> '.$this->CI->lang->line('profiler_post_data').' </legend>';
243 if (count($_POST) == 0)
245 $output .= "<div style='color:#009900;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_post')."</div>";
249 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
251 foreach ($_POST as $key => $val)
253 if ( ! is_numeric($key))
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;'>";
261 $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
265 $output .= htmlspecialchars(stripslashes($val));
267 $output .= "</td></tr>\n";
270 $output .= "</table>\n";
272 $output .= "</fieldset>";
277 // --------------------------------------------------------------------
285 function _compile_uri_string()
288 $output .= '<fieldset style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
290 $output .= '<legend style="color:#000;"> '.$this->CI->lang->line('profiler_uri_string').' </legend>';
293 if ($this->CI->uri->uri_string == '')
295 $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_uri')."</div>";
299 $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->uri->uri_string."</div>";
302 $output .= "</fieldset>";
307 // --------------------------------------------------------------------
310 * Show the controller and function that were called
315 function _compile_controller_info()
318 $output .= '<fieldset style="border:1px solid #995300;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
320 $output .= '<legend style="color:#995300;"> '.$this->CI->lang->line('profiler_controller_info').' </legend>';
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>";
326 $output .= "</fieldset>";
330 // --------------------------------------------------------------------
333 * Compile memory usage
335 * Display total used memory
340 function _compile_memory_usage()
343 $output .= '<fieldset style="border:1px solid #5a0099;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
345 $output .= '<legend style="color:#5a0099;"> '.$this->CI->lang->line('profiler_memory_usage').' </legend>';
348 if (function_exists('memory_get_usage') && ($usage = memory_get_usage()) != '')
350 $output .= "<div style='color:#5a0099;font-weight:normal;padding:4px 0 4px 0'>".number_format($usage).' bytes</div>';
354 $output .= "<div style='color:#5a0099;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_memory_usage')."</div>";
357 $output .= "</fieldset>";
362 // --------------------------------------------------------------------
372 $output = "<div id='codeigniter_profiler' style='clear:both;background-color:#fff;padding:10px;'>";
374 $output .= $this->_compile_uri_string();
375 $output .= $this->_compile_controller_info();
376 $output .= $this->_compile_memory_usage();
377 $output .= $this->_compile_benchmarks();
378 $output .= $this->_compile_get();
379 $output .= $this->_compile_post();
380 $output .= $this->_compile_queries();
389 // END CI_Profiler class
391 /* End of file Profiler.php */
392 /* Location: ./system/libraries/Profiler.php */