-
Notifications
You must be signed in to change notification settings - Fork 5
/
Profiler.php
326 lines (277 loc) · 9.11 KB
/
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
/**
* Port of PHP Quick Profiler by Ryan Campbell
* Original URL: http://particletree.com/features/php-quick-profiler
*/
class Profiler_Profiler {
/**
* Holds log data collected by Profiler_Console
* @var array
*/
public $output = array();
/**
* Holds config data passed inot the constructor
* @var array
*/
public $config = array();
/**
* The list of query types we care about for type specific stats
*
* @var array
*
*/
protected $_queryTypes = array('select', 'update', 'delete', 'insert');
/**
* Sets the configuration options for this object and sets the start time.
*
* Possible configuration options include:
* <ul>
* <li><strong>query_explain_callback:</strong> Callback used to explain queries. Follow format used by call_user_func</li>
* </ul>
*
* @param array $config List of configuration options
* @param int $startTime Time to use as the start time of the profiler
*/
public function __construct(array $config = array(), $startTime = null) {
if (is_null($startTime)) {
$startTime = microtime(true);
}
$this->startTime = $startTime;
$this->config = $config;
}
/**
* Shortcut for setting the callback used to explain queries.
*
* @param string|array $callback
*/
public function setQueryExplainCallback($callback) {
$this->config['query_explain_callback'] = $callback;
}
/**
* Shortcut for setting the callback used to interact with the MySQL
* query profiler.
*
* @param string|array $callback
*/
public function setQueryProfilerCallback($callback) {
$this->config['query_profiler_callback'] = $callback;
}
/**
* Collects and aggregates data recorded by Profiler_Console.
*/
public function gatherConsoleData() {
$logs = Profiler_Console::getLogs();
foreach ($logs as $type => $data) {
// Console data will already be properly formatted.
if ($type == 'console') {
continue;
}
// Ignore empty message lists
if (!$data['count']) {
continue;
}
foreach ($data['messages'] as $message) {
$data = $message;
switch ($type) {
case 'logs':
$data['type'] = 'log';
$data['data'] = print_r($message['data'], true);
break;
case 'memory':
$data['type'] = 'memory';
$data['data'] = $this->getReadableFileSize($data['data']);
break;
case 'speed':
$data['type'] = 'speed';
$data['data'] = $this->getReadableTime(($message['data'] - $this->startTime) * 1000);
break;
case 'benchmarks':
$data['type'] = 'benchmark';
$data['data'] = $this->getReadableTime($message['end_time'] - $message['start_time']);
break;
}
if (isset($data['type'])) {
$logs['console']['messages'][] = $data;
}
}
}
$this->output['logs'] = $logs;
}
/**
* Gathers and aggregates data on included files such as size
*/
public function gatherFileData() {
$files = get_included_files();
$fileList = array();
$fileTotals = array('count' => count($files), 'size' => 0, 'largest' => 0);
foreach($files as $key => $file) {
$size = filesize($file);
$fileList[] = array('name' => $file, 'size' => $this->getReadableFileSize($size));
$fileTotals['size'] += $size;
if ($size > $fileTotals['largest']) {
$fileTotals['largest'] = $size;
}
}
$fileTotals['size'] = $this->getReadableFileSize($fileTotals['size']);
$fileTotals['largest'] = $this->getReadableFileSize($fileTotals['largest']);
$this->output['files'] = $fileList;
$this->output['fileTotals'] = $fileTotals;
}
/**
* Gets the peak memory usage the configured memory limit
*/
public function gatherMemoryData() {
$memoryTotals = array();
$memoryTotals['used'] = $this->getReadableFileSize(memory_get_peak_usage());
$memoryTotals['total'] = ini_get('memory_limit');
$this->output['memoryTotals'] = $memoryTotals;
}
/**
* Gathers and aggregates data regarding executed queries
*/
public function gatherQueryData() {
$queries = array();
$type_default = array('total' => 0, 'time' => 0, 'percentage' => 0, 'time_percentage' => 0);
$types = array('select' => $type_default, 'update' => $type_default, 'insert' => $type_default, 'delete' => $type_default);
$queryTotals = array('all' => 0, 'count' => 0, 'time' => 0, 'duplicates' => 0, 'types' => $types);
foreach($this->output['logs']['queries']['messages'] as $entries) {
if (count($entries) > 1) {
$queryTotals['duplicates'] += 1;
}
$queryTotals['count'] += 1;
foreach ($entries as $i => $log) {
if (isset($log['end_time'])) {
$query = array('sql' => $log['sql'],
'explain' => $log['explain'],
'time' => ($log['end_time'] - $log['start_time']),
'duplicate' => $i > 0 ? true : false);
// Lets figure out the type of query for our counts
$trimmed = trim($log['sql']);
$type = strtolower(substr($trimmed, 0, strpos($trimmed, ' ')));
if (in_array($type, $this->_queryTypes) && isset($queryTotals['types'][$type])) {
$queryTotals['types'][$type]['total'] += 1;
$queryTotals['types'][$type]['time'] += $query['time'];
}
// Need to get total times and a readable format of our query time
$queryTotals['time'] += $query['time'];
$queryTotals['all'] += 1;
$query['time'] = $this->getReadableTime($query['time']);
// If an explain callback is setup try to get the explain data
if (isset($this->_queryTypes[$type]) && isset($this->config['query_explain_callback']) && !empty($this->config['query_explain_callback'])) {
$query['explain'] = $this->_attemptToExplainQuery($query['sql']);
}
// If a query profiler callback is setup get the profiler data
if (isset($this->config['query_profiler_callback']) && !empty($this->config['query_profiler_callback'])) {
$query['profile'] = $this->_attemptToProfileQuery($query['sql']);
}
$queries[] = $query;
}
}
}
// Go through the type totals and calculate percentages
foreach ($queryTotals['types'] as $type => $stats) {
$total_perc = !$stats['total'] ? 0 : round(($stats['total'] / $queryTotals['count']) * 100, 2);
$time_perc = !$stats['time'] ? 0 : round(($stats['time'] / $queryTotals['time']) * 100, 2);
$queryTotals['types'][$type]['percentage'] = $total_perc;
$queryTotals['types'][$type]['time_percentage'] = $time_perc;
$queryTotals['types'][$type]['time'] = $this->getReadableTime($queryTotals['types'][$type]['time']);
}
$queryTotals['time'] = $this->getReadableTime($queryTotals['time']);
$this->output['queries'] = $queries;
$this->output['queryTotals'] = $queryTotals;
}
/**
* Calculates the execution time from the start of profiling to *now* and
* collects the congirued maximum execution time.
*/
public function gatherSpeedData() {
$speedTotals = array();
$speedTotals['total'] = $this->getReadableTime((microtime(true) - $this->startTime)*1000);
$speedTotals['allowed'] = ini_get('max_execution_time');
$this->output['speedTotals'] = $speedTotals;
}
/**
* Converts a number of bytes to a more readable format
* @param int $size The number of bytes
* @param string $retstring The format of the return string
* @return string
*/
public function getReadableFileSize($size, $retString = null) {
$sizes = array('bytes', 'kB', 'MB', 'GB', 'TB');
if ($retString === null) {
$retString = '%01.2f %s';
}
$lastSizeString = end($sizes);
foreach ($sizes as $sizeString) {
if ($size < 1024) {
break;
}
if ($sizeString != $lastSizeString) {
$size /= 1024;
}
}
if ($sizeString == $sizes[0]) {
$retString = '%01d %s';
}
return sprintf($retString, $size, $sizeString);
}
/**
* Converts a small time format (fractions of a millisecond) to a more readable format
* @param float $time
* @return int
*/
public function getReadableTime($time) {
$ret = $time;
$formatter = 0;
$formats = array('ms', 's', 'm');
if ($time >= 1000 && $time < 60000) {
$formatter = 1;
$ret = ($time / 1000);
}
if ($time >= 60000) {
$formatter = 2;
$ret = ($time / 1000) / 60;
}
$ret = number_format($ret, 3, '.', '') . ' ' . $formats[$formatter];
return $ret;
}
/**
* Collects data from the console and performs various calculations on it before
* displaying the console on screen.
*/
public function display() {
$this->gatherConsoleData();
$this->gatherFileData();
$this->gatherMemoryData();
$this->gatherQueryData();
$this->gatherSpeedData();
Profiler_Display::display($this->output, $this->config);
}
/**
* Used with a callback to allow integration into DAL's to explain an executed query.
*
* @param string $sql The query that is being explained
* @return array
*/
protected function _attemptToExplainQuery($sql) {
try {
$sql = 'EXPLAIN ' . $sql;
return call_user_func_array($this->config['query_explain_callback'], $sql);
} catch (Exception $e) {
return array();
}
}
/**
* Used with a callback to allow integration into DAL's to profiler an execute query.
*
* @param string $sql The query being profiled
* @return array
*/
protected function _attemptToProfileQuery($sql) {
try {
return call_user_func_array($this->config['query_profiler_callback'], $sql);
} catch (Exception $e) {
return array();
}
}
}