-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFire_Profiler.php
150 lines (124 loc) · 3.63 KB
/
Fire_Profiler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
class Fire_Profiler extends FirePHP{
public function __construct(){
self::init();
// Add all built in profiles to event
Event::add('fire-profiler.run', array($this, 'benchmarks'));
Event::add('fire-profiler.run', array($this, 'database'));
Event::add('fire-profiler.run', array($this, 'session'));
Event::add('fire-profiler.run', array($this, 'post'));
Event::add('fire-profiler.run', array($this, 'cookies'));
// Add profiler to page output automatically
Event::add('system.display', array($this, 'render'));
}
/**
* Disables the profiler for this page only.
* Best used when profiler is autoloaded.
*
* @return void
*/
public function disable()
{
// Removes itself from the event queue
Event::clear('system.display', array($this, 'render'));
}
/**
* Render the profiler. Output is added to FirePHP
*
* @param boolean return the output if TRUE
* @return void|string
*/
public function render(){
Event::run('fire-profiler.run', $this);
}
/**
* Benchmark times and memory usage from the Benchmark library.
*
* @return void
*/
public function database(){
$queries = Database::$benchmarks;
$total_time = $total_rows = 0;
$table = array();
$table[] = array('SQL Statement','Time','Rows');
foreach ($queries as $query)
{
$table[]=array(str_replace("\n",' ',$query['query']), number_format($query['time'], 3), $query['rows']);
$total_time += $query['time'];
$total_rows += $query['rows'];
}
$this->fb(array(count($queries).' SQL queries took '.number_format($total_time,3).' seconds and returned '.$total_rows.' rows', $table
),FirePHP::TABLE);
}
/**
* Database query benchmarks.
*
* @return void
*/
public function benchmarks(){
$benchmarks = Benchmark::get(TRUE);
// Moves the first benchmark (total execution time) to the end of the array
$benchmarks = array_slice($benchmarks, 1) + array_slice($benchmarks, 0, 1);
$table = array();
$table[] = array('Benchmark','Time','Memory');
foreach ($benchmarks as $name => $benchmark)
{
// Clean unique id from system benchmark names
$name = ucwords(str_replace(array('_', '-'), ' ', str_replace(SYSTEM_BENCHMARK.'_', '', $name)));
$table[]= array($name, number_format($benchmark['time'], 3), number_format($benchmark['memory'] / 1024 / 1024, 2).'MB');
}
$this->fb(array(count($benchmarks).' benchmarks took '.number_format($benchmark['time'], 3).' seconds and used up '. number_format($benchmark['memory'] / 1024 / 1024, 2).'MB'.' memory', $table
),FirePHP::TABLE);
}
/**
* Session data.
*
* @return void
*/
public function session()
{
if (empty($_SESSION)) return;
$table = array();
$table[] = array('Session','Value');
foreach($_SESSION as $name => $value)
{
if (is_object($value))
{
$value = get_class($value).' [object]';
}
$table[] = array($name, $value);
}
$this->fb(array('Session: '.count($_SESSION).' session variables', $table ),FirePHP::TABLE);
}
/**
* Cookie data.
*
* @return void
*/
public function cookies()
{
$table = array();
$table[] = array('Cookies','Value');
foreach($_COOKIE as $name => $value)
{
$table[] = array($name, $value);
}
$this->fb(array('Cookies: '.count($_COOKIE).' cookies', $table ),FirePHP::TABLE);
}
/**
* POST data.
*
* @return void
*/
public function post()
{
if (empty($_POST)) return;
$table = array();
$table[] = array('POST','Value');
foreach($_POST as $name => $value)
{
$table[] = array($name, $value);
}
$this->fb(array('Post: '.count($_POST).' POST variables', $table ),FirePHP::TABLE);
}
}