Changeset 2686

Show
Ignore:
Timestamp:
05/22/2008 04:13:52 AM (6 months ago)
Author:
PugFish
Message:

Added start of new Profiler library

Location:
trunk/system
Files:
3 added
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/config/profiler.php

    r1684 r2686  
    33 * @package  Profiler 
    44 * 
    5  * Show benchmarks. 
     5 * Array of section names to display in the Profiler, TRUE to display all of them. 
     6 * Built in sections are benchmarks, database, session, post and cookies, custom sections can be used too. 
    67 */ 
    7 $config['benchmarks'] = TRUE; 
    8  
    9 /** 
    10  * Show database queries. 
    11  */ 
    12 $config['database'] = TRUE; 
    13  
    14 /** 
    15  * Show POST data. 
    16  */ 
    17 $config['post'] = TRUE; 
    18  
    19 /** 
    20  * Show session data. 
    21  */ 
    22 $config['session'] = TRUE; 
    23  
    24 /** 
    25  * Show cookie data. 
    26  */ 
    27 $config['cookie'] = TRUE; 
     8$config['show'] = TRUE; 
  • trunk/system/libraries/Profiler.php

    r2018 r2686  
    33 * Adds useful information to the bottom of the current page for debugging and optimization purposes. 
    44 * 
    5  * Benchmarks   - The times and memory usage of benchmarks run by the <Benchmark> library 
    6  * Database     - The raw SQL and number of affected rows of <Database> queries 
    7  * POST Data    - The name and values of any POST data submitted to the current page 
    8  * Session Data - Data stored in the current session if using the <Session> library 
     5 * Benchmarks   - The times and memory usage of benchmarks run by the Benchmark library. 
     6 * Database     - The raw SQL and number of affected rows of Database queries. 
     7 * Session Data - Data stored in the current session if using the Session library. 
     8 * POST Data    - The name and values of any POST data submitted to the current page. 
     9 * Cookie Data  - All cookies sent for the current request. 
    910 * 
    1011 * $Id$ 
    1112 * 
    12  * @package    Core 
     13 * @package    Profiler 
    1314 * @author     Kohana Team 
    1415 * @copyright  (c) 2007-2008 Kohana Team 
     
    1718class Profiler_Core { 
    1819 
    19         /** 
    20          * Adds event for adding the profile output to the page when displayed. 
    21          */ 
     20        protected $profiles = array(); 
     21        protected $show; 
     22 
    2223        public function __construct() 
    2324        { 
     
    2930 
    3031        /** 
     32         * Magic __call method. Creates a new profiler section object. 
     33         * 
     34         * @param   string   input type 
     35         * @param   string   input name 
     36         * @return  object 
     37         */ 
     38        public function __call($method, $args) 
     39        { 
     40                if ( ! $this->show OR (is_array($this->show) AND ! in_array($args[0], $this->show))) 
     41                        return FALSE; 
     42 
     43                // Class name 
     44                $class = 'Profiler_'.ucfirst($method); 
     45 
     46                $class = new $class(); 
     47 
     48                $this->profiles[$args[0]] = $class; 
     49 
     50                return $class; 
     51        } 
     52 
     53        /** 
    3154         * Disables the profiler for this page only. 
    3255         * Best used when profiler is autoloaded. 
     56         * 
     57         * @return  void 
    3358         */ 
    3459        public function disable() 
     
    4671        public function render($return = FALSE) 
    4772        { 
    48                 $data = array(); 
    49  
    50                 if (Config::item('profiler.benchmarks')) 
    51                 { 
    52                         // Clean unique id from system benchmark names 
    53                         foreach (Benchmark::get(TRUE) as $name => $time) 
    54                         { 
    55                                 $data['benchmarks'][str_replace(SYSTEM_BENCHMARK.'_', '', $name)] = $time; 
    56                         } 
    57                 } 
    58  
    59                 // Load database benchmarks, if Database has been loaded 
    60                 if (Config::item('profiler.database') AND class_exists('Database', FALSE)) 
    61                 { 
    62                         $data['queries'] = Database::$benchmarks; 
    63                 } 
    64  
    65                 // Load POST data 
    66                 if (Config::item('profiler.post')) 
    67                 { 
    68                         $data['post'] = TRUE; 
    69                 } 
    70  
    71                 if (Config::item('profiler.session')) 
    72                 { 
    73                         $data['session'] = TRUE; 
    74                 } 
    75  
    76                 if (Config::item('profiler.cookie')) 
    77                 { 
    78                         $data['cookie'] = TRUE; 
    79                 } 
     73                $start = microtime(TRUE); 
     74 
     75                $get = isset($_GET['profiler']) ? explode(',', $_GET['profiler']) : array(); 
     76                $this->show = empty($get) ? Config::item('profiler.show') : $get; 
     77 
     78                Event::run('profiler.run'); 
     79 
     80                $this->benchmarks(); 
     81                $this->database(); 
     82                $this->session(); 
     83                $this->post(); 
     84                $this->cookies(); 
     85 
     86                $styles = ''; 
     87                foreach ($this->profiles as $profile) 
     88                { 
     89                        $styles .= $profile->styles(); 
     90                } 
     91 
     92                // Don't display if there's no profiles 
     93                if (empty($this->profiles)) 
     94                        return; 
    8095 
    8196                // Load the profiler view 
     97                $data = array 
     98                ( 
     99                        'profiles' => $this->profiles, 
     100                        'styles'   => $styles, 
     101                        'execution_time' => microtime(TRUE) - $start 
     102                ); 
    82103                $view = new View('kohana_profiler', $data); 
    83104 
     
    100121 
    101122        /** 
    102          * Magically convert this object to a string, the rendered profiler. 
    103          * 
    104          * @return  string 
    105          */ 
    106         public function __toString() 
    107         { 
    108                 return $this->render(TRUE); 
    109         } 
    110  
    111 } // End Profiler Class 
     123         * Benchmark times and memory usage from the Benchmark library. 
     124         * 
     125         * @return  void 
     126         */ 
     127        public function benchmarks() 
     128        { 
     129                if ( ! $table = $this->table('benchmarks')) 
     130                        return; 
     131 
     132                $table->add_column(); 
     133                $table->add_column('kp-column kp-data'); 
     134                $table->add_column('kp-column kp-data'); 
     135                $table->add_row(array('Benchmarks', 'Time', 'Memory'), 'kp-title', 'background-color: #FFE0E0'); 
     136 
     137                $benchmarks = Benchmark::get(TRUE); 
     138 
     139                // Moves the first benchmark (total execution time) to the end of the array 
     140                $benchmarks = array_slice($benchmarks, 1) + array_slice($benchmarks, 0, 1); 
     141 
     142                text::alternate(); 
     143                foreach ($benchmarks as $name => $benchmark) 
     144                { 
     145                        // Clean unique id from system benchmark names 
     146                        $name = ucwords(str_replace(array('_', '-'), ' ', str_replace(SYSTEM_BENCHMARK.'_', '', $name))); 
     147 
     148                        $data = array($name, number_format($benchmark['time'], 3), number_format($benchmark['memory'] / 1024 / 1024, 2).'MB'); 
     149                        $class = text::alternate('', 'kp-altrow'); 
     150 
     151                        if ($name == 'Total Execution') 
     152                                $class = 'kp-totalrow'; 
     153 
     154                        $table->add_row($data, $class); 
     155                } 
     156        } 
     157 
     158        /** 
     159         * Database query benchmarks. 
     160         * 
     161         * @return  void 
     162         */ 
     163        public function database() 
     164        { 
     165                if ( ! $table = $this->table('database')) 
     166                        return; 
     167 
     168                $table->add_column(); 
     169                $table->add_column('kp-column kp-data'); 
     170                $table->add_column('kp-column kp-data'); 
     171                $table->add_row(array('Queries', 'Time', 'Rows'), 'kp-title', 'background-color: #E0FFE0'); 
     172 
     173                $queries = Database::$benchmarks; 
     174 
     175                text::alternate(); 
     176                $total_time = 0; 
     177                foreach ($queries as $query) 
     178                { 
     179                        $data = array($query['query'], number_format($query['time'], 3), $query['rows']); 
     180                        $class = text::alternate('', 'kp-altrow'); 
     181                        $table->add_row($data, $class); 
     182                        $total_time += $query['time']; 
     183                } 
     184 
     185                $data = array('Total: ' . count($queries), number_format($total_time, 3), ''); 
     186                $table->add_row($data, 'kp-totalrow'); 
     187        } 
     188 
     189        /** 
     190         * Session data. 
     191         * 
     192         * @return  void 
     193         */ 
     194        public function session() 
     195        { 
     196                if (empty($_SESSION)) return; 
     197 
     198                if ( ! $table = $this->table('session')) 
     199                        return; 
     200 
     201                $table->add_column('kp-name'); 
     202                $table->add_column(); 
     203                $table->add_row(array('Session', 'Value'), 'kp-title', 'background-color: #CCE8FB'); 
     204 
     205                text::alternate(); 
     206                foreach($_SESSION as $name => $value) 
     207                { 
     208                        $data = array($name, $value); 
     209                        $class = text::alternate('', 'kp-altrow'); 
     210                        $table->add_row($data, $class); 
     211                } 
     212        } 
     213 
     214        /** 
     215         * POST data. 
     216         * 
     217         * @return  void 
     218         */ 
     219        public function post() 
     220        { 
     221                if (empty($_POST)) return; 
     222 
     223                if ( ! $table = $this->table('post')) 
     224                        return; 
     225 
     226                $table->add_column('kp-name'); 
     227                $table->add_column(); 
     228                $table->add_row(array('POST', 'Value'), 'kp-title', 'background-color: #E0E0FF'); 
     229 
     230                text::alternate(); 
     231                foreach($_POST as $name => $value) 
     232                { 
     233                        $data = array($name, $value); 
     234                        $class = text::alternate('', 'kp-altrow'); 
     235                        $table->add_row($data, $class); 
     236                } 
     237        } 
     238 
     239        /** 
     240         * Cookie data. 
     241         * 
     242         * @return  void 
     243         */ 
     244        public function cookies() 
     245        { 
     246                if (empty($_COOKIE)) return; 
     247 
     248                if ( ! $table = $this->table('cookies')) 
     249                        return; 
     250 
     251                $table->add_column('kp-name'); 
     252                $table->add_column(); 
     253                $table->add_row(array('Cookies', 'Value'), 'kp-title', 'background-color: #FFF4D7'); 
     254 
     255                text::alternate(); 
     256                foreach($_COOKIE as $name => $value) 
     257                { 
     258                        $data = array($name, $value); 
     259                        $class = text::alternate('', 'kp-altrow'); 
     260                        $table->add_row($data, $class); 
     261                } 
     262        } 
     263} 
  • trunk/system/views/kohana_profiler.php

    r2593 r2686  
    1515        font: inherit; 
    1616} 
    17 #kohana-profiler table 
    18 { 
    19         font-size: 1.0em; 
    20         color: #4D6171; 
    21         width: 100%; 
    22         border-collapse: collapse; 
    23         border-top: 1px solid #E5EFF8; 
    24         border-right: 1px solid #E5EFF8; 
    25         border-left: 1px solid #E5EFF8; 
    26         margin-bottom: 10px; 
    27 } 
    28 #kohana-profiler th 
    29 { 
    30         text-align: left; 
    31         border-bottom: 1px solid #E5EFF8; 
    32         background-color: #F9FCFE; 
    33         padding: 3px; 
    34         color: #263038; 
    35 } 
    36 #kohana-profiler td 
    37 { 
    38         background-color: #FFFFFF; 
    39         border-bottom: 1px solid #E5EFF8; 
    40         padding: 3px; 
    41 } 
    42 #kohana-profiler .kp-altrow td 
    43 { 
    44         background-color: #F7FBFF; 
    45 } 
    46 #kohana-profiler .kp-totalrow td 
    47 { 
    48         background-color: #FAFAFA; 
    49         border-top: 1px solid #D2DCE5; 
    50         font-weight: bold; 
    51 } 
    52 #kp-benchmarks th 
    53 { 
    54         background-color: #FFE0E0; 
    55 } 
    56 #kp-queries th 
    57 { 
    58         background-color: #E0FFE0; 
    59 } 
    60 #kp-postdata th 
    61 { 
    62         background-color: #E0E0FF; 
    63 } 
    64 #kp-sessiondata th 
    65 { 
    66         background-color: #CCE8FB; 
    67 } 
    68 #kp-cookiedata th 
    69 { 
    70         background-color: #FFF4D7; 
    71 } 
    72 #kohana-profiler .kp-column 
    73 { 
    74         width: 100px; 
    75         border-left: 1px solid #E5EFF8; 
    76         text-align: center; 
    77 } 
    78 #kohana-profiler .kp-data, #kohana-profiler .kp-name 
    79 { 
    80         background-color: #FAFAFB; 
    81         vertical-align: top; 
    82 } 
    83 #kohana-profiler .kp-name 
    84 { 
    85         width: 200px; 
    86         border-right: 1px solid #E5EFF8; 
    87 } 
    88 #kohana-profiler .kp-altrow .kp-data, #kohana-profiler .kp-altrow .kp-name 
    89 { 
    90         background-color: #F6F8FB; 
    91 } 
     17<?php echo $styles ?> 
    9218</style> 
    9319<div id="kohana-profiler"> 
    94  
    95 <?php if (isset($benchmarks)): ?> 
    96         <table id="kp-benchmarks"> 
    97                 <tr> 
    98                         <th><?php echo Kohana::lang('profiler.benchmarks') ?></th> 
    99                         <th class="kp-column">Time</th> 
    100                         <th class="kp-column">Memory</th> 
    101                 </tr> 
    10220<?php 
    103  
    104 // Moves the first benchmark (total execution time) to the end of the array 
    105 $benchmarks = array_slice($benchmarks, 1) + array_slice($benchmarks, 0, 1); 
    106  
    107 foreach ($benchmarks as $name => $benchmark): 
    108  
    109         $class = ($name == 'total_execution') ? ' class="kp-totalrow"' : text::alternate('', ' class="kp-altrow"'); 
    110         $name = ucwords(str_replace(array('_', '-'), ' ', $name)); 
    111  
     21foreach ($profiles as $profile) 
     22{ 
     23        echo $profile->render(); 
     24} 
    11225?> 
    113                 <tr<?php echo $class ?>> 
    114                         <td><?php echo $name ?></td> 
    115                         <td class="kp-column kp-data"><?php echo number_format($benchmark['time'], 4) ?></td> 
    116                         <td class="kp-column kp-data"><?php echo number_format($benchmark['memory'] / 1024 / 1024, 2) ?> MB</td> 
    117                 </tr> 
    118 <?php 
    119  
    120 endforeach; 
    121  
    122 ?> 
    123         </table> 
    124 <?php endif; ?> 
    125  
    126 <?php if (isset($queries)): ?> 
    127         <table id="kp-queries"> 
    128                 <tr> 
    129                         <th><?php echo Kohana::lang('profiler.queries') ?></th> 
    130                         <th class="kp-column">Time</th> 
    131                         <th class="kp-column">Rows</th> 
    132                 </tr> 
    133 <?php 
    134  
    135 if ($queries === FALSE): 
    136  
    137 ?> 
    138                 <tr><td colspan="3"><?php echo Kohana::lang('profiler.no_database') ?></td></tr> 
    139 <?php 
    140  
    141 else: 
    142  
    143         if (count($queries) == 0): 
    144  
    145 ?> 
    146                 <tr><td colspan="3"><?php echo Kohana::lang('profiler.no_queries') ?></td></tr> 
    147 <?php 
    148  
    149         else: 
    150                 text::alternate(); 
    151                 $total_time = 0; 
    152                 foreach ($queries as $query): 
    153                         $total_time += $query['time']; 
    154 ?> 
    155                 <tr<?php echo text::alternate('', ' class="kp-altrow"') ?>> 
    156                         <td><?php echo html::specialchars($query['query']) ?></td> 
    157                         <td class="kp-column kp-data"><?php echo number_format($query['time'], 4) ?></td> 
    158                         <td class="kp-column kp-data"><?php echo $query['rows'] ?></td> 
    159                 </tr> 
    160 <?php 
    161  
    162                 endforeach; 
    163 ?> 
    164                 <tr class="kp-totalrow"> 
    165                         <td>Total: <?php echo count($queries) ?></td> 
    166                         <td class="kp-column kp-data"><?php echo number_format($total_time, 4) ?></td> 
    167                         <td class="kp-column kp-data">&nbsp;</td> 
    168                 </tr> 
    169 <?php 
    170  
    171         endif; 
    172 endif; 
    173  
    174 ?> 
    175         </table> 
    176 <?php endif; ?> 
    177  
    178 <?php if (isset($post)): ?> 
    179         <table id="kp-postdata"> 
    180                 <tr> 
    181                         <th colspan="2"><?php echo Kohana::lang('profiler.post_data') ?></th> 
    182                 </tr> 
    183 <?php 
    184  
    185 if (count($_POST) == 0): 
    186  
    187 ?> 
    188                 <tr><td colspan="2"><?php echo Kohana::lang('profiler.no_post') ?></td></tr> 
    189 <?php 
    190  
    191 else: 
    192         text::alternate(); 
    193         foreach ($_POST as $name => $value): 
    194  
    195 ?> 
    196                 <tr<?php echo text::alternate('', ' class="kp-altrow"') ?>> 
    197                         <td class="kp-name"><?php echo $name ?></td> 
    198                         <td> 
    199                                 <?php echo (is_array($value)) ? '<pre>'.html::specialchars(print_r($value, TRUE)).'</pre>' : html::specialchars($value) ?> 
    200                         </td> 
    201                 </tr> 
    202 <?php 
    203  
    204         endforeach; 
    205 endif; 
    206  
    207 ?> 
    208         </table> 
    209 <?php endif; ?> 
    210  
    211 <?php if (isset($session)): ?> 
    212         <table id="kp-sessiondata"> 
    213                 <tr> 
    214                         <th colspan="2"><?php echo Kohana::lang('profiler.session_data') ?></th> 
    215                 </tr> 
    216 <?php 
    217  
    218 if ( ! isset($_SESSION)): 
    219  
    220 ?> 
    221                 <tr><td colspan="2"><?php echo Kohana::lang('profiler.no_session') ?></td></tr> 
    222 <?php 
    223  
    224 else: 
    225         text::alternate(); 
    226         foreach ($_SESSION as $name => $value): 
    227  
    228 ?> 
    229                 <tr<?php echo text::alternate('', ' class="kp-altrow"') ?>> 
    230                         <td class="kp-name"><?php echo $name ?></td> 
    231                         <td> 
    232                                 <?php echo (is_array($value) OR is_object($value)) ? '<pre>'.html::specialchars(print_r($value, TRUE)).'</pre>' : html::specialchars($value) ?> 
    233                         </td> 
    234                 </tr> 
    235 <?php 
    236  
    237         endforeach; 
    238 endif; 
    239  
    240 ?> 
    241         </table> 
    242 <?php endif; ?> 
    243  
    244 <?php if (isset($cookie)): ?> 
    245         <table id="kp-cookiedata"> 
    246                 <tr> 
    247                         <th colspan="2"><?php echo Kohana::lang('profiler.cookie_data') ?></th> 
    248                 </tr> 
    249 <?php 
    250  
    251 if (count($_COOKIE) == 0): 
    252  
    253 ?> 
    254                 <tr><td colspan="2"><?php echo Kohana::lang('profiler.no_cookie') ?></td></tr> 
    255 <?php 
    256  
    257 else: 
    258         text::alternate(); 
    259         foreach ($_COOKIE as $name => $value): 
    260  
    261 ?> 
    262                 <tr<?php echo text::alternate('', ' class="kp-altrow"') ?>> 
    263                         <td class="kp-name"><?php echo $name ?></td> 
    264                         <td> 
    265                                 <?php echo (is_array($value)) ? '<pre>'.html::specialchars(print_r($value, TRUE)).'</pre>' : html::specialchars($value) ?> 
    266                         </td> 
    267                 </tr> 
    268 <?php 
    269  
    270         endforeach; 
    271 endif; 
    272  
    273 ?> 
    274         </table> 
    275 <?php endif; ?> 
    276  
     26Profiler executed in <?php echo number_format($execution_time, 3) ?>s 
    27727</div>