|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/***************************************** |
|
4
|
|
|
* Title : Php Quick Profiler Display Class |
|
5
|
|
|
* Author : Created by Ryan Campbell |
|
6
|
|
|
* URL : http://particletree.com/features/php-quick-profiler/ |
|
7
|
|
|
* Description : This is a hacky way of pushing profiling logic to the |
|
8
|
|
|
* PQP HTML. This is great because it will just work in your project, |
|
9
|
|
|
* but it is hard to maintain and read. |
|
10
|
|
|
*****************************************/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Particletree\Pqp; |
|
13
|
|
|
|
|
14
|
|
|
class Display |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** @var array */ |
|
18
|
|
|
protected $defaults = array( |
|
19
|
|
|
'script_path' => 'asset/script.js', |
|
20
|
|
|
'style_path' => 'asset/style.css' |
|
21
|
|
|
); |
|
22
|
|
|
|
|
23
|
|
|
/** @var array */ |
|
24
|
|
|
protected $options; |
|
25
|
|
|
|
|
26
|
|
|
/** @var array */ |
|
27
|
|
|
protected $output; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param array $options |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(array $options = array()) |
|
33
|
|
|
{ |
|
34
|
|
|
$options = array_intersect_key($options, $this->defaults); |
|
35
|
|
|
$this->options = array_replace($this->defaults, $options); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function setConsole(Console $console) |
|
39
|
|
|
{ |
|
40
|
|
|
$console_data = array( |
|
41
|
|
|
'messages' => array(), |
|
42
|
|
|
'meta' => array( |
|
43
|
|
|
'log' => 0, |
|
44
|
|
|
'memory' => 0, |
|
45
|
|
|
'error' => 0, |
|
46
|
|
|
'speed' => 0 |
|
47
|
|
|
) |
|
48
|
|
|
); |
|
49
|
|
|
foreach ($console->getLogs() as $log) { |
|
50
|
|
|
switch($log['type']) { |
|
51
|
|
View Code Duplication |
case 'log': |
|
|
|
|
|
|
52
|
|
|
$message = array( |
|
53
|
|
|
'message' => print_r($log['data'], true), |
|
54
|
|
|
'type' => 'log' |
|
55
|
|
|
); |
|
56
|
|
|
$console_data['meta']['log']++; |
|
57
|
|
|
break; |
|
58
|
|
|
case 'memory': |
|
59
|
|
|
$message = array( |
|
60
|
|
|
'message' => (!empty($log['data_type']) ? "{$log['data_type']}: " : '') . $log['name'], |
|
61
|
|
|
'data' => self::getReadableMemory($log['data']), |
|
62
|
|
|
'type' => 'memory' |
|
63
|
|
|
); |
|
64
|
|
|
$console_data['meta']['memory']++; |
|
65
|
|
|
break; |
|
66
|
|
|
case 'error': |
|
67
|
|
|
$message = array( |
|
68
|
|
|
'message' => "Line {$log['line']}: {$log['data']} in {$log['file']}", |
|
69
|
|
|
'type' => 'error' |
|
70
|
|
|
); |
|
71
|
|
|
$console_data['meta']['error']++; |
|
72
|
|
|
break; |
|
73
|
|
|
case 'speed': |
|
74
|
|
|
$elapsedTime = $log['data'] - $this->startTime; |
|
|
|
|
|
|
75
|
|
|
$message = array( |
|
76
|
|
|
'message' => $log['name'], |
|
77
|
|
|
'data' => self::getReadableTime($elapsedTime), |
|
78
|
|
|
'type' => 'speed' |
|
79
|
|
|
); |
|
80
|
|
|
$console_data['meta']['speed']++; |
|
81
|
|
|
break; |
|
82
|
|
View Code Duplication |
default: |
|
|
|
|
|
|
83
|
|
|
$message = array( |
|
84
|
|
|
'message' => "Unrecognized console log type: {$log['type']}", |
|
85
|
|
|
'type' => 'error' |
|
86
|
|
|
); |
|
87
|
|
|
$console_data['meta']['error']++; |
|
88
|
|
|
break; |
|
89
|
|
|
} |
|
90
|
|
|
array_push($console_data['messages'], $message); |
|
91
|
|
|
} |
|
92
|
|
|
$this->output['console'] = $console_data; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Sets file data |
|
97
|
|
|
* |
|
98
|
|
|
* @param array $data |
|
99
|
|
|
*/ |
|
100
|
|
View Code Duplication |
public function setFileData(array $data) |
|
|
|
|
|
|
101
|
|
|
{ |
|
102
|
|
|
$fileData = array( |
|
103
|
|
|
'messages' => array(), |
|
104
|
|
|
'meta' => array( |
|
105
|
|
|
'count' => count($data), |
|
106
|
|
|
'size' => 0, |
|
107
|
|
|
'largest' => 0 |
|
108
|
|
|
) |
|
109
|
|
|
); |
|
110
|
|
|
|
|
111
|
|
|
foreach ($data as $file) { |
|
112
|
|
|
array_push($fileData['messages'], array( |
|
113
|
|
|
'message' => $file['name'], |
|
114
|
|
|
'data' => self::getReadableMemory($file['size']) |
|
115
|
|
|
)); |
|
116
|
|
|
|
|
117
|
|
|
$fileData['meta']['size'] += $file['size']; |
|
118
|
|
|
if ($file['size'] > $fileData['meta']['largest']) { |
|
119
|
|
|
$fileData['meta']['largest'] = $file['size']; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$fileData['meta']['size'] = self::getReadableMemory($fileData['meta']['size']); |
|
124
|
|
|
$fileData['meta']['largest'] = self::getReadableMemory($fileData['meta']['largest']); |
|
125
|
|
|
|
|
126
|
|
|
$this->output['files'] = $fileData; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Sets memory data |
|
131
|
|
|
* |
|
132
|
|
|
* @param array $data |
|
133
|
|
|
*/ |
|
134
|
|
|
public function setMemoryData(array $data) |
|
135
|
|
|
{ |
|
136
|
|
|
$this->output['memory']['meta'] = array( |
|
137
|
|
|
'used' => self::getReadableMemory($data['used']), |
|
138
|
|
|
'allowed' => $data['allowed'] |
|
139
|
|
|
); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
View Code Duplication |
public function setQueryData(array $data) |
|
|
|
|
|
|
143
|
|
|
{ |
|
144
|
|
|
$queryData = array( |
|
145
|
|
|
'messages' => array(), |
|
146
|
|
|
'meta' => array( |
|
147
|
|
|
'count' => count($data), |
|
148
|
|
|
'time' => 0, |
|
149
|
|
|
'slowest' => 0 |
|
150
|
|
|
) |
|
151
|
|
|
); |
|
152
|
|
|
|
|
153
|
|
|
foreach ($data as $query) { |
|
154
|
|
|
array_push($queryData['messages'], array( |
|
155
|
|
|
'message' => $query['sql'], |
|
156
|
|
|
'sub_data' => array_filter($query['explain']), |
|
157
|
|
|
'data' => self::getReadableTime($query['time']) |
|
158
|
|
|
)); |
|
159
|
|
|
$queryData['meta']['time'] += $query['time']; |
|
160
|
|
|
if ($query['time'] > $queryData['meta']['slowest']) { |
|
161
|
|
|
$queryData['meta']['slowest'] = $query['time']; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$queryData['meta']['time'] = self::getReadableTime($queryData['meta']['time']); |
|
166
|
|
|
$queryData['meta']['slowest'] = self::getReadableTime($queryData['meta']['slowest']); |
|
167
|
|
|
|
|
168
|
|
|
$this->output['query'] = $queryData; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Sets speed data |
|
173
|
|
|
* |
|
174
|
|
|
* @param array $data |
|
175
|
|
|
*/ |
|
176
|
|
|
public function setSpeedData(array $data) |
|
177
|
|
|
{ |
|
178
|
|
|
$this->output['speed']['meta'] = array( |
|
179
|
|
|
'elapsed' => self::getReadableTime($data['elapsed']), |
|
180
|
|
|
'allowed' => self::getReadableTime($data['allowed'], 0) |
|
181
|
|
|
); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Static formatter for human-readable time |
|
186
|
|
|
* Only handles time up to 60 minutes gracefully |
|
187
|
|
|
* |
|
188
|
|
|
* @param double $time |
|
189
|
|
|
* @param integer $decimals |
|
190
|
|
|
* @return string |
|
191
|
|
|
*/ |
|
192
|
|
|
public static function getReadableTime($time, $decimals = 3) |
|
193
|
|
|
{ |
|
194
|
|
|
$unit = 's'; |
|
195
|
|
|
if ($time < 1) { |
|
196
|
|
|
$time *= 1000; |
|
197
|
|
|
$unit = 'ms'; |
|
198
|
|
|
} else if ($time > 60) { |
|
199
|
|
|
$time /= 60; |
|
200
|
|
|
$unit = 'm'; |
|
201
|
|
|
} |
|
202
|
|
|
$time = number_format($time, $decimals); |
|
203
|
|
|
return "{$time} {$unit}"; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Static formatter for human-readable memory |
|
208
|
|
|
* |
|
209
|
|
|
* @param double $size |
|
210
|
|
|
* @param integer $decimals |
|
211
|
|
|
*/ |
|
212
|
|
|
public static function getReadableMemory($size, $decimals = 2) |
|
213
|
|
|
{ |
|
214
|
|
|
$unitOptions = array('b', 'k', 'M', 'G'); |
|
215
|
|
|
|
|
216
|
|
|
$base = log($size, 1024); |
|
217
|
|
|
|
|
218
|
|
|
$memory = round(pow(1024, $base - floor($base)), $decimals); |
|
219
|
|
|
$unit = $unitOptions[floor($base)]; |
|
220
|
|
|
return "{$memory} {$unit}"; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
public function __invoke() |
|
224
|
|
|
{ |
|
225
|
|
|
$output = $this->output; |
|
226
|
|
|
$header = array( |
|
|
|
|
|
|
227
|
|
|
'console' => count($output['console']['messages']), |
|
228
|
|
|
'speed' => $output['speed']['meta']['elapsed'], |
|
229
|
|
|
'query' => $output['query']['meta']['count'], |
|
230
|
|
|
'memory' => $output['memory']['meta']['used'], |
|
231
|
|
|
'files' => $output['files']['meta']['count'] |
|
232
|
|
|
); |
|
233
|
|
|
|
|
234
|
|
|
$console = $output['console']; |
|
235
|
|
|
|
|
236
|
|
|
$speed = $output['speed']; |
|
237
|
|
|
$speed['messages'] = array_filter($console['messages'], function ($message) { |
|
238
|
|
|
return $message['type'] == 'speed'; |
|
239
|
|
|
}); |
|
240
|
|
|
|
|
241
|
|
|
$query = $output['query']; |
|
|
|
|
|
|
242
|
|
|
|
|
243
|
|
|
$memory = $output['memory']; |
|
244
|
|
|
$memory['messages'] = array_filter($console['messages'], function ($message) { |
|
245
|
|
|
return $message['type'] == 'memory'; |
|
246
|
|
|
}); |
|
247
|
|
|
|
|
248
|
|
|
$files = $output['files']; |
|
|
|
|
|
|
249
|
|
|
|
|
250
|
|
|
// todo is this really the best way to load these? |
|
251
|
|
|
$styles = file_get_contents(__DIR__ . "./../{$this->options['style_path']}"); |
|
|
|
|
|
|
252
|
|
|
$script = file_get_contents(__DIR__ . "./../{$this->options['script_path']}"); |
|
|
|
|
|
|
253
|
|
|
|
|
254
|
|
|
require_once __DIR__ .'/../asset/display.tpl.php'; |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.