| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
class HC3_Profiler |
| 3 |
{ |
| 4 |
private $benchmark = NULL; |
| 5 |
protected $_available_sections = array( |
| 6 |
'benchmarks', |
| 7 |
'get', |
| 8 |
'memory_usage', |
| 9 |
'post', |
| 10 |
'uri_string', |
| 11 |
'controller_info', |
| 12 |
'queries', |
| 13 |
'http_headers', |
| 14 |
'session_data', |
| 15 |
'config' |
| 16 |
); |
| 17 |
|
| 18 |
protected $_query_toggle_count = 25; |
| 19 |
protected $dbs = array(); |
| 20 |
|
| 21 |
public $_compile_benchmarks; |
| 22 |
public $_compile_get; |
| 23 |
public $_compile_memory_usage; |
| 24 |
public $_compile_post; |
| 25 |
public $_compile_uri_string; |
| 26 |
public $_compile_controller_info; |
| 27 |
public $_compile_queries; |
| 28 |
public $_compile_http_headers; |
| 29 |
public $_compile_session_data; |
| 30 |
public $_compile_config; |
| 31 |
|
| 32 |
public function add_db( $db ) |
| 33 |
{ |
| 34 |
$this->dbs[] = $db; |
| 35 |
} |
| 36 |
|
| 37 |
public function __construct() |
| 38 |
{ |
| 39 |
$this->benchmark = new HC3_Profiler_Benchmark; |
| 40 |
|
| 41 |
// default all sections to display |
| 42 |
foreach ($this->_available_sections as $section){ |
| 43 |
if ( ! isset($config[$section])){ |
| 44 |
$this->{'_compile_' . $section} = TRUE; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
// $this->set_sections($config); |
| 49 |
|
| 50 |
// if wordpress |
| 51 |
if( defined('WPINC') ){ |
| 52 |
global $wpdb; |
| 53 |
$this->add_db( $wpdb ); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
function mark($name) |
| 58 |
{ |
| 59 |
$this->benchmark->mark($name); |
| 60 |
return $this; |
| 61 |
} |
| 62 |
|
| 63 |
public function set_sections($config) |
| 64 |
{ |
| 65 |
foreach ($config as $method => $enable){ |
| 66 |
if (in_array($method, $this->_available_sections)){ |
| 67 |
$this->{'_compile_' . $method} = ($enable !== FALSE) ? TRUE : FALSE; |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
protected function _compile_benchmarks() |
| 73 |
{ |
| 74 |
$profile = array(); |
| 75 |
|
| 76 |
foreach ( $this->benchmark->marker as $key => $val){ |
| 77 |
// We match the "end" marker so that the list ends |
| 78 |
// up in the order that it was defined |
| 79 |
if (preg_match("/(.+?)_end/i", $key, $match)){ |
| 80 |
if (isset($this->benchmark->marker[$match[1].'_end']) AND isset($this->benchmark->marker[$match[1].'_start'])){ |
| 81 |
$profile[$match[1]] = $this->benchmark->elapsed_time($match[1].'_start', $key); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
$output = "\n\n"; |
| 87 |
$output .= '<fieldset id="ci_profiler_benchmarks" style="border:1px solid #900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">'; |
| 88 |
$output .= "\n"; |
| 89 |
$output .= '<legend style="color:#900;"> '.'profiler_benchmarks'.' </legend>'; |
| 90 |
$output .= "\n"; |
| 91 |
$output .= "\n\n<table style='width:100%'>\n"; |
| 92 |
|
| 93 |
foreach ($profile as $key => $val){ |
| 94 |
$key = ucwords(str_replace(array('_', '-'), ' ', $key)); |
| 95 |
$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"; |
| 96 |
} |
| 97 |
|
| 98 |
$output .= "</table>\n"; |
| 99 |
$output .= "</fieldset>"; |
| 100 |
|
| 101 |
return $output; |
| 102 |
} |
| 103 |
|
| 104 |
protected function _compile_queries() |
| 105 |
{ |
| 106 |
$dbs = $this->dbs; |
| 107 |
|
| 108 |
if( (count($dbs) == 0) && defined('NTS_DEVELOPMENT3') ){ |
| 109 |
global $wpdb; |
| 110 |
$dbs[] = $wpdb; |
| 111 |
} |
| 112 |
|
| 113 |
if (count($dbs) == 0){ |
| 114 |
$output = "\n\n"; |
| 115 |
$output .= '<fieldset id="ci_profiler_queries" style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">'; |
| 116 |
$output .= "\n"; |
| 117 |
$output .= '<legend style="color:#0000FF;"> '.'profiler_queries'.' </legend>'; |
| 118 |
$output .= "\n"; |
| 119 |
$output .= "\n\n<table style='border:none; width:100%;'>\n"; |
| 120 |
$output .="<tr><td style='width:100%;color:#0000FF;font-weight:normal;background-color:#eee;padding:5px'>".'profiler_no_db'."</td></tr>\n"; |
| 121 |
$output .= "</table>\n"; |
| 122 |
$output .= "</fieldset>"; |
| 123 |
|
| 124 |
return $output; |
| 125 |
} |
| 126 |
|
| 127 |
// Key words we want bolded |
| 128 |
$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', '(', ')'); |
| 129 |
|
| 130 |
$output = "\n\n"; |
| 131 |
|
| 132 |
$count = 0; |
| 133 |
|
| 134 |
foreach ($dbs as $db){ |
| 135 |
$queries = method_exists($db, 'queries') ? $db->queries() : $db->queries; |
| 136 |
|
| 137 |
$count++; |
| 138 |
|
| 139 |
$hide_queries = (count($queries) > $this->_query_toggle_count) ? ' display:none' : ''; |
| 140 |
|
| 141 |
$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==\''.'profiler_section_hide'.'\'?\''.'profiler_section_show'.'\':\''.'profiler_section_hide'.'\';">'.'profiler_section_hide'.'</span>)'; |
| 142 |
|
| 143 |
if ($hide_queries != ''){ |
| 144 |
$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==\''.'profiler_section_show'.'\'?\''.'profiler_section_hide'.'\':\''.'profiler_section_show'.'\';">'.'profiler_section_show'.'</span>)'; |
| 145 |
} |
| 146 |
|
| 147 |
$output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">'; |
| 148 |
$output .= "\n"; |
| 149 |
$output .= '<legend style="color:#0000FF;"> '.'profiler_queries'.': '.count($queries).' '.$show_hide_js.'</legend>'; |
| 150 |
$output .= "\n"; |
| 151 |
$output .= "\n\n<table style='width:100%;{$hide_queries}' id='ci_profiler_queries_db_{$count}'>\n"; |
| 152 |
|
| 153 |
if (count($queries) == 0){ |
| 154 |
$output .= "<tr><td style='width:100%;color:#0000FF;font-weight:normal;background-color:#eee;padding:5px;'>".'profiler_no_queries'."</td></tr>\n"; |
| 155 |
} |
| 156 |
else { |
| 157 |
foreach ($queries as $key => $val){ |
| 158 |
list( $val, $time ) = $val; |
| 159 |
$time = number_format( $time, 4); |
| 160 |
$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"; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
$output .= "</table>\n"; |
| 165 |
$output .= "</fieldset>"; |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
return $output; |
| 170 |
} |
| 171 |
|
| 172 |
protected function _compile_get() |
| 173 |
{ |
| 174 |
$output = "\n\n"; |
| 175 |
$output .= '<fieldset id="ci_profiler_get" style="border:1px solid #cd6e00;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">'; |
| 176 |
$output .= "\n"; |
| 177 |
$output .= '<legend style="color:#cd6e00;"> '.'profiler_get_data'.' </legend>'; |
| 178 |
$output .= "\n"; |
| 179 |
|
| 180 |
if (count($_GET) == 0){ |
| 181 |
$output .= "<div style='color:#cd6e00;font-weight:normal;padding:4px 0 4px 0'>".'profiler_no_get'."</div>"; |
| 182 |
} |
| 183 |
else { |
| 184 |
$output .= "\n\n<table style='width:100%; border:none'>\n"; |
| 185 |
|
| 186 |
foreach ($_GET as $key => $val){ |
| 187 |
if ( ! is_numeric($key)){ |
| 188 |
$key = "'".$key."'"; |
| 189 |
} |
| 190 |
|
| 191 |
$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;'>"; |
| 192 |
if (is_array($val)){ |
| 193 |
$output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>"; |
| 194 |
} |
| 195 |
else { |
| 196 |
$output .= htmlspecialchars(stripslashes($val)); |
| 197 |
} |
| 198 |
$output .= "</td></tr>\n"; |
| 199 |
} |
| 200 |
|
| 201 |
$output .= "</table>\n"; |
| 202 |
} |
| 203 |
$output .= "</fieldset>"; |
| 204 |
|
| 205 |
return $output; |
| 206 |
} |
| 207 |
|
| 208 |
protected function _compile_post() |
| 209 |
{ |
| 210 |
$output = "\n\n"; |
| 211 |
$output .= '<fieldset id="ci_profiler_post" style="border:1px solid #009900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">'; |
| 212 |
$output .= "\n"; |
| 213 |
$output .= '<legend style="color:#009900;"> '.'profiler_post_data'.' </legend>'; |
| 214 |
$output .= "\n"; |
| 215 |
|
| 216 |
if (count($_POST) == 0){ |
| 217 |
$output .= "<div style='color:#009900;font-weight:normal;padding:4px 0 4px 0'>".'profiler_no_post'."</div>"; |
| 218 |
} |
| 219 |
else { |
| 220 |
$output .= "\n\n<table style='width:100%'>\n"; |
| 221 |
|
| 222 |
foreach ($_POST as $key => $val){ |
| 223 |
if ( ! is_numeric($key)){ |
| 224 |
$key = "'".$key."'"; |
| 225 |
} |
| 226 |
|
| 227 |
$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;'>"; |
| 228 |
if (is_array($val)){ |
| 229 |
$output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, TRUE))) . "</pre>"; |
| 230 |
} |
| 231 |
else { |
| 232 |
$output .= htmlspecialchars(stripslashes($val)); |
| 233 |
} |
| 234 |
$output .= "</td></tr>\n"; |
| 235 |
} |
| 236 |
|
| 237 |
$output .= "</table>\n"; |
| 238 |
} |
| 239 |
$output .= "</fieldset>"; |
| 240 |
|
| 241 |
return $output; |
| 242 |
} |
| 243 |
|
| 244 |
protected function _compile_uri_string() |
| 245 |
{ |
| 246 |
return; |
| 247 |
$output = "\n\n"; |
| 248 |
$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">'; |
| 249 |
$output .= "\n"; |
| 250 |
$output .= '<legend style="color:#000;"> '.'profiler_uri_string'.' </legend>'; |
| 251 |
$output .= "\n"; |
| 252 |
|
| 253 |
if ($this->CI->uri->uri_string == ''){ |
| 254 |
$output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".'profiler_no_uri'."</div>"; |
| 255 |
} |
| 256 |
else { |
| 257 |
$output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->uri->uri_string."</div>"; |
| 258 |
} |
| 259 |
|
| 260 |
$output .= "</fieldset>"; |
| 261 |
return $output; |
| 262 |
} |
| 263 |
|
| 264 |
protected function _compile_controller_info() |
| 265 |
{ |
| 266 |
return; |
| 267 |
$output = "\n\n"; |
| 268 |
$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">'; |
| 269 |
$output .= "\n"; |
| 270 |
$output .= '<legend style="color:#995300;"> '.'profiler_controller_info'.' </legend>'; |
| 271 |
$output .= "\n"; |
| 272 |
|
| 273 |
$output .= "<div style='color:#995300;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->router->fetch_class()."/".$this->CI->router->fetch_method()."</div>"; |
| 274 |
|
| 275 |
$output .= "</fieldset>"; |
| 276 |
|
| 277 |
return $output; |
| 278 |
} |
| 279 |
|
| 280 |
protected function _compile_memory_usage() |
| 281 |
{ |
| 282 |
$output = "\n\n"; |
| 283 |
$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">'; |
| 284 |
$output .= "\n"; |
| 285 |
$output .= '<legend style="color:#5a0099;"> '.'profiler_memory_usage'.' </legend>'; |
| 286 |
$output .= "\n"; |
| 287 |
|
| 288 |
if (function_exists('memory_get_usage') && ($usage = memory_get_usage()) != ''){ |
| 289 |
$output .= "<div style='color:#5a0099;font-weight:normal;padding:4px 0 4px 0'>".number_format($usage).' bytes</div>'; |
| 290 |
} |
| 291 |
else { |
| 292 |
$output .= "<div style='color:#5a0099;font-weight:normal;padding:4px 0 4px 0'>".'profiler_no_memory'."</div>"; |
| 293 |
} |
| 294 |
|
| 295 |
$output .= "</fieldset>"; |
| 296 |
return $output; |
| 297 |
} |
| 298 |
|
| 299 |
protected function _compile_http_headers() |
| 300 |
{ |
| 301 |
$output = "\n\n"; |
| 302 |
$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">'; |
| 303 |
$output .= "\n"; |
| 304 |
$output .= '<legend style="color:#000;"> '.'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==\''.'profiler_section_show'.'\'?\''.'profiler_section_hide'.'\':\''.'profiler_section_show'.'\';">'.'profiler_section_show'.'</span>)</legend>'; |
| 305 |
$output .= "\n"; |
| 306 |
|
| 307 |
$output .= "\n\n<table style='width:100%;display:none' id='ci_profiler_httpheaders_table'>\n"; |
| 308 |
|
| 309 |
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){ |
| 310 |
$val = (isset($_SERVER[$header])) ? $_SERVER[$header] : ''; |
| 311 |
$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"; |
| 312 |
} |
| 313 |
|
| 314 |
$output .= "</table>\n"; |
| 315 |
$output .= "</fieldset>"; |
| 316 |
|
| 317 |
return $output; |
| 318 |
} |
| 319 |
|
| 320 |
protected function _compile_config() |
| 321 |
{ |
| 322 |
return; |
| 323 |
} |
| 324 |
|
| 325 |
private function _compile_session_data() |
| 326 |
{ |
| 327 |
return; |
| 328 |
} |
| 329 |
|
| 330 |
public function render( $content ) |
| 331 |
{ |
| 332 |
$out = $content . $this->run(); |
| 333 |
return $out; |
| 334 |
} |
| 335 |
|
| 336 |
public function run() |
| 337 |
{ |
| 338 |
$output = "<div class='hc-xs-hide' id='codeigniter_profiler' style='clear:both;background-color:#fff;padding:10px; margin-top: 5em;'>"; |
| 339 |
$fields_displayed = 0; |
| 340 |
|
| 341 |
foreach ($this->_available_sections as $section){ |
| 342 |
if ($this->{'_compile_' . $section} !== FALSE){ |
| 343 |
$func = "_compile_{$section}"; |
| 344 |
$output .= $this->{$func}(); |
| 345 |
$fields_displayed++; |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
if ($fields_displayed == 0){ |
| 350 |
$output .= '<p style="border:1px solid #5a0099;padding:10px;margin:20px 0;background-color:#eee">'.'profiler_no_profiles'.'</p>'; |
| 351 |
} |
| 352 |
|
| 353 |
$output .= '</div>'; |
| 354 |
return $output; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
class HC3_Profiler_Benchmark |
| 359 |
{ |
| 360 |
var $marker = array(); |
| 361 |
|
| 362 |
function mark($name) |
| 363 |
{ |
| 364 |
$this->marker[$name] = microtime(); |
| 365 |
} |
| 366 |
|
| 367 |
function elapsed_time($point1 = '', $point2 = '', $decimals = 4) |
| 368 |
{ |
| 369 |
if ($point1 == ''){ |
| 370 |
return '{elapsed_time}'; |
| 371 |
} |
| 372 |
|
| 373 |
if ( ! isset($this->marker[$point1])){ |
| 374 |
return ''; |
| 375 |
} |
| 376 |
|
| 377 |
if ( ! isset($this->marker[$point2])){ |
| 378 |
$this->marker[$point2] = microtime(); |
| 379 |
} |
| 380 |
|
| 381 |
list($sm, $ss) = explode(' ', $this->marker[$point1]); |
| 382 |
list($em, $es) = explode(' ', $this->marker[$point2]); |
| 383 |
|
| 384 |
return number_format(($em + $es) - ($sm + $ss), $decimals); |
| 385 |
} |
| 386 |
|
| 387 |
function memory_usage() |
| 388 |
{ |
| 389 |
return '{memory_usage}'; |
| 390 |
} |
| 391 |
} |
| 392 |
|