Profiler.php
Go to the documentation of this file.00001 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 class CI_Profiler {
00034
00035 var $CI;
00036
00037 function CI_Profiler()
00038 {
00039 $this->CI =& get_instance();
00040 $this->CI->load->language('profiler');
00041 }
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 function _compile_benchmarks()
00057 {
00058 $profile = array();
00059 foreach ($this->CI->benchmark->marker as $key => $val)
00060 {
00061
00062
00063 if (preg_match("/(.+?)_end/i", $key, $match))
00064 {
00065 if (isset($this->CI->benchmark->marker[$match[1].'_end']) AND isset($this->CI->benchmark->marker[$match[1].'_start']))
00066 {
00067 $profile[$match[1]] = $this->CI->benchmark->elapsed_time($match[1].'_start', $key);
00068 }
00069 }
00070 }
00071
00072
00073
00074
00075
00076 $output = "\n\n";
00077 $output .= '<fieldset style="border:1px solid #990000;padding:6px 10px 10px 10px;margin:0 0 20px 0;background-color:#eee">';
00078 $output .= "\n";
00079 $output .= '<legend style="color:#990000;"> '.$this->CI->lang->line('profiler_benchmarks').' </legend>';
00080 $output .= "\n";
00081 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
00082
00083 foreach ($profile as $key => $val)
00084 {
00085 $key = ucwords(str_replace(array('_', '-'), ' ', $key));
00086 $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";
00087 }
00088
00089 $output .= "</table>\n";
00090 $output .= "</fieldset>";
00091
00092 return $output;
00093 }
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 function _compile_queries()
00104 {
00105 $output = "\n\n";
00106 $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
00107 $output .= "\n";
00108
00109 if ( ! class_exists('CI_DB_driver'))
00110 {
00111 $output .= '<legend style="color:#0000FF;"> '.$this->CI->lang->line('profiler_queries').' </legend>';
00112 $output .= "\n";
00113 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
00114 $output .="<tr><td width='100%' style='color:#0000FF;font-weight:normal;background-color:#eee;'>".$this->CI->lang->line('profiler_no_db')."</td></tr>\n";
00115 }
00116 else
00117 {
00118 $output .= '<legend style="color:#0000FF;"> '.$this->CI->lang->line('profiler_queries').' ('.count($this->CI->db->queries).') </legend>';
00119 $output .= "\n";
00120 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
00121
00122 if (count($this->CI->db->queries) == 0)
00123 {
00124 $output .= "<tr><td width='100%' style='color:#0000FF;font-weight:normal;background-color:#eee;'>".$this->CI->lang->line('profiler_no_queries')."</td></tr>\n";
00125 }
00126 else
00127 {
00128 $highlight = array('SELECT', 'FROM', 'WHERE', 'AND', 'LEFT JOIN', 'ORDER BY', 'LIMIT', 'INSERT', 'INTO', 'VALUES', 'UPDATE', 'OR');
00129
00130 foreach ($this->CI->db->queries as $key => $val)
00131 {
00132 $val = htmlspecialchars($val, ENT_QUOTES);
00133 $time = number_format($this->CI->db->query_times[$key], 4);
00134
00135 foreach ($highlight as $bold)
00136 {
00137 $val = str_replace($bold, '<strong>'.$bold.'</strong>', $val);
00138 }
00139
00140 $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";
00141 }
00142 }
00143 }
00144
00145 $output .= "</table>\n";
00146 $output .= "</fieldset>";
00147
00148 return $output;
00149 }
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160 function _compile_get()
00161 {
00162 $output = "\n\n";
00163 $output .= '<fieldset style="border:1px solid #cd6e00;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
00164 $output .= "\n";
00165 $output .= '<legend style="color:#cd6e00;"> '.$this->CI->lang->line('profiler_get_data').' </legend>';
00166 $output .= "\n";
00167
00168 if (count($_GET) == 0)
00169 {
00170 $output .= "<div style='color:#cd6e00;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_get')."</div>";
00171 }
00172 else
00173 {
00174 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
00175
00176 foreach ($_GET as $key => $val)
00177 {
00178 if ( ! is_numeric($key))
00179 {
00180 $key = "'".$key."'";
00181 }
00182
00183 $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;'>";
00184 if (is_array($val))
00185 {
00186 $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
00187 }
00188 else
00189 {
00190 $output .= htmlspecialchars(stripslashes($val));
00191 }
00192 $output .= "</td></tr>\n";
00193 }
00194
00195 $output .= "</table>\n";
00196 }
00197 $output .= "</fieldset>";
00198
00199 return $output;
00200 }
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 function _compile_post()
00211 {
00212 $output = "\n\n";
00213 $output .= '<fieldset style="border:1px solid #009900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
00214 $output .= "\n";
00215 $output .= '<legend style="color:#009900;"> '.$this->CI->lang->line('profiler_post_data').' </legend>';
00216 $output .= "\n";
00217
00218 if (count($_POST) == 0)
00219 {
00220 $output .= "<div style='color:#009900;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_post')."</div>";
00221 }
00222 else
00223 {
00224 $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
00225
00226 foreach ($_POST as $key => $val)
00227 {
00228 if ( ! is_numeric($key))
00229 {
00230 $key = "'".$key."'";
00231 }
00232
00233
00234 $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;'>";
00235 if (is_array($val))
00236 {
00237 $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
00238 }
00239 else
00240 {
00241 $output .= htmlspecialchars(stripslashes($val));
00242 }
00243 $output .= "</td></tr>\n";
00244 }
00245
00246 $output .= "</table>\n";
00247 }
00248 $output .= "</fieldset>";
00249
00250 return $output;
00251 }
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261 function _compile_uri_string()
00262 {
00263 $output = "\n\n";
00264 $output .= '<fieldset style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
00265 $output .= "\n";
00266 $output .= '<legend style="color:#000;"> '.$this->CI->lang->line('profiler_uri_string').' </legend>';
00267 $output .= "\n";
00268
00269 if ($this->CI->uri->uri_string == '')
00270 {
00271 $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_uri')."</div>";
00272 }
00273 else
00274 {
00275 $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->uri->uri_string."</div>";
00276 }
00277
00278 $output .= "</fieldset>";
00279
00280 return $output;
00281 }
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293 function _compile_memory_usage()
00294 {
00295 $output = "\n\n";
00296 $output .= '<fieldset style="border:1px solid #000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
00297 $output .= "\n";
00298 $output .= '<legend style="color:#000;"> '.$this->CI->lang->line('profiler_memory_usage').' </legend>';
00299 $output .= "\n";
00300
00301 if (function_exists('memory_get_usage') && ($usage = memory_get_usage()) != '')
00302 {
00303 $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".number_format($usage).' bytes</div>';
00304 }
00305 else
00306 {
00307 $output .= "<div style='color:#000;font-weight:normal;padding:4px 0 4px 0'>".$this->CI->lang->line('profiler_no_memory_usage')."</div>";
00308 }
00309
00310 $output .= "</fieldset>";
00311
00312 return $output;
00313 }
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323 function run()
00324 {
00325 $output = '<br clear="all" />';
00326 $output .= "<div style='background-color:#fff;padding:10px;'>";
00327
00328 $output .= $this->_compile_memory_usage();
00329 $output .= $this->_compile_benchmarks();
00330 $output .= $this->_compile_uri_string();
00331 $output .= $this->_compile_get();
00332 $output .= $this->_compile_post();
00333 $output .= $this->_compile_queries();
00334
00335 $output .= '</div>';
00336
00337 return $output;
00338 }
00339
00340 }
00341
00342
00343
00344
00345