Profiler.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2006, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * CodeIgniter Profiler Class
00020  *
00021  * This class enables you to display benchmark, query, and other data
00022  * in order to help with debugging and optimization.
00023  *
00024  * Note: At some point it would be good to move all the HTML in this class
00025  * into a set of template files in order to allow customization.
00026  *
00027  * @package             CodeIgniter
00028  * @subpackage  Libraries
00029  * @category    Libraries
00030  * @author              ExpressionEngine Dev Team
00031  * @link                http://codeigniter.com/user_guide/general/profiling.html
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          * Auto Profiler
00047          *
00048          * This function cycles through the entire array of mark points and
00049          * matches any two points that are named identically (ending in "_start"
00050          * and "_end" respectively).  It then compiles the execution times for
00051          * all points and returns it as an array
00052          *
00053          * @access      private
00054          * @return      array
00055          */
00056         function _compile_benchmarks()
00057         {
00058                 $profile = array();
00059                 foreach ($this->CI->benchmark->marker as $key => $val)
00060                 {
00061                         // We match the "end" marker so that the list ends
00062                         // up in the order that it was defined
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                 // Build a table containing the profile data.
00073                 // Note: At some point we should turn this into a template that can
00074                 // be modified.  We also might want to make this data available to be logged
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;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_benchmarks').'&nbsp;&nbsp;</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."&nbsp;&nbsp;</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          * Compile Queries
00099          *
00100          * @access      private
00101          * @return      string
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;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').'&nbsp;&nbsp;</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;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').' ('.count($this->CI->db->queries).')&nbsp;&nbsp;</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."&nbsp;&nbsp;</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          * Compile $_GET Data
00156          *
00157          * @access      private
00158          * @return      string
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;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_get_data').'&nbsp;&nbsp;</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;'>&#36;_GET[".$key."]&nbsp;&nbsp; </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          * Compile $_POST Data
00206          *
00207          * @access      private
00208          * @return      string
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;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_post_data').'&nbsp;&nbsp;</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 //                              $output .= "<tr><td width='50%' style='color:#000;background-color:#ddd;'>&#36;_POST[".$key."]&nbsp;&nbsp;</td><td width='50%' style='color:#009900;font-weight:normal;background-color:#ddd;'>".htmlspecialchars(stripslashes($val))."</td></tr>\n";
00234                                 $output .= "<tr><td width='50%' style='color:#000;background-color:#ddd;'>&#36;_POST[".$key."]&nbsp;&nbsp; </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          * Show query string
00257          *
00258          * @access      private
00259          * @return      string
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;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_uri_string').'&nbsp;&nbsp;</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          * Compile memory usage
00287          *
00288          * Display total used memory
00289          *
00290          * @access      public
00291          * @return      string
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;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_memory_usage').'&nbsp;&nbsp;</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          * Run the Profiler
00319          *
00320          * @access      private
00321          * @return      string
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 // END CI_Profiler class
00343 
00344 /* End of file Profiler.php */
00345 /* Location: ./system/libraries/Profiler.php */