@@ -14,244 +14,244 @@ |
||
14 | 14 | class Display |
15 | 15 | { |
16 | 16 | |
17 | - /** @var array */ |
|
18 | - protected $defaults = array( |
|
19 | - 'script_path' => 'asset/script.js', |
|
20 | - 'style_path' => 'asset/style.css' |
|
21 | - ); |
|
17 | + /** @var array */ |
|
18 | + protected $defaults = array( |
|
19 | + 'script_path' => 'asset/script.js', |
|
20 | + 'style_path' => 'asset/style.css' |
|
21 | + ); |
|
22 | 22 | |
23 | - /** @var array */ |
|
24 | - protected $options; |
|
23 | + /** @var array */ |
|
24 | + protected $options; |
|
25 | 25 | |
26 | - /** @var array */ |
|
27 | - protected $output; |
|
26 | + /** @var array */ |
|
27 | + protected $output; |
|
28 | 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 | - } |
|
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 | 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 | - 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' => $this->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' => $this->getReadableTime($elapsedTime), |
|
78 | - 'type' => 'speed' |
|
79 | - ); |
|
80 | - $console_data['meta']['speed']++; |
|
81 | - break; |
|
82 | - 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 | - } |
|
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 | + 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' => $this->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' => $this->getReadableTime($elapsedTime), |
|
78 | + 'type' => 'speed' |
|
79 | + ); |
|
80 | + $console_data['meta']['speed']++; |
|
81 | + break; |
|
82 | + 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 | 94 | |
95 | - /** |
|
96 | - * Sets file data |
|
97 | - * |
|
98 | - * @param array $data |
|
99 | - */ |
|
100 | - 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 | - ); |
|
95 | + /** |
|
96 | + * Sets file data |
|
97 | + * |
|
98 | + * @param array $data |
|
99 | + */ |
|
100 | + 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 | 110 | |
111 | - foreach ($data as $file) { |
|
112 | - array_push($fileData['messages'], array( |
|
113 | - 'message' => $file['name'], |
|
114 | - 'data' => $this->getReadableMemory($file['size']) |
|
115 | - )); |
|
111 | + foreach ($data as $file) { |
|
112 | + array_push($fileData['messages'], array( |
|
113 | + 'message' => $file['name'], |
|
114 | + 'data' => $this->getReadableMemory($file['size']) |
|
115 | + )); |
|
116 | 116 | |
117 | - $fileData['meta']['size'] += $file['size']; |
|
118 | - if ($file['size'] > $fileData['meta']['largest']) { |
|
119 | - $fileData['meta']['largest'] = $file['size']; |
|
120 | - } |
|
121 | - } |
|
117 | + $fileData['meta']['size'] += $file['size']; |
|
118 | + if ($file['size'] > $fileData['meta']['largest']) { |
|
119 | + $fileData['meta']['largest'] = $file['size']; |
|
120 | + } |
|
121 | + } |
|
122 | 122 | |
123 | - $fileData['meta']['size'] = $this->getReadableMemory($fileData['meta']['size']); |
|
124 | - $fileData['meta']['largest'] = $this->getReadableMemory($fileData['meta']['largest']); |
|
123 | + $fileData['meta']['size'] = $this->getReadableMemory($fileData['meta']['size']); |
|
124 | + $fileData['meta']['largest'] = $this->getReadableMemory($fileData['meta']['largest']); |
|
125 | 125 | |
126 | - $this->output['files'] = $fileData; |
|
127 | - } |
|
126 | + $this->output['files'] = $fileData; |
|
127 | + } |
|
128 | 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' => $this->getReadableMemory($data['used']), |
|
138 | - 'allowed' => $data['allowed'] |
|
139 | - ); |
|
140 | - } |
|
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' => $this->getReadableMemory($data['used']), |
|
138 | + 'allowed' => $data['allowed'] |
|
139 | + ); |
|
140 | + } |
|
141 | 141 | |
142 | - 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 | - ); |
|
142 | + 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 | 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' => $this->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 | - } |
|
153 | + foreach ($data as $query) { |
|
154 | + array_push($queryData['messages'], array( |
|
155 | + 'message' => $query['sql'], |
|
156 | + 'sub_data' => array_filter($query['explain']), |
|
157 | + 'data' => $this->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 | 164 | |
165 | - $queryData['meta']['time'] = $this->getReadableTime($queryData['meta']['time']); |
|
166 | - $queryData['meta']['slowest'] = $this->getReadableTime($queryData['meta']['slowest']); |
|
165 | + $queryData['meta']['time'] = $this->getReadableTime($queryData['meta']['time']); |
|
166 | + $queryData['meta']['slowest'] = $this->getReadableTime($queryData['meta']['slowest']); |
|
167 | 167 | |
168 | - $this->output['query'] = $queryData; |
|
169 | - } |
|
168 | + $this->output['query'] = $queryData; |
|
169 | + } |
|
170 | 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' => $this->getReadableTime($data['elapsed']), |
|
180 | - 'allowed' => $this->getReadableTime($data['allowed'], 0) |
|
181 | - ); |
|
182 | - } |
|
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' => $this->getReadableTime($data['elapsed']), |
|
180 | + 'allowed' => $this->getReadableTime($data['allowed'], 0) |
|
181 | + ); |
|
182 | + } |
|
183 | 183 | |
184 | - /** |
|
185 | - * Formatter for human-readable time |
|
186 | - * Only handles time up to 60 minutes gracefully |
|
187 | - * |
|
188 | - * @param double $time |
|
189 | - * @param integer $percision |
|
190 | - * @return string |
|
191 | - */ |
|
192 | - protected function getReadableTime($time, $percision = 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, $percision); |
|
203 | - return "{$time} {$unit}"; |
|
204 | - } |
|
184 | + /** |
|
185 | + * Formatter for human-readable time |
|
186 | + * Only handles time up to 60 minutes gracefully |
|
187 | + * |
|
188 | + * @param double $time |
|
189 | + * @param integer $percision |
|
190 | + * @return string |
|
191 | + */ |
|
192 | + protected function getReadableTime($time, $percision = 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, $percision); |
|
203 | + return "{$time} {$unit}"; |
|
204 | + } |
|
205 | 205 | |
206 | - /** |
|
207 | - * Formatter for human-readable memory |
|
208 | - * Only handles time up to a few gigs gracefully |
|
209 | - * |
|
210 | - * @param double $size |
|
211 | - * @param integer $percision |
|
212 | - */ |
|
213 | - protected function getReadableMemory($size, $percision = 2) |
|
214 | - { |
|
215 | - $unitOptions = array('b', 'k', 'M', 'G'); |
|
206 | + /** |
|
207 | + * Formatter for human-readable memory |
|
208 | + * Only handles time up to a few gigs gracefully |
|
209 | + * |
|
210 | + * @param double $size |
|
211 | + * @param integer $percision |
|
212 | + */ |
|
213 | + protected function getReadableMemory($size, $percision = 2) |
|
214 | + { |
|
215 | + $unitOptions = array('b', 'k', 'M', 'G'); |
|
216 | 216 | |
217 | - $base = log($size, 1024); |
|
217 | + $base = log($size, 1024); |
|
218 | 218 | |
219 | - $memory = round(pow(1024, $base - floor($base)), $percision); |
|
220 | - $unit = $unitOptions[floor($base)]; |
|
221 | - return "{$memory} {$unit}"; |
|
222 | - } |
|
219 | + $memory = round(pow(1024, $base - floor($base)), $percision); |
|
220 | + $unit = $unitOptions[floor($base)]; |
|
221 | + return "{$memory} {$unit}"; |
|
222 | + } |
|
223 | 223 | |
224 | - public function __invoke() |
|
225 | - { |
|
226 | - $output = $this->output; |
|
227 | - $header = array( |
|
228 | - 'console' => count($output['console']['messages']), |
|
229 | - 'speed' => $output['speed']['meta']['elapsed'], |
|
230 | - 'query' => $output['query']['meta']['count'], |
|
231 | - 'memory' => $output['memory']['meta']['used'], |
|
232 | - 'files' => $output['files']['meta']['count'] |
|
233 | - ); |
|
224 | + public function __invoke() |
|
225 | + { |
|
226 | + $output = $this->output; |
|
227 | + $header = array( |
|
228 | + 'console' => count($output['console']['messages']), |
|
229 | + 'speed' => $output['speed']['meta']['elapsed'], |
|
230 | + 'query' => $output['query']['meta']['count'], |
|
231 | + 'memory' => $output['memory']['meta']['used'], |
|
232 | + 'files' => $output['files']['meta']['count'] |
|
233 | + ); |
|
234 | 234 | |
235 | - $console = $output['console']; |
|
235 | + $console = $output['console']; |
|
236 | 236 | |
237 | - $speed = $output['speed']; |
|
238 | - $speed['messages'] = array_filter($console['messages'], function ($message) { |
|
239 | - return $message['type'] == 'speed'; |
|
240 | - }); |
|
237 | + $speed = $output['speed']; |
|
238 | + $speed['messages'] = array_filter($console['messages'], function ($message) { |
|
239 | + return $message['type'] == 'speed'; |
|
240 | + }); |
|
241 | 241 | |
242 | - $query = $output['query']; |
|
242 | + $query = $output['query']; |
|
243 | 243 | |
244 | - $memory = $output['memory']; |
|
245 | - $memory['messages'] = array_filter($console['messages'], function ($message) { |
|
246 | - return $message['type'] == 'memory'; |
|
247 | - }); |
|
244 | + $memory = $output['memory']; |
|
245 | + $memory['messages'] = array_filter($console['messages'], function ($message) { |
|
246 | + return $message['type'] == 'memory'; |
|
247 | + }); |
|
248 | 248 | |
249 | - $files = $output['files']; |
|
249 | + $files = $output['files']; |
|
250 | 250 | |
251 | - // todo is this really the best way to load these? |
|
252 | - $styles = file_get_contents(__DIR__ . "./../{$this->options['style_path']}"); |
|
253 | - $script = file_get_contents(__DIR__ . "./../{$this->options['script_path']}"); |
|
251 | + // todo is this really the best way to load these? |
|
252 | + $styles = file_get_contents(__DIR__ . "./../{$this->options['style_path']}"); |
|
253 | + $script = file_get_contents(__DIR__ . "./../{$this->options['script_path']}"); |
|
254 | 254 | |
255 | - require_once __DIR__ .'/../asset/display.html'; |
|
256 | - } |
|
255 | + require_once __DIR__ .'/../asset/display.html'; |
|
256 | + } |
|
257 | 257 | } |
@@ -8,43 +8,43 @@ |
||
8 | 8 | class DisplayTest extends PHPUnit_Framework_TestCase |
9 | 9 | { |
10 | 10 | |
11 | - public function testGetReadableTime() |
|
12 | - { |
|
13 | - $timeTest = array( |
|
14 | - '.032432' => '32.432 ms', |
|
15 | - '24.3781' => '24.378 s', |
|
16 | - '145.123' => '2.419 m' |
|
17 | - ); |
|
18 | - $display = new Display(); |
|
19 | - $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableTime'); |
|
20 | - |
|
21 | - foreach ($timeTest as $rawTime => $expectedReadableTime) { |
|
22 | - $readableTime = $reflectedMethod->invokeArgs($display, array($rawTime)); |
|
23 | - $this->assertEquals($expectedReadableTime, $readableTime); |
|
24 | - } |
|
25 | - } |
|
26 | - |
|
27 | - public function testGetReadableMemory() |
|
28 | - { |
|
29 | - $memoryTest = array( |
|
30 | - '314' => '314 b', |
|
31 | - '7403' => '7.23 k', |
|
32 | - '2589983' => '2.47 M' |
|
33 | - ); |
|
34 | - $display = new Display(); |
|
35 | - $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableMemory'); |
|
36 | - |
|
37 | - foreach ($memoryTest as $rawMemory => $expectedReadableMemory) { |
|
38 | - $readableMemory = $reflectedMethod->invokeArgs($display, array($rawMemory)); |
|
39 | - $this->assertEquals($expectedReadableMemory, $readableMemory); |
|
40 | - } |
|
41 | - } |
|
42 | - |
|
43 | - protected function getAccessibleMethod(Display $display, $methodName) |
|
44 | - { |
|
45 | - $reflectedConsole = new ReflectionClass(get_class($display)); |
|
46 | - $reflectedMethod = $reflectedConsole->getMethod($methodName); |
|
47 | - $reflectedMethod->setAccessible(true); |
|
48 | - return $reflectedMethod; |
|
49 | - } |
|
11 | + public function testGetReadableTime() |
|
12 | + { |
|
13 | + $timeTest = array( |
|
14 | + '.032432' => '32.432 ms', |
|
15 | + '24.3781' => '24.378 s', |
|
16 | + '145.123' => '2.419 m' |
|
17 | + ); |
|
18 | + $display = new Display(); |
|
19 | + $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableTime'); |
|
20 | + |
|
21 | + foreach ($timeTest as $rawTime => $expectedReadableTime) { |
|
22 | + $readableTime = $reflectedMethod->invokeArgs($display, array($rawTime)); |
|
23 | + $this->assertEquals($expectedReadableTime, $readableTime); |
|
24 | + } |
|
25 | + } |
|
26 | + |
|
27 | + public function testGetReadableMemory() |
|
28 | + { |
|
29 | + $memoryTest = array( |
|
30 | + '314' => '314 b', |
|
31 | + '7403' => '7.23 k', |
|
32 | + '2589983' => '2.47 M' |
|
33 | + ); |
|
34 | + $display = new Display(); |
|
35 | + $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableMemory'); |
|
36 | + |
|
37 | + foreach ($memoryTest as $rawMemory => $expectedReadableMemory) { |
|
38 | + $readableMemory = $reflectedMethod->invokeArgs($display, array($rawMemory)); |
|
39 | + $this->assertEquals($expectedReadableMemory, $readableMemory); |
|
40 | + } |
|
41 | + } |
|
42 | + |
|
43 | + protected function getAccessibleMethod(Display $display, $methodName) |
|
44 | + { |
|
45 | + $reflectedConsole = new ReflectionClass(get_class($display)); |
|
46 | + $reflectedMethod = $reflectedConsole->getMethod($methodName); |
|
47 | + $reflectedMethod->setAccessible(true); |
|
48 | + return $reflectedMethod; |
|
49 | + } |
|
50 | 50 | } |