]>
jfr.im git - z_archive/KronOS.git/blob - video/system/libraries/Profiler.php
882a82c1fa5312959fb01f83e1da7b79594db1a1
1 <?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 5.1.6 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2011, 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
35 protected $_available_sections = array(
48 protected $_query_toggle_count = 25;
52 // --------------------------------------------------------------------
54 public function __construct($config = array())
56 $this->CI
=& get_instance();
57 $this->CI
->load
->language('profiler');
59 if (isset($config['query_toggle_count']))
61 $this->_query_toggle_count
= (int) $config['query_toggle_count'];
62 unset($config['query_toggle_count']);
65 // default all sections to display
66 foreach ($this->_available_sections
as $section)
68 if ( ! isset($config[$section]))
70 $this->_compile_{$section
} = TRUE;
74 $this->set_sections($config);
77 // --------------------------------------------------------------------
82 * Sets the private _compile_* properties to enable/disable Profiler sections
87 public function set_sections($config)
89 foreach ($config as $method => $enable)
91 if (in_array($method, $this->_available_sections
))
93 $this->_compile_{$method
} = ($enable !== FALSE) ? TRUE : FALSE;
98 // --------------------------------------------------------------------
103 * This function cycles through the entire array of mark points and
104 * matches any two points that are named identically (ending in "_start"
105 * and "_end" respectively). It then compiles the execution times for
106 * all points and returns it as an array
110 protected function _compile_benchmarks()
113 foreach ($this->CI
->benchmark
->marker
as $key => $val)
115 // We match the "end" marker so that the list ends
116 // up in the order that it was defined
117 if (preg_match("/(.+?)_end/i", $key, $match))
119 if (isset($this->CI
->benchmark
->marker
[$match[1].'_end']) AND isset($this->CI
->benchmark
->marker
[$match[1].'_start']))
121 $profile[$match[1]] = $this->CI
->benchmark
->elapsed_time($match[1].'_start', $key);
126 // Build a table containing the profile data.
127 // Note: At some point we should turn this into a template that can
128 // be modified. We also might want to make this data available to be logged
131 $output .= '<fieldset id="ci_profiler_benchmarks" style="border:1px solid #900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
133 $output .= '<legend style="color:#900;"> '.$this->CI
->lang
->line('profiler_benchmarks').' </legend>';
135 $output .= "\n\n<table style='width:100%'>\n";
137 foreach ($profile as $key => $val)
139 $key = ucwords(str_replace(array('_', '-'), ' ', $key));
140 $output .= "<tr><td style='padding:5px;width:50%;color:#000;font-weight:bold;background-color:#ddd;'>".$key." </td><td style='padding:5px;width:50%;color:#900;font-weight:normal;background-color:#ddd;'>".$val."</td></tr>\n";
143 $output .= "</table>\n";
144 $output .= "</fieldset>";
149 // --------------------------------------------------------------------
156 protected function _compile_queries()
160 // Let's determine which databases are currently connected to
161 foreach (get_object_vars($this->CI
) as $CI_object)
163 if (is_object($CI_object) && is_subclass_of(get_class($CI_object), 'CI_DB') )
169 if (count($dbs) == 0)
172 $output .= '<fieldset id="ci_profiler_queries" style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
174 $output .= '<legend style="color:#0000FF;"> '.$this->CI
->lang
->line('profiler_queries').' </legend>';
176 $output .= "\n\n<table style='border:none; width:100%;'>\n";
177 $output .="<tr><td style='width:100%;color:#0000FF;font-weight:normal;background-color:#eee;padding:5px'>".$this->CI
->lang
->line('profiler_no_db')."</td></tr>\n";
178 $output .= "</table>\n";
179 $output .= "</fieldset>";
184 // Load the text helper so we can highlight the SQL
185 $this->CI
->load
->helper('text');
187 // Key words we want bolded
188 $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', '(', ')');
194 foreach ($dbs as $db)
198 $hide_queries = (count($db->queries
) > $this->_query_toggle_count
) ? ' display:none' : '';
200 $show_hide_js = '(<span style="cursor: pointer;" onclick="var s=document.getElementById(\'ci_profiler_queries_db_'.$count.'\').style;s.display=s.display==\'none\'?\'\':\'none\';this.innerHTML=this.innerHTML==\''.$this->CI
->lang
->line('profiler_section_hide').'\'?\''.$this->CI
->lang
->line('profiler_section_show').'\':\''.$this->CI
->lang
->line('profiler_section_hide').'\';">'.$this->CI
->lang
->line('profiler_section_hide').'</span>)';
202 if ($hide_queries != '')
204 $show_hide_js = '(<span style="cursor: pointer;" onclick="var s=document.getElementById(\'ci_profiler_queries_db_'.$count.'\').style;s.display=s.display==\'none\'?\'\':\'none\';this.innerHTML=this.innerHTML==\''.$this->CI
->lang
->line('profiler_section_show').'\'?\''.$this->CI
->lang
->line('profiler_section_hide').'\':\''.$this->CI
->lang
->line('profiler_section_show').'\';">'.$this->CI
->lang
->line('profiler_section_show').'</span>)';
207 $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
209 $output .= '<legend style="color:#0000FF;"> '.$this->CI
->lang
->line('profiler_database').': '.$db->database
.' '.$this->CI
->lang
->line('profiler_queries').': '.count($db->queries
).' '.$show_hide_js.'</legend>';
211 $output .= "\n\n<table style='width:100%;{$hide_queries}' id='ci_profiler_queries_db_{$count}'>\n";
213 if (count($db->queries
) == 0)
215 $output .= "<tr><td style='width:100%;color:#0000FF;font-weight:normal;background-color:#eee;padding:5px;'>".$this->CI
->lang
->line('profiler_no_queries')."</td></tr>\n";
219 foreach ($db->queries
as $key => $val)
221 $time = number_format($db->query_times
[$key], 4);
223 $val = highlight_code($val, ENT_QUOTES
);
225 foreach ($highlight as $bold)
227 $val = str_replace($bold, '<strong>'.$bold.'</strong>', $val);
230 $output .= "<tr><td style='padding:5px; vertical-align: top;width:1%;color:#900;font-weight:normal;background-color:#ddd;'>".$time." </td><td style='padding:5px; color:#000;font-weight:normal;background-color:#ddd;'>".$val."</td></tr>\n";
234 $output .= "</table>\n";
235 $output .= "</fieldset>";
243 // --------------------------------------------------------------------
250 protected function _compile_get()
253 $output .= '<fieldset id="ci_profiler_get" style="border:1px solid #cd6e00;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
255 $output .= '<legend style="color:#cd6e00;"> '.$this->CI
->lang
->line('profiler_get_data').' </legend>';
258 if (count($_GET) == 0)
260 $output .= "<div style='color:#cd6e00;font-weight:normal;padding:4px 0 4px 0'>".$this->CI
->lang
->line('profiler_no_get')."</div>";
264 $output .= "\n\n<table style='width:100%; border:none'>\n";
266 foreach ($_GET as $key => $val)
268 if ( ! is_numeric($key))
273 $output .= "<tr><td style='width:50%;color:#000;background-color:#ddd;padding:5px'>$_GET[".$key."] </td><td style='width:50%;padding:5px;color:#cd6e00;font-weight:normal;background-color:#ddd;'>";
276 $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
280 $output .= htmlspecialchars(stripslashes($val));
282 $output .= "</td></tr>\n";
285 $output .= "</table>\n";
287 $output .= "</fieldset>";
292 // --------------------------------------------------------------------
295 * Compile $_POST Data
299 protected function _compile_post()
302 $output .= '<fieldset id="ci_profiler_post" style="border:1px solid #009900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
304 $output .= '<legend style="color:#009900;"> '.$this->CI
->lang
->line('profiler_post_data').' </legend>';
307 if (count($_POST) == 0)
309 $output .= "<div style='color:#009900;font-weight:normal;padding:4px 0 4px 0'>".$this->CI
->lang
->line('profiler_no_post')."</div>";
313 $output .= "\n\n<table style='width:100%'>\n";
315 foreach ($_POST as $key => $val)
317 if ( ! is_numeric($key))
322 $output .= "<tr><td style='width:50%;padding:5px;color:#000;background-color:#ddd;'>$_POST[".$key."] </td><td style='width:50%;padding:5px;color:#009900;font-weight:normal;background-color:#ddd;'>";
325 $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, TRUE))) . "</pre>";
329 $output .= htmlspecialchars(stripslashes($val));
331 $output .= "</td></tr>\n";
334 $output .= "</table>\n";
336 $output .= "</fieldset>";
341 // --------------------------------------------------------------------
348 protected function _compile_uri_string()
351 $output .= '<fieldset id="ci_profiler_uri_string" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
353 $output .= '<legend style="color:#000;"> '.$this->CI
->lang
->line('profiler_uri_string').' </legend>';
356 if ($this->CI
->uri
->uri_string
== '')
358 $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI
->lang
->line('profiler_no_uri')."</div>";
362 $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI
->uri
->uri_string
."</div>";
365 $output .= "</fieldset>";
370 // --------------------------------------------------------------------
373 * Show the controller and function that were called
377 protected function _compile_controller_info()
380 $output .= '<fieldset id="ci_profiler_controller_info" style="border:1px solid #995300;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
382 $output .= '<legend style="color:#995300;"> '.$this->CI
->lang
->line('profiler_controller_info').' </legend>';
385 $output .= "<div style='color:#995300;font-weight:normal;padding:4px 0 4px 0'>".$this->CI
->router
->fetch_class()."/".$this->CI
->router
->fetch_method()."</div>";
387 $output .= "</fieldset>";
392 // --------------------------------------------------------------------
395 * Compile memory usage
397 * Display total used memory
401 protected function _compile_memory_usage()
404 $output .= '<fieldset id="ci_profiler_memory_usage" style="border:1px solid #5a0099;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
406 $output .= '<legend style="color:#5a0099;"> '.$this->CI
->lang
->line('profiler_memory_usage').' </legend>';
409 if (function_exists('memory_get_usage') && ($usage = memory_get_usage()) != '')
411 $output .= "<div style='color:#5a0099;font-weight:normal;padding:4px 0 4px 0'>".number_format($usage).' bytes</div>';
415 $output .= "<div style='color:#5a0099;font-weight:normal;padding:4px 0 4px 0'>".$this->CI
->lang
->line('profiler_no_memory')."</div>";
418 $output .= "</fieldset>";
423 // --------------------------------------------------------------------
426 * Compile header information
432 protected function _compile_http_headers()
435 $output .= '<fieldset id="ci_profiler_http_headers" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
437 $output .= '<legend style="color:#000;"> '.$this->CI
->lang
->line('profiler_headers').' (<span style="cursor: pointer;" onclick="var s=document.getElementById(\'ci_profiler_httpheaders_table\').style;s.display=s.display==\'none\'?\'\':\'none\';this.innerHTML=this.innerHTML==\''.$this->CI
->lang
->line('profiler_section_show').'\'?\''.$this->CI
->lang
->line('profiler_section_hide').'\':\''.$this->CI
->lang
->line('profiler_section_show').'\';">'.$this->CI
->lang
->line('profiler_section_show').'</span>)</legend>';
440 $output .= "\n\n<table style='width:100%;display:none' id='ci_profiler_httpheaders_table'>\n";
442 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)
444 $val = (isset($_SERVER[$header])) ? $_SERVER[$header] : '';
445 $output .= "<tr><td style='vertical-align: top;width:50%;padding:5px;color:#900;background-color:#ddd;'>".$header." </td><td style='width:50%;padding:5px;color:#000;background-color:#ddd;'>".$val."</td></tr>\n";
448 $output .= "</table>\n";
449 $output .= "</fieldset>";
454 // --------------------------------------------------------------------
457 * Compile config information
459 * Lists developer config variables
463 protected function _compile_config()
466 $output .= '<fieldset id="ci_profiler_config" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
468 $output .= '<legend style="color:#000;"> '.$this->CI
->lang
->line('profiler_config').' (<span style="cursor: pointer;" onclick="var s=document.getElementById(\'ci_profiler_config_table\').style;s.display=s.display==\'none\'?\'\':\'none\';this.innerHTML=this.innerHTML==\''.$this->CI
->lang
->line('profiler_section_show').'\'?\''.$this->CI
->lang
->line('profiler_section_hide').'\':\''.$this->CI
->lang
->line('profiler_section_show').'\';">'.$this->CI
->lang
->line('profiler_section_show').'</span>)</legend>';
471 $output .= "\n\n<table style='width:100%; display:none' id='ci_profiler_config_table'>\n";
473 foreach ($this->CI
->config
->config
as $config=>$val)
477 $val = print_r($val, TRUE);
480 $output .= "<tr><td style='padding:5px; vertical-align: top;color:#900;background-color:#ddd;'>".$config." </td><td style='padding:5px; color:#000;background-color:#ddd;'>".htmlspecialchars($val)."</td></tr>\n";
483 $output .= "</table>\n";
484 $output .= "</fieldset>";
489 // --------------------------------------------------------------------
492 * Compile session userdata
496 private function _compile_session_data()
498 if ( ! isset($this->CI
->session
))
503 $output = '<fieldset id="ci_profiler_csession" style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
504 $output .= '<legend style="color:#000;"> '.$this->CI
->lang
->line('profiler_session_data').' (<span style="cursor: pointer;" onclick="var s=document.getElementById(\'ci_profiler_session_data\').style;s.display=s.display==\'none\'?\'\':\'none\';this.innerHTML=this.innerHTML==\''.$this->CI
->lang
->line('profiler_section_show').'\'?\''.$this->CI
->lang
->line('profiler_section_hide').'\':\''.$this->CI
->lang
->line('profiler_section_show').'\';">'.$this->CI
->lang
->line('profiler_section_show').'</span>)</legend>';
505 $output .= "<table style='width:100%;display:none' id='ci_profiler_session_data'>";
507 foreach ($this->CI
->session
->all_userdata() as $key => $val)
509 if (is_array($val) OR is_object($val))
511 $val = print_r($val, TRUE);
514 $output .= "<tr><td style='padding:5px; vertical-align: top;color:#900;background-color:#ddd;'>".$key." </td><td style='padding:5px; color:#000;background-color:#ddd;'>".htmlspecialchars($val)."</td></tr>\n";
517 $output .= '</table>';
518 $output .= "</fieldset>";
522 // --------------------------------------------------------------------
529 public function run()
531 $output = "<div id='codeigniter_profiler' style='clear:both;background-color:#fff;padding:10px;'>";
532 $fields_displayed = 0;
534 foreach ($this->_available_sections
as $section)
536 if ($this->_compile_{$section
} !== FALSE)
538 $func = "_compile_{$section}";
539 $output .= $this->{$func
}();
544 if ($fields_displayed == 0)
546 $output .= '<p style="border:1px solid #5a0099;padding:10px;margin:20px 0;background-color:#eee">'.$this->CI
->lang
->line('profiler_no_profiles').'</p>';
555 // END CI_Profiler class
557 /* End of file Profiler.php */
558 /* Location: ./system/libraries/Profiler.php */