root/trunk/system/libraries/Profiler.php

Revision 2774, 6.6 kB (checked in by Shadowhand, 1 month ago)

Added total rows count to Profiler's database output. Thanks speal!

  • Property svn:eol-style set to LF
  • Property copyright set to Copyright (c) 2007 Kohana Team
  • Property svn:keywords set to Id
Line 
1 <?php defined('SYSPATH') or die('No direct script access.');
2 /**
3  * Adds useful information to the bottom of the current page for debugging and optimization purposes.
4  *
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.
10  *
11  * $Id$
12  *
13  * @package    Profiler
14  * @author     Kohana Team
15  * @copyright  (c) 2007-2008 Kohana Team
16  * @license    http://kohanaphp.com/license.html
17  */
18 class Profiler_Core {
19
20     protected $profiles = array();
21     protected $show;
22
23     public function __construct()
24     {
25         // Add all built in profiles to event
26         Event::add('profiler.run', array($this, 'benchmarks'));
27         Event::add('profiler.run', array($this, 'database'));
28         Event::add('profiler.run', array($this, 'session'));
29         Event::add('profiler.run', array($this, 'post'));
30         Event::add('profiler.run', array($this, 'cookies'));
31
32         // Add profiler to page output automatically
33         Event::add('system.display', array($this, 'render'));
34
35         Log::add('debug', 'Profiler Library initialized');
36     }
37
38     /**
39      * Magic __call method. Creates a new profiler section object.
40      *
41      * @param   string   input type
42      * @param   string   input name
43      * @return  object
44      */
45     public function __call($method, $args)
46     {
47         if ( ! $this->show OR (is_array($this->show) AND ! in_array($args[0], $this->show)))
48             return FALSE;
49
50         // Class name
51         $class = 'Profiler_'.ucfirst($method);
52
53         $class = new $class();
54
55         $this->profiles[$args[0]] = $class;
56
57         return $class;
58     }
59
60     /**
61      * Disables the profiler for this page only.
62      * Best used when profiler is autoloaded.
63      *
64      * @return  void
65      */
66     public function disable()
67     {
68         // Removes itself from the event queue
69         Event::clear('system.display', array($this, 'render'));
70     }
71
72     /**
73      * Render the profiler. Output is added to the bottom of the page by default.
74      *
75      * @param   boolean  return the output if TRUE
76      * @return  void|string
77      */
78     public function render($return = FALSE)
79     {
80         $start = microtime(TRUE);
81
82         $get = isset($_GET['profiler']) ? explode(',', $_GET['profiler']) : array();
83         $this->show = empty($get) ? Config::item('profiler.show') : $get;
84
85         Event::run('profiler.run', $this);
86
87         $styles = '';
88         foreach ($this->profiles as $profile)
89         {
90             $styles .= $profile->styles();
91         }
92
93         // Don't display if there's no profiles
94         if (empty($this->profiles))
95             return;
96
97         // Load the profiler view
98         $data = array
99         (
100             'profiles' => $this->profiles,
101             'styles'   => $styles,
102             'execution_time' => microtime(TRUE) - $start
103         );
104         $view = new View('kohana_profiler', $data);
105
106         // Return rendered view if $return is TRUE
107         if ($return == TRUE)
108             return $view->render();
109
110         // Add profiler data to the output
111         if (stripos(Kohana::$output, '</body>') !== FALSE)
112         {
113             // Closing body tag was found, insert the profiler data before it
114             Kohana::$output = str_ireplace('</body>', $view->render().'</body>', Kohana::$output);
115         }
116         else
117         {
118             // Append the profiler data to the output
119             Kohana::$output .= $view->render();
120         }
121     }
122
123     /**
124      * Benchmark times and memory usage from the Benchmark library.
125      *
126      * @return  void
127      */
128     public function benchmarks()
129     {
130         if ( ! $table = $this->table('benchmarks'))
131             return;
132
133         $table->add_column();
134         $table->add_column('kp-column kp-data');
135         $table->add_column('kp-column kp-data');
136         $table->add_row(array('Benchmarks', 'Time', 'Memory'), 'kp-title', 'background-color: #FFE0E0');
137
138         $benchmarks = Benchmark::get(TRUE);
139
140         // Moves the first benchmark (total execution time) to the end of the array
141         $benchmarks = array_slice($benchmarks, 1) + array_slice($benchmarks, 0, 1);
142
143         text::alternate();
144         foreach ($benchmarks as $name => $benchmark)
145         {
146             // Clean unique id from system benchmark names
147             $name = ucwords(str_replace(array('_', '-'), ' ', str_replace(SYSTEM_BENCHMARK.'_', '', $name)));
148
149             $data = array($name, number_format($benchmark['time'], 3), number_format($benchmark['memory'] / 1024 / 1024, 2).'MB');
150             $class = text::alternate('', 'kp-altrow');
151
152             if ($name == 'Total Execution')
153                 $class = 'kp-totalrow';
154
155             $table->add_row($data, $class);
156         }
157     }
158
159     /**
160      * Database query benchmarks.
161      *
162      * @return  void
163      */
164     public function database()
165     {
166         if ( ! $table = $this->table('database'))
167             return;
168
169         $table->add_column();
170         $table->add_column('kp-column kp-data');
171         $table->add_column('kp-column kp-data');
172         $table->add_row(array('Queries', 'Time', 'Rows'), 'kp-title', 'background-color: #E0FFE0');
173
174         $queries = Database::$benchmarks;
175
176         text::alternate();
177         $total_time = $total_rows = 0;
178         foreach ($queries as $query)
179         {
180             $data = array($query['query'], number_format($query['time'], 3), $query['rows']);
181             $class = text::alternate('', 'kp-altrow');
182             $table->add_row($data, $class);
183             $total_time += $query['time'];
184             $total_rows += $query['rows'];
185         }
186
187         $data = array('Total: ' . count($queries), number_format($total_time, 3), $total_rows);
188         $table->add_row($data, 'kp-totalrow');
189     }
190
191     /**
192      * Session data.
193      *
194      * @return  void
195      */
196     public function session()
197     {
198         if (empty($_SESSION)) return;
199
200         if ( ! $table = $this->table('session'))
201             return;
202
203         $table->add_column('kp-name');
204         $table->add_column();
205         $table->add_row(array('Session', 'Value'), 'kp-title', 'background-color: #CCE8FB');
206
207         text::alternate();
208         foreach($_SESSION as $name => $value)
209         {
210             $data = array($name, $value);
211             $class = text::alternate('', 'kp-altrow');
212             $table->add_row($data, $class);
213         }
214     }
215
216     /**
217      * POST data.
218      *
219      * @return  void
220      */
221     public function post()
222     {
223         if (empty($_POST)) return;
224
225         if ( ! $table = $this->table('post'))
226             return;
227
228         $table->add_column('kp-name');
229         $table->add_column();
230         $table->add_row(array('POST', 'Value'), 'kp-title', 'background-color: #E0E0FF');
231
232         text::alternate();
233         foreach($_POST as $name => $value)
234         {
235             $data = array($name, $value);
236             $class = text::alternate('', 'kp-altrow');
237             $table->add_row($data, $class);
238         }
239     }
240
241     /**
242      * Cookie data.
243      *
244      * @return  void
245      */
246     public function cookies()
247     {
248         if (empty($_COOKIE)) return;
249
250         if ( ! $table = $this->table('cookies'))
251             return;
252
253         $table->add_column('kp-name');
254         $table->add_column();
255         $table->add_row(array('Cookies', 'Value'), 'kp-title', 'background-color: #FFF4D7');
256
257         text::alternate();
258         foreach($_COOKIE as $name => $value)
259         {
260             $data = array($name, $value);
261             $class = text::alternate('', 'kp-altrow');
262             $table->add_row($data, $class);
263         }
264     }
265 }
Note: See TracBrowser for help on using the browser.