Removed dep on API
[ninja.git] / system / libraries / Profiler.php
blob9deaa8a24812e11916c89970115e2b840bd3281c
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3 * Adds useful information to the bottom of the current page for debugging and optimization purposes.
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.
11 * $Id: Profiler.php 3917 2009-01-21 03:06:22Z zombor $
13 * @package Profiler
14 * @author Kohana Team
15 * @copyright (c) 2007-2008 Kohana Team
16 * @license http://kohanaphp.com/license.html
18 class Profiler {
20 protected $profiles = array();
21 protected $show;
23 public function __construct()
25 if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
26 Kohana::log('debug', 'Skipping init of Profiler, since the current request is ajaxish');
27 return;
29 // Add all built in profiles to event
30 Event::add('profiler.run', array($this, 'benchmarks'));
31 Event::add('profiler.run', array($this, 'database'));
32 Event::add('profiler.run', array($this, 'session'));
33 Event::add('profiler.run', array($this, 'post'));
34 Event::add('profiler.run', array($this, 'cookies'));
36 // Add profiler to page output automatically
37 Event::add('system.display', array($this, 'render'));
40 /**
41 * Magic __call method. Creates a new profiler section object.
43 * @param string input type
44 * @param string input name
45 * @return object
47 public function __call($method, $args)
49 if ( ! $this->show OR (is_array($this->show) AND ! in_array($args[0], $this->show)))
50 return FALSE;
52 // Class name
53 $class = 'Profiler_'.ucfirst($method);
55 $class = new $class();
57 $this->profiles[$args[0]] = $class;
59 return $class;
62 /**
63 * Disables the profiler for this page only.
64 * Best used when profiler is autoloaded.
66 * @return void
68 public function disable()
70 // Removes itself from the event queue
71 Event::clear('system.display', array($this, 'render'));
74 /**
75 * Render the profiler. Output is added to the bottom of the page by default.
77 * @param boolean return the output if TRUE
78 * @return void|string
80 public function render($return = FALSE)
82 $start = microtime(TRUE);
84 $get = isset($_GET['profiler']) ? explode(',', $_GET['profiler']) : array();
85 $this->show = empty($get) ? Kohana::config('profiler.show') : $get;
87 Event::run('profiler.run', $this);
89 $styles = '';
90 foreach ($this->profiles as $profile)
92 $styles .= $profile->styles();
95 // Don't display if there's no profiles
96 if (empty($this->profiles))
97 return;
99 // Load the profiler view
100 $data = array
102 'profiles' => $this->profiles,
103 'styles' => $styles,
104 'execution_time' => microtime(TRUE) - $start
106 $view = new View('kohana_profiler', $data);
108 // Return rendered view if $return is TRUE
109 if ($return == TRUE)
110 return $view->render();
112 // Add profiler data to the output
113 if (stripos(Kohana::$output, '</body>') !== FALSE)
115 // Closing body tag was found, insert the profiler data before it
116 Kohana::$output = str_ireplace('</body>', $view->render().'</body>', Kohana::$output);
118 else
120 // Append the profiler data to the output
121 Kohana::$output .= $view->render();
126 * Benchmark times and memory usage from the Benchmark library.
128 * @return void
130 public function benchmarks()
132 if ( ! $table = $this->table('benchmarks'))
133 return;
135 $table->add_column();
136 $table->add_column('kp-column kp-data');
137 $table->add_column('kp-column kp-data');
138 $table->add_row(array('Benchmarks', 'Time', 'Memory'), 'kp-title', 'background-color: #FFE0E0');
140 $benchmarks = Benchmark::get(TRUE);
142 // Moves the first benchmark (total execution time) to the end of the array
143 $benchmarks = array_slice($benchmarks, 1) + array_slice($benchmarks, 0, 1);
145 text::alternate();
146 foreach ($benchmarks as $name => $benchmark)
148 // Clean unique id from system benchmark names
149 $name = ucwords(str_replace(array('_', '-'), ' ', str_replace(SYSTEM_BENCHMARK.'_', '', $name)));
151 $data = array($name, number_format($benchmark['time'], 3), number_format($benchmark['memory'] / 1024 / 1024, 2).'MB');
152 $class = text::alternate('', 'kp-altrow');
154 if ($name == 'Total Execution')
155 $class = 'kp-totalrow';
157 $table->add_row($data, $class);
162 * Database query benchmarks.
164 * @return void
166 public function database()
168 if ( ! $table = $this->table('database'))
169 return;
171 $table->add_column();
172 $table->add_column('kp-column kp-data');
173 $table->add_column('kp-column kp-data');
174 $table->add_row(array('Queries', 'Time', 'Rows'), 'kp-title', 'background-color: #E0FFE0');
176 $queries = Database::$benchmarks;
178 text::alternate();
179 $total_time = $total_rows = 0;
180 foreach ($queries as $query)
182 $data = array($query['query'], number_format($query['time'], 3), $query['rows']);
183 $class = text::alternate('', 'kp-altrow');
184 $table->add_row($data, $class);
185 $total_time += $query['time'];
186 $total_rows += $query['rows'];
189 $data = array('Total: ' . count($queries), number_format($total_time, 3), $total_rows);
190 $table->add_row($data, 'kp-totalrow');
194 * Session data.
196 * @return void
198 public function session()
200 if (empty($_SESSION)) return;
202 if ( ! $table = $this->table('session'))
203 return;
205 $table->add_column('kp-name');
206 $table->add_column();
207 $table->add_row(array('Session', 'Value'), 'kp-title', 'background-color: #CCE8FB');
209 text::alternate();
210 foreach($_SESSION as $name => $value)
212 if (is_object($value))
214 $value = get_class($value).' [object]';
217 $data = array($name, $value);
218 $class = text::alternate('', 'kp-altrow');
219 $table->add_row($data, $class);
224 * POST data.
226 * @return void
228 public function post()
230 if (empty($_POST)) return;
232 if ( ! $table = $this->table('post'))
233 return;
235 $table->add_column('kp-name');
236 $table->add_column();
237 $table->add_row(array('POST', 'Value'), 'kp-title', 'background-color: #E0E0FF');
239 text::alternate();
240 foreach($_POST as $name => $value)
242 $data = array($name, $value);
243 $class = text::alternate('', 'kp-altrow');
244 $table->add_row($data, $class);
249 * Cookie data.
251 * @return void
253 public function cookies()
255 if (empty($_COOKIE)) return;
257 if ( ! $table = $this->table('cookies'))
258 return;
260 $table->add_column('kp-name');
261 $table->add_column();
262 $table->add_row(array('Cookies', 'Value'), 'kp-title', 'background-color: #FFF4D7');
264 text::alternate();
265 foreach($_COOKIE as $name => $value)
267 $data = array($name, $value);
268 $class = text::alternate('', 'kp-altrow');
269 $table->add_row($data, $class);