Changeset 658

Show
Ignore:
Timestamp:
10/05/2007 01:15:08 AM (12 months ago)
Author:
Shadowhand
Message:

Cleaned up Profiler and related files.

Location:
trunk
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • trunk/application/config/config.php

    r651 r658  
    133133$config['autoload'] = array 
    134134( 
    135         'libraries' => '', 
     135        'libraries' => 'profiler', 
    136136        'models'    => '' 
    137137); 
  • trunk/application/config/database.php

    r647 r658  
    1010 * 
    1111 * @param boolean show_errors   - Enable or disable database exceptions 
     12 * @param boolean benchmark     - Enable or disable database benchmarking 
    1213 * @param boolean persistent    - Enable or disable a persistent connection 
    1314 * @param string  connection    - DSN identifier: driver://user:password@server/database 
     
    2021( 
    2122        'show_errors'   => TRUE, 
     23        'benchmark'     => TRUE, 
    2224        'persistent'    => FALSE, 
    2325        'connection'    => 'mysql://dbuser:secret@localhost/kohana', 
  • trunk/system/core/Benchmark.php

    r657 r658  
    4949        public static function get($name, $decimals = 4) 
    5050        { 
     51                $total = FALSE; 
     52 
    5153                if (isset(self::$marks[$name])) 
    5254                { 
     
    5658                        } 
    5759 
    58                         return number_format(self::$marks[$name]['stop'] - self::$marks[$name]['start'], $decimals); 
     60                        $total = number_format(self::$marks[$name]['stop'] - self::$marks[$name]['start'], $decimals); 
    5961                } 
     62 
     63                return $total; 
    6064        } 
    6165 
     
    7074        { 
    7175                $benchmarks = array(); 
    72                 foreach (self::$marks as $name => $times) 
     76 
     77                foreach (array_keys(self::$marks) as $name) 
    7378                { 
    7479                        $benchmarks[$name] = self::get($name, $decimals); 
  • trunk/system/libraries/Database.php

    r655 r658  
    3030class Database_Core { 
    3131 
     32        // Global benchmark 
     33        public static $benchmarks = array(); 
     34 
    3235        // Configuration 
    33         protected $config  = array 
     36        protected $config = array 
    3437        ( 
     38                'show_errors'   => TRUE, 
     39                'benchmark'     => TRUE, 
     40                'persistent'    => FALSE, 
    3541                'connection'    => '', 
    36                 'persistent'    => FALSE, 
    37                 'show_errors'   => TRUE, 
    3842                'character_set' => 'utf8', 
    39                 'table_prefix'  => '' 
     43                'table_prefix'  => '', 
     44                'object'        => TRUE 
    4045        ); 
    4146 
     
    5964        protected $connected  = FALSE; 
    6065        protected $last_query = ''; 
    61         public $benchmark  = array(); 
    6266 
    6367        /** 
     
    474478                $stop = microtime(TRUE); 
    475479 
    476                 // benchmark the query 
    477                 $this->benchmark[] = array('query' => $sql, 'time' => $stop - $start); 
     480                if ($this->config['benchmark'] == TRUE) 
     481                { 
     482                        // Benchmark the query 
     483                        self::$benchmarks[] = array('query' => $sql, 'time' => $stop - $start); 
     484                } 
    478485 
    479486                $this->reset_select(); 
  • trunk/system/libraries/Profiler.php

    • Property svn:copyright set to Copyright (c) 2007 Kohana Team
    • Property svn:eol-style set to LF
    • Property svn:keywords set to Id
    r654 r658  
    1212 * @since            Version 2.0 
    1313 * @filesource 
     14 * 
     15 * $Id$ 
    1416 */ 
    1517 
     
    2628 * @link        http://kohanaphp.com/user_guide/general/profiling.html 
    2729 */ 
     30class Profiler_Core { 
    2831 
    29 class Profiler_Core 
    30 { 
     32        public function __construct() 
     33        { 
     34                // Add profiler to page output automatically 
     35                Event::add('system.output', array($this, 'render')); 
    3136 
    32         private $core; 
    33  
    34         public function __construct() 
    35         { 
    36                 // Add profiler to page output automatically 
    37                 Event::add('system.output', array($this, 'render')); 
    38  
    39                 $this->core = Kohana::instance(); 
    40                  
    41                 Log::add('debug', 'Profiler initialized'); 
    42         } 
     37                Log::add('debug', 'Profiler Library initialized'); 
     38        } 
    4339 
    4440        /** 
     
    5046        public function render() 
    5147        { 
    52                 // Get list of benchmarks 
    53                 $benchmarks = Benchmark::get_all(); 
     48                $data = array 
     49                ( 
     50                        'benchmarks' => array(), 
     51                        'queries'    => FALSE 
     52                ); 
    5453 
    5554                // Clean unique id from system benchmark names 
    56                 $renamed_bm = array(); 
    57                 foreach ($benchmarks as $name => $time) 
     55                foreach (Benchmark::get_all() as $name => $time) 
    5856                { 
    59                         $name              = str_replace(SYSTEM_BENCHMARK.'_', '', $name); 
    60                         $renamed_bm[$name] = $time; 
     57                        $data['benchmarks'][str_replace(SYSTEM_BENCHMARK.'_', '', $name)] = $time; 
    6158                } 
    6259 
    63                 $data['benchmarks'] = $renamed_bm; 
    64  
    6560                // Get database queries 
    66                 $data['db_exists'] = FALSE; 
    67                 $data['queries']   = array(); 
    68                 if (isset($this->core->db)) 
     61                if (isset(Kohana::instance()->db)) 
    6962                { 
    70                         $data['db_exists'] = TRUE; 
    71                         $data['queries']   = $this->core->db->benchmark; 
     63                        $data['queries'] = Database::$benchmarks; 
    7264                } 
    7365 
    74                 // Get profiler view ready to add to output 
     66                // Load the profiler view 
    7567                $view = new View('kohana_profiler', $data); 
    7668 
    77                 // Get page output 
    78                 $output = Kohana::$output; 
    79  
    80                 if (preg_match("|</body>.*?</html>|is", $output)) 
     69                // Add profiler data to the output 
     70                if (strpos('</body>', Kohana::$output) !== FALSE) 
    8171                { 
    82                         // Output has closing body and html tags, put profiler before them 
    83                         $output  = preg_replace("|</body>.*?</html>|is", '', $output); 
    84                         $output .= $view; 
    85                         $output .= '</body></html>'; 
     72                        // Closing body tag was found, insert the profiler data before it 
     73                        Kohana::$output = str_replace('</body>', $view.'</body>', Kohana::$output); 
    8674                } 
    8775                else 
    8876                { 
    89                         // No closing tags, just add profiler to the end 
    90                         $output .= $view; 
     77                        // Append the profiler data to the output 
     78                        Kohana::$output .= $view; 
    9179                } 
    92  
    93                 // Set new page output 
    94                 Kohana::$output = $output; 
    9580        } 
    9681 
  • trunk/system/views/kohana_profiler.php

    r654 r658  
    11<style type="text/css"> 
    2         #kohana-profiler 
    3         { 
    4                 font-family: Courier New; 
    5                 background-color: #F8FFF8; 
    6                 margin-top: 20px; 
    7                 clear: both; 
    8                 padding: 10px; 
    9                 border: 1px solid #E5EFF8; 
    10         } 
    11         #kohana-profiler table 
    12         { 
    13                 font-size: 1.0em; 
    14                 color: #4D6171; 
    15                 width: 100%; 
    16                 border-collapse: collapse; 
    17                 border-top: 1px solid #E5EFF8; 
    18                 border-right: 1px solid #E5EFF8; 
    19                 border-left: 1px solid #E5EFF8; 
    20                 margin-bottom: 10px; 
    21         } 
    22         #kohana-profiler th 
    23         { 
    24                 text-align: left; 
    25                 border-bottom: 1px solid #E5EFF8; 
    26                 background-color: #F9FCFE; 
    27                 padding: 3px; 
    28                 color: #263038; 
    29         } 
    30         #kohana-profiler td 
    31         { 
    32                 background-color: #FFFFFF; 
    33                 border-bottom: 1px solid #E5EFF8; 
    34                 padding: 3px; 
    35         } 
    36         #kohana-profiler .kp-altrow td 
    37         { 
    38                 background-color: #F7FBFF; 
    39         } 
    40         #kp-benchmarks th 
    41         { 
    42                 background-color: #FFE0E0; 
    43         } 
    44         #kp-queries th 
    45         { 
    46                 background-color: #E0FFE0; 
    47         } 
    48         #kp-postdata th 
    49         { 
    50                 background-color: #E0E0FF; 
    51         } 
    52         #kohana-profiler .kp-time 
    53         { 
    54                 width: 100px; 
    55                 background-color: #FAFAFB !important; 
    56                 border-left: 1px solid #E5EFF8; 
    57                 text-align: center; 
    58         } 
    59         #kohana-profiler .kp-postname 
    60         { 
    61                 width: 200px; 
    62                 background-color: #FAFAFB !important; 
    63                 border-right: 1px solid #E5EFF8; 
    64         } 
     2#kohana-profiler 
     3{ 
     4        font-family: 'Courier New'; 
     5        background-color: #F8FFF8; 
     6        margin-top: 20px; 
     7        clear: both; 
     8        padding: 10px; 
     9        border: 1px solid #E5EFF8; 
     10} 
     11#kohana-profiler table 
     12{ 
     13        font-size: 1.0em; 
     14        color: #4D6171; 
     15        width: 100%; 
     16        border-collapse: collapse; 
     17        border-top: 1px solid #E5EFF8; 
     18        border-right: 1px solid #E5EFF8; 
     19        border-left: 1px solid #E5EFF8; 
     20        margin-bottom: 10px; 
     21} 
     22#kohana-profiler th 
     23{ 
     24        text-align: left; 
     25        border-bottom: 1px solid #E5EFF8; 
     26        background-color: #F9FCFE; 
     27        padding: 3px; 
     28        color: #263038; 
     29} 
     30#kohana-profiler td 
     31{ 
     32        background-color: #FFFFFF; 
     33        border-bottom: 1px solid #E5EFF8; 
     34        padding: 3px; 
     35} 
     36#kohana-profiler .kp-altrow td 
     37{ 
     38        background-color: #F7FBFF; 
     39} 
     40#kp-benchmarks th 
     41{ 
     42        background-color: #FFE0E0; 
     43} 
     44#kp-queries th 
     45{ 
     46        background-color: #E0FFE0; 
     47} 
     48#kp-postdata th 
     49{ 
     50        background-color: #E0E0FF; 
     51} 
     52#kohana-profiler .kp-time 
     53{ 
     54        width: 100px; 
     55        background-color: #FAFAFB !important; 
     56        border-left: 1px solid #E5EFF8; 
     57        text-align: center; 
     58} 
     59#kohana-profiler .kp-postname 
     60{ 
     61        width: 200px; 
     62        background-color: #FAFAFB !important; 
     63        border-right: 1px solid #E5EFF8; 
     64} 
    6565</style> 
    6666<div id="kohana-profiler"> 
     
    7070                        <th colspan="2"><?php echo Kohana::lang('profiler.benchmarks') ?></th> 
    7171                </tr> 
    72                 <?php 
    73                 $count = 0; 
    74                 foreach ($benchmarks as $name => $time) 
    75                 { 
    76                         $name = ucwords(str_replace(array('_', '-'), ' ', $name)); 
    77                         ?> 
    78                         <tr<?php if ($count % 2) echo ' class="kp-altrow"'; ?>> 
     72<?php 
     73 
     74$count = 0; 
     75foreach ($benchmarks as $name => $time): 
     76 
     77        $name = ucwords(str_replace(array('_', '-'), ' ', $name)); 
     78 
     79?> 
     80                        <tr<?php if ($count++ % 2): ?> class="kp-altrow"<?php endif; ?>> 
    7981                                <td><?php echo $name ?></td> 
    8082                                <td class="kp-time"><?php echo $time ?></td> 
    8183                        </tr> 
    82                         <?php 
    83                         $count++; 
    84                 } 
    85                 ?> 
     84<?php 
     85 
     86endforeach; 
     87 
     88?> 
    8689        </table> 
    8790 
    8891        <table id="kp-queries"> 
    8992                <tr> 
    90                         <th colspan="2"><?php echo Kohana::lang('profiler.queries') ?> (<?php echo count($queries) ?>)</th> 
     93                        <th colspan="2"><?php echo Kohana::lang('profiler.queries') ?> (<?php echo ($queries == FALSE ? '0' : count($queries)) ?>)</th> 
    9194                </tr> 
    92                 <?php 
    93                 if ( ! $db_exists) 
    94                 { 
    95                         ?> 
    96                         <tr><td colspan="2"><?php echo Kohana::lang('profiler.no_database') ?></td></tr> 
    97                         <?php 
    98                 } 
    99                 else 
    100                 { 
    101                         if (count($queries) == 0) 
    102                         { 
    103                                 ?> 
    104                                 <tr><td colspan="2"><?php echo Kohana::lang('profiler.no_queries') ?></td></tr> 
    105                                 <?php 
    106                         } 
    107                         else 
    108                         { 
    109                                 $count = 0; 
    110                                 foreach ($queries as $query) 
    111                                 { 
    112                                         ?> 
    113                                         <tr<?php if ($count % 2) echo ' class="kp-altrow"'; ?>> 
    114                                                 <td><?php echo htmlspecialchars($query['query']) ?></td> 
    115                                                 <td class="kp-time"><?php echo number_format($query['time'], 4) ?></td> 
    116                                         </tr> 
    117                                         <?php 
    118                                         $count++; 
    119                                 } 
    120                         } 
    121                 } 
    122                 ?> 
     95<?php 
     96 
     97if ($queries === FALSE): 
     98 
     99?> 
     100                <tr><td colspan="2"><?php echo Kohana::lang('profiler.no_database') ?></td></tr> 
     101<?php 
     102 
     103else: 
     104 
     105        if (count($queries) == 0): 
     106 
     107?> 
     108                <tr><td colspan="2"><?php echo Kohana::lang('profiler.no_queries') ?></td></tr> 
     109<?php 
     110 
     111        else: 
     112                $count = 0; 
     113                foreach($queries as $query): 
     114 
     115?> 
     116                <tr<?php if ($count++ % 2): ?> class="kp-altrow"<?php endif; ?>> 
     117                        <td><?php echo htmlspecialchars($query['query']) ?></td> 
     118                        <td class="kp-time"><?php echo number_format($query['time'], 4) ?></td> 
     119                </tr> 
     120<?php 
     121 
     122                endforeach; 
     123        endif; 
     124endif; 
     125 
     126?> 
    123127        </table> 
    124128 
     
    127131                        <th colspan="2"><?php echo Kohana::lang('profiler.post_data') ?></th> 
    128132                </tr> 
    129                 <?php 
    130                 if (count($_POST) == 0) 
    131                 { 
    132                         ?> 
    133                         <tr><td colspan="2"><?php echo Kohana::lang('profiler.no_post') ?></td></tr> 
    134                         <?php 
    135                 } 
    136                 else 
    137                 { 
    138                         $count = 0; 
    139                         foreach ($_POST as $name => $value) 
    140                         { 
    141                                 ?> 
    142                                 <tr<?php if ($count % 2) echo ' class="kp-altrow"'; ?>> 
    143                                         <td class="kp-postname"><?php echo $name ?></td> 
    144                                         <td> 
    145                                                 <?php 
    146                                                 if (is_array($value)) 
    147                                                 { 
    148                                                         echo '<pre>' . htmlspecialchars(print_r($value, true)) . '</pre>'; 
    149                                                 } 
    150                                                 else 
    151                                                 { 
    152                                                         echo htmlspecialchars($value); 
    153                                                 } 
    154                                                 ?> 
    155                                         </td> 
    156                                 </tr> 
    157                                 <?php 
    158                                 $count++; 
    159                         } 
    160                 } 
    161                 ?> 
     133<?php 
     134 
     135if (count($_POST) == 0): 
     136 
     137?> 
     138                <tr><td colspan="2"><?php echo Kohana::lang('profiler.no_post') ?></td></tr> 
     139<?php 
     140 
     141else: 
     142        $count = 0; 
     143        foreach($_POST as $name => $value): 
     144 
     145?> 
     146                <tr<?php if ($count++ % 2): ?> class="kp-altrow" <?php endif; ?>> 
     147                        <td class="kp-postname"><?php echo $name ?></td> 
     148                        <td> 
     149                                <pre><?php echo htmlspecialchars(is_array($value) ? print_r($value, TRUE) : $value) ?></pre> 
     150                        </td> 
     151                </tr> 
     152<?php 
     153 
     154        endforeach; 
     155endif; 
     156 
     157?> 
    162158        </table> 
    163159