@@ -8,19 +8,19 @@ discard block |
||
8 | 8 | class PhpQuickProfilerTest extends PHPUnit_Framework_TestCase |
9 | 9 | { |
10 | 10 | |
11 | - protected static $dbConnection; |
|
11 | + protected static $dbConnection; |
|
12 | 12 | |
13 | - public static function setUpBeforeClass() |
|
14 | - { |
|
15 | - self::$dbConnection = new LoggingPdo('sqlite::memory:'); |
|
16 | - $createTable = " |
|
13 | + public static function setUpBeforeClass() |
|
14 | + { |
|
15 | + self::$dbConnection = new LoggingPdo('sqlite::memory:'); |
|
16 | + $createTable = " |
|
17 | 17 | CREATE TABLE IF NOT EXISTS `testing` ( |
18 | 18 | `id` integer PRIMARY KEY AUTOINCREMENT, |
19 | 19 | `title` varchar(60) NOT NULL |
20 | 20 | );"; |
21 | - self::$dbConnection->exec($createTable); |
|
21 | + self::$dbConnection->exec($createTable); |
|
22 | 22 | |
23 | - $hydrateTable = " |
|
23 | + $hydrateTable = " |
|
24 | 24 | INSERT INTO `testing` |
25 | 25 | (`title`) |
26 | 26 | VALUES |
@@ -28,318 +28,318 @@ discard block |
||
28 | 28 | ('beta'), |
29 | 29 | ('charlie'), |
30 | 30 | ('delta');"; |
31 | - self::$dbConnection->exec($hydrateTable); |
|
32 | - } |
|
33 | - |
|
34 | - public function testConstruct() |
|
35 | - { |
|
36 | - $startTime = microtime(true); |
|
37 | - |
|
38 | - $profiler = new PhpQuickProfiler(); |
|
39 | - $this->assertAttributeEquals($startTime, 'startTime', $profiler); |
|
40 | - |
|
41 | - $profiler = new PhpQuickProfiler($startTime); |
|
42 | - $this->assertAttributeEquals($startTime, 'startTime', $profiler); |
|
43 | - } |
|
44 | - |
|
45 | - public function testSetConsole() |
|
46 | - { |
|
47 | - $console = new Console(); |
|
48 | - $profiler = new PhpQuickProfiler(); |
|
49 | - $profiler->setConsole($console); |
|
50 | - |
|
51 | - $this->assertAttributeSame($console, 'console', $profiler); |
|
52 | - } |
|
53 | - |
|
54 | - public function testSetDisplay() |
|
55 | - { |
|
56 | - $display = new Display(); |
|
57 | - $profiler = new PhpQuickProfiler(); |
|
58 | - $profiler->setDisplay($display); |
|
59 | - |
|
60 | - $this->assertAttributeSame($display, 'display', $profiler); |
|
61 | - } |
|
62 | - |
|
63 | - public function testGatherFileData() |
|
64 | - { |
|
65 | - $files = get_included_files(); |
|
66 | - $profiler = new PhpQuickProfiler(); |
|
67 | - $gatheredFileData = $profiler->gatherFileData(); |
|
68 | - |
|
69 | - $this->assertInternalType('array', $gatheredFileData); |
|
70 | - $this->assertEquals(count($files), count($gatheredFileData)); |
|
71 | - foreach ($gatheredFileData as $fileData) { |
|
72 | - $this->assertInternalType('array', $fileData); |
|
73 | - $this->assertArrayHasKey('name', $fileData); |
|
74 | - $this->assertContains($fileData['name'], $files); |
|
75 | - $this->assertArrayHasKey('size', $fileData); |
|
76 | - $this->assertEquals($fileData['size'], filesize($fileData['name'])); |
|
77 | - } |
|
78 | - } |
|
79 | - |
|
80 | - public function testGatherMemoryData() |
|
81 | - { |
|
82 | - $memoryUsage = memory_get_peak_usage(); |
|
83 | - $allowedLimit = ini_get('memory_limit'); |
|
84 | - $profiler = new PhpQuickProfiler(); |
|
85 | - $gatheredMemoryData = $profiler->gatherMemoryData(); |
|
86 | - |
|
87 | - $this->assertInternalType('array', $gatheredMemoryData); |
|
88 | - $this->assertEquals(2, count($gatheredMemoryData)); |
|
89 | - $this->assertArrayHasKey('used', $gatheredMemoryData); |
|
90 | - $this->assertEquals($memoryUsage, $gatheredMemoryData['used']); |
|
91 | - $this->assertArrayHasKey('allowed', $gatheredMemoryData); |
|
92 | - $this->assertEquals($allowedLimit, $gatheredMemoryData['allowed']); |
|
93 | - } |
|
94 | - |
|
95 | - public function testSetProfiledQueries() |
|
96 | - { |
|
97 | - $profiledQueries = $this->dataProfiledQueries(); |
|
98 | - $profiler = new PhpQuickProfiler(); |
|
99 | - $profiler->setProfiledQueries($profiledQueries); |
|
100 | - |
|
101 | - $this->assertAttributeEquals($profiledQueries, 'profiledQueries', $profiler); |
|
102 | - } |
|
103 | - |
|
104 | - public function testGatherQueryData() |
|
105 | - { |
|
106 | - $profiledQueries = $this->dataProfiledQueries(); |
|
107 | - $profiledQueriesSql = array(); |
|
108 | - $profiledQueriesTime = array(); |
|
109 | - foreach ($profiledQueries as $queryData) { |
|
110 | - array_push($profiledQueriesSql, $queryData['sql']); |
|
111 | - array_push($profiledQueriesTime, $queryData['time']); |
|
112 | - } |
|
113 | - |
|
114 | - $profiler = new PhpQuickProfiler(); |
|
115 | - $profiler->setProfiledQueries($profiledQueries); |
|
116 | - $gatheredQueryData = $profiler->gatherQueryData(self::$dbConnection); |
|
117 | - |
|
118 | - $this->assertInternalType('array', $gatheredQueryData); |
|
119 | - $this->assertEquals(count($profiledQueries), count($gatheredQueryData)); |
|
120 | - foreach ($gatheredQueryData as $queryData) { |
|
121 | - $this->assertInternalType('array', $queryData); |
|
122 | - $this->assertArrayHasKey('sql', $queryData); |
|
123 | - $this->assertContains($queryData['sql'], $profiledQueriesSql); |
|
124 | - $this->assertArrayHasKey('explain', $queryData); |
|
125 | - $this->assertInternaltype('array', $queryData['explain']); |
|
126 | - $this->assertGreaterThan(0, count($queryData['explain'])); |
|
127 | - $this->assertArrayHasKey('time', $queryData); |
|
128 | - $this->assertContains($queryData['time'], $profiledQueriesTime); |
|
129 | - } |
|
130 | - } |
|
131 | - |
|
132 | - public function testGatherQueryDataInternalProfiler() |
|
133 | - { |
|
134 | - $profiledQueries = $this->dataProfiledQueries(); |
|
135 | - $dbConnection = self::$dbConnection; |
|
136 | - $dbConnection->queries = $profiledQueries; |
|
137 | - $profiler = new PhpQuickProfiler(); |
|
138 | - $profiler->gatherQueryData($dbConnection); |
|
139 | - |
|
140 | - $this->assertAttributeSame($profiledQueries, 'profiledQueries', $profiler); |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * @dataProvider dataProfiledQueries |
|
145 | - */ |
|
146 | - public function testExplainQuery($sql, $parameters) |
|
147 | - { |
|
148 | - $profiler = new PhpQuickProfiler(); |
|
149 | - $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery'); |
|
150 | - |
|
151 | - $explainedQuery = $reflectedMethod->invokeArgs( |
|
152 | - $profiler, |
|
153 | - array(self::$dbConnection, $sql, $parameters) |
|
154 | - ); |
|
155 | - $this->assertInternalType('array', $explainedQuery); |
|
156 | - $this->assertGreaterThan(0, count($explainedQuery)); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * @expectedException Exception |
|
161 | - */ |
|
162 | - public function testExplainQueryBadQueryException() |
|
163 | - { |
|
164 | - $invalidQuery = 'SELECT * FROM `fake_table`'; |
|
165 | - $profiler = new PhpQuickProfiler(); |
|
166 | - $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery'); |
|
167 | - |
|
168 | - $reflectedMethod->invokeArgs( |
|
169 | - $profiler, |
|
170 | - array(self::$dbConnection, $invalidQuery) |
|
171 | - ); |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * @expectedException Exception |
|
176 | - */ |
|
177 | - public function testExplainQueryBadParametersException() |
|
178 | - { |
|
179 | - $query = 'SELECT * FROM `testing` WHERE `title` = :title'; |
|
180 | - $invalidParams = array('id' => 1); |
|
181 | - $profiler = new PhpQuickProfiler(); |
|
182 | - $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery'); |
|
183 | - |
|
184 | - $reflectedMethod->invokeArgs( |
|
185 | - $profiler, |
|
186 | - array(self::$dbConnection, $query, $invalidParams) |
|
187 | - ); |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * @dataProvider dataConnectionDrivers |
|
192 | - */ |
|
193 | - public function testGetExplainQuery($driver, $prefix) |
|
194 | - { |
|
195 | - $query = 'SELECT * FROM `testing`'; |
|
196 | - $profiler = new PhpQuickProfiler(); |
|
197 | - $reflectedMethod = $this->getAccessibleMethod($profiler, 'getExplainQuery'); |
|
198 | - |
|
199 | - $explainQuery = $reflectedMethod->invokeArgs( |
|
200 | - $profiler, |
|
201 | - array($query, $driver) |
|
202 | - ); |
|
203 | - |
|
204 | - $explainPrefix = str_replace($query, '', $explainQuery); |
|
205 | - $explainPrefix = trim($explainPrefix); |
|
206 | - $this->assertEquals($prefix, $explainPrefix); |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * @expectedException Exception |
|
211 | - */ |
|
212 | - public function testGetExplainQueryUnsupportedDriver() |
|
213 | - { |
|
214 | - $query = 'SELECT * FROM `testing`'; |
|
215 | - $unsupportedDriver = 'zz'; |
|
216 | - $profiler = new PhpQuickProfiler(); |
|
217 | - $reflectedMethod = $this->getAccessibleMethod($profiler, 'getExplainQuery'); |
|
218 | - |
|
219 | - $reflectedMethod->invokeArgs( |
|
220 | - $profiler, |
|
221 | - array($query, $unsupportedDriver) |
|
222 | - ); |
|
223 | - } |
|
224 | - |
|
225 | - public function testGatherSpeedData() |
|
226 | - { |
|
227 | - $elapsedTime = 1.234; |
|
228 | - $startTime = microtime(true) - $elapsedTime; |
|
229 | - $allowedTime = ini_get('max_execution_time'); |
|
230 | - $profiler = new PhpQuickProfiler($startTime); |
|
231 | - $gatheredSpeedData = $profiler->gatherSpeedData(); |
|
232 | - |
|
233 | - $this->assertInternalType('array', $gatheredSpeedData); |
|
234 | - $this->assertEquals(2, count($gatheredSpeedData)); |
|
235 | - $this->assertArrayHasKey('elapsed', $gatheredSpeedData); |
|
236 | - $this->assertEquals($elapsedTime, $gatheredSpeedData['elapsed']); |
|
237 | - $this->assertArrayHasKey('allowed', $gatheredSpeedData); |
|
238 | - $this->assertEquals($allowedTime, $gatheredSpeedData['allowed']); |
|
239 | - } |
|
240 | - |
|
241 | - public function testDisplay() |
|
242 | - { |
|
243 | - $console = new Console(); |
|
244 | - $profiler = new PhpQuickProfiler(); |
|
245 | - |
|
246 | - $reflectedProfiler = new ReflectionClass(get_class($profiler)); |
|
247 | - $reflectedProperty = $reflectedProfiler->getProperty('startTime'); |
|
248 | - $reflectedProperty->setAccessible(true); |
|
249 | - $startTime = $reflectedProperty->getValue($profiler); |
|
250 | - |
|
251 | - $expectedDisplay = new Display(); |
|
252 | - $expectedDisplay->setStartTime($startTime); |
|
253 | - $expectedDisplay->setConsole($console); |
|
254 | - $expectedDisplay->setFileData($profiler->gatherFileData()); |
|
255 | - $expectedDisplay->setMemoryData($profiler->gatherMemoryData()); |
|
256 | - $expectedDisplay->setQueryData($profiler->gatherQueryData()); |
|
257 | - $expectedDisplay->setSpeedData($profiler->gatherSpeedData()); |
|
258 | - ob_start(); |
|
259 | - $expectedDisplay->__invoke(); |
|
260 | - ob_end_clean(); |
|
261 | - |
|
262 | - $display = new Display(); |
|
263 | - $profiler->setConsole($console); |
|
264 | - $profiler->setDisplay($display); |
|
265 | - ob_start(); |
|
266 | - $profiler->display(); |
|
267 | - ob_end_clean(); |
|
268 | - |
|
269 | - $this->assertAttributeEquals($expectedDisplay, 'display', $profiler); |
|
270 | - } |
|
271 | - |
|
272 | - /** |
|
273 | - * @expectedException Exception |
|
274 | - */ |
|
275 | - public function testDisplayNothingSetException() |
|
276 | - { |
|
277 | - $profiler = new PhpQuickProfiler(); |
|
278 | - $profiler->display(); |
|
279 | - } |
|
280 | - |
|
281 | - /** |
|
282 | - * @expectedException Exception |
|
283 | - */ |
|
284 | - public function testDisplayNoConsoleException() |
|
285 | - { |
|
286 | - $display = new Display(); |
|
287 | - $profiler = new PhpQuickProfiler(); |
|
288 | - $profiler->setDisplay($display); |
|
289 | - $profiler->display(); |
|
290 | - } |
|
291 | - |
|
292 | - /** |
|
293 | - * @expectedException Exception |
|
294 | - */ |
|
295 | - public function testDisplayNoDisplayException() |
|
296 | - { |
|
297 | - $console = new Console(); |
|
298 | - $profiler = new PhpQuickProfiler(); |
|
299 | - $profiler->setConsole($console); |
|
300 | - $profiler->display(); |
|
301 | - } |
|
302 | - |
|
303 | - public function dataProfiledQueries() |
|
304 | - { |
|
305 | - return array( |
|
306 | - array( |
|
307 | - 'sql' => "SELECT * FROM testing", |
|
308 | - 'parameters' => array(), |
|
309 | - 'time' => 25 |
|
310 | - ), |
|
311 | - array( |
|
312 | - 'sql' => "SELECT id FROM testing WHERE title = :title", |
|
313 | - 'parameters' => array('title' => 'beta'), |
|
314 | - 'time' => 5 |
|
315 | - ) |
|
316 | - ); |
|
317 | - } |
|
318 | - |
|
319 | - public function dataConnectionDrivers() |
|
320 | - { |
|
321 | - return array( |
|
322 | - array( |
|
323 | - 'driver' => 'mysql', |
|
324 | - 'prefix' => 'EXPLAIN' |
|
325 | - ), |
|
326 | - array( |
|
327 | - 'driver' => 'sqlite', |
|
328 | - 'prefix' => 'EXPLAIN QUERY PLAN' |
|
329 | - ) |
|
330 | - ); |
|
331 | - } |
|
332 | - |
|
333 | - protected function getAccessibleMethod(PhpQuickProfiler $profiler, $methodName) |
|
334 | - { |
|
335 | - $reflectedConsole = new ReflectionClass(get_class($profiler)); |
|
336 | - $reflectedMethod = $reflectedConsole->getMethod($methodName); |
|
337 | - $reflectedMethod->setAccessible(true); |
|
338 | - return $reflectedMethod; |
|
339 | - } |
|
340 | - |
|
341 | - public static function tearDownAfterClass() |
|
342 | - { |
|
343 | - self::$dbConnection = null; |
|
344 | - } |
|
31 | + self::$dbConnection->exec($hydrateTable); |
|
32 | + } |
|
33 | + |
|
34 | + public function testConstruct() |
|
35 | + { |
|
36 | + $startTime = microtime(true); |
|
37 | + |
|
38 | + $profiler = new PhpQuickProfiler(); |
|
39 | + $this->assertAttributeEquals($startTime, 'startTime', $profiler); |
|
40 | + |
|
41 | + $profiler = new PhpQuickProfiler($startTime); |
|
42 | + $this->assertAttributeEquals($startTime, 'startTime', $profiler); |
|
43 | + } |
|
44 | + |
|
45 | + public function testSetConsole() |
|
46 | + { |
|
47 | + $console = new Console(); |
|
48 | + $profiler = new PhpQuickProfiler(); |
|
49 | + $profiler->setConsole($console); |
|
50 | + |
|
51 | + $this->assertAttributeSame($console, 'console', $profiler); |
|
52 | + } |
|
53 | + |
|
54 | + public function testSetDisplay() |
|
55 | + { |
|
56 | + $display = new Display(); |
|
57 | + $profiler = new PhpQuickProfiler(); |
|
58 | + $profiler->setDisplay($display); |
|
59 | + |
|
60 | + $this->assertAttributeSame($display, 'display', $profiler); |
|
61 | + } |
|
62 | + |
|
63 | + public function testGatherFileData() |
|
64 | + { |
|
65 | + $files = get_included_files(); |
|
66 | + $profiler = new PhpQuickProfiler(); |
|
67 | + $gatheredFileData = $profiler->gatherFileData(); |
|
68 | + |
|
69 | + $this->assertInternalType('array', $gatheredFileData); |
|
70 | + $this->assertEquals(count($files), count($gatheredFileData)); |
|
71 | + foreach ($gatheredFileData as $fileData) { |
|
72 | + $this->assertInternalType('array', $fileData); |
|
73 | + $this->assertArrayHasKey('name', $fileData); |
|
74 | + $this->assertContains($fileData['name'], $files); |
|
75 | + $this->assertArrayHasKey('size', $fileData); |
|
76 | + $this->assertEquals($fileData['size'], filesize($fileData['name'])); |
|
77 | + } |
|
78 | + } |
|
79 | + |
|
80 | + public function testGatherMemoryData() |
|
81 | + { |
|
82 | + $memoryUsage = memory_get_peak_usage(); |
|
83 | + $allowedLimit = ini_get('memory_limit'); |
|
84 | + $profiler = new PhpQuickProfiler(); |
|
85 | + $gatheredMemoryData = $profiler->gatherMemoryData(); |
|
86 | + |
|
87 | + $this->assertInternalType('array', $gatheredMemoryData); |
|
88 | + $this->assertEquals(2, count($gatheredMemoryData)); |
|
89 | + $this->assertArrayHasKey('used', $gatheredMemoryData); |
|
90 | + $this->assertEquals($memoryUsage, $gatheredMemoryData['used']); |
|
91 | + $this->assertArrayHasKey('allowed', $gatheredMemoryData); |
|
92 | + $this->assertEquals($allowedLimit, $gatheredMemoryData['allowed']); |
|
93 | + } |
|
94 | + |
|
95 | + public function testSetProfiledQueries() |
|
96 | + { |
|
97 | + $profiledQueries = $this->dataProfiledQueries(); |
|
98 | + $profiler = new PhpQuickProfiler(); |
|
99 | + $profiler->setProfiledQueries($profiledQueries); |
|
100 | + |
|
101 | + $this->assertAttributeEquals($profiledQueries, 'profiledQueries', $profiler); |
|
102 | + } |
|
103 | + |
|
104 | + public function testGatherQueryData() |
|
105 | + { |
|
106 | + $profiledQueries = $this->dataProfiledQueries(); |
|
107 | + $profiledQueriesSql = array(); |
|
108 | + $profiledQueriesTime = array(); |
|
109 | + foreach ($profiledQueries as $queryData) { |
|
110 | + array_push($profiledQueriesSql, $queryData['sql']); |
|
111 | + array_push($profiledQueriesTime, $queryData['time']); |
|
112 | + } |
|
113 | + |
|
114 | + $profiler = new PhpQuickProfiler(); |
|
115 | + $profiler->setProfiledQueries($profiledQueries); |
|
116 | + $gatheredQueryData = $profiler->gatherQueryData(self::$dbConnection); |
|
117 | + |
|
118 | + $this->assertInternalType('array', $gatheredQueryData); |
|
119 | + $this->assertEquals(count($profiledQueries), count($gatheredQueryData)); |
|
120 | + foreach ($gatheredQueryData as $queryData) { |
|
121 | + $this->assertInternalType('array', $queryData); |
|
122 | + $this->assertArrayHasKey('sql', $queryData); |
|
123 | + $this->assertContains($queryData['sql'], $profiledQueriesSql); |
|
124 | + $this->assertArrayHasKey('explain', $queryData); |
|
125 | + $this->assertInternaltype('array', $queryData['explain']); |
|
126 | + $this->assertGreaterThan(0, count($queryData['explain'])); |
|
127 | + $this->assertArrayHasKey('time', $queryData); |
|
128 | + $this->assertContains($queryData['time'], $profiledQueriesTime); |
|
129 | + } |
|
130 | + } |
|
131 | + |
|
132 | + public function testGatherQueryDataInternalProfiler() |
|
133 | + { |
|
134 | + $profiledQueries = $this->dataProfiledQueries(); |
|
135 | + $dbConnection = self::$dbConnection; |
|
136 | + $dbConnection->queries = $profiledQueries; |
|
137 | + $profiler = new PhpQuickProfiler(); |
|
138 | + $profiler->gatherQueryData($dbConnection); |
|
139 | + |
|
140 | + $this->assertAttributeSame($profiledQueries, 'profiledQueries', $profiler); |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * @dataProvider dataProfiledQueries |
|
145 | + */ |
|
146 | + public function testExplainQuery($sql, $parameters) |
|
147 | + { |
|
148 | + $profiler = new PhpQuickProfiler(); |
|
149 | + $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery'); |
|
150 | + |
|
151 | + $explainedQuery = $reflectedMethod->invokeArgs( |
|
152 | + $profiler, |
|
153 | + array(self::$dbConnection, $sql, $parameters) |
|
154 | + ); |
|
155 | + $this->assertInternalType('array', $explainedQuery); |
|
156 | + $this->assertGreaterThan(0, count($explainedQuery)); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * @expectedException Exception |
|
161 | + */ |
|
162 | + public function testExplainQueryBadQueryException() |
|
163 | + { |
|
164 | + $invalidQuery = 'SELECT * FROM `fake_table`'; |
|
165 | + $profiler = new PhpQuickProfiler(); |
|
166 | + $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery'); |
|
167 | + |
|
168 | + $reflectedMethod->invokeArgs( |
|
169 | + $profiler, |
|
170 | + array(self::$dbConnection, $invalidQuery) |
|
171 | + ); |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * @expectedException Exception |
|
176 | + */ |
|
177 | + public function testExplainQueryBadParametersException() |
|
178 | + { |
|
179 | + $query = 'SELECT * FROM `testing` WHERE `title` = :title'; |
|
180 | + $invalidParams = array('id' => 1); |
|
181 | + $profiler = new PhpQuickProfiler(); |
|
182 | + $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery'); |
|
183 | + |
|
184 | + $reflectedMethod->invokeArgs( |
|
185 | + $profiler, |
|
186 | + array(self::$dbConnection, $query, $invalidParams) |
|
187 | + ); |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * @dataProvider dataConnectionDrivers |
|
192 | + */ |
|
193 | + public function testGetExplainQuery($driver, $prefix) |
|
194 | + { |
|
195 | + $query = 'SELECT * FROM `testing`'; |
|
196 | + $profiler = new PhpQuickProfiler(); |
|
197 | + $reflectedMethod = $this->getAccessibleMethod($profiler, 'getExplainQuery'); |
|
198 | + |
|
199 | + $explainQuery = $reflectedMethod->invokeArgs( |
|
200 | + $profiler, |
|
201 | + array($query, $driver) |
|
202 | + ); |
|
203 | + |
|
204 | + $explainPrefix = str_replace($query, '', $explainQuery); |
|
205 | + $explainPrefix = trim($explainPrefix); |
|
206 | + $this->assertEquals($prefix, $explainPrefix); |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * @expectedException Exception |
|
211 | + */ |
|
212 | + public function testGetExplainQueryUnsupportedDriver() |
|
213 | + { |
|
214 | + $query = 'SELECT * FROM `testing`'; |
|
215 | + $unsupportedDriver = 'zz'; |
|
216 | + $profiler = new PhpQuickProfiler(); |
|
217 | + $reflectedMethod = $this->getAccessibleMethod($profiler, 'getExplainQuery'); |
|
218 | + |
|
219 | + $reflectedMethod->invokeArgs( |
|
220 | + $profiler, |
|
221 | + array($query, $unsupportedDriver) |
|
222 | + ); |
|
223 | + } |
|
224 | + |
|
225 | + public function testGatherSpeedData() |
|
226 | + { |
|
227 | + $elapsedTime = 1.234; |
|
228 | + $startTime = microtime(true) - $elapsedTime; |
|
229 | + $allowedTime = ini_get('max_execution_time'); |
|
230 | + $profiler = new PhpQuickProfiler($startTime); |
|
231 | + $gatheredSpeedData = $profiler->gatherSpeedData(); |
|
232 | + |
|
233 | + $this->assertInternalType('array', $gatheredSpeedData); |
|
234 | + $this->assertEquals(2, count($gatheredSpeedData)); |
|
235 | + $this->assertArrayHasKey('elapsed', $gatheredSpeedData); |
|
236 | + $this->assertEquals($elapsedTime, $gatheredSpeedData['elapsed']); |
|
237 | + $this->assertArrayHasKey('allowed', $gatheredSpeedData); |
|
238 | + $this->assertEquals($allowedTime, $gatheredSpeedData['allowed']); |
|
239 | + } |
|
240 | + |
|
241 | + public function testDisplay() |
|
242 | + { |
|
243 | + $console = new Console(); |
|
244 | + $profiler = new PhpQuickProfiler(); |
|
245 | + |
|
246 | + $reflectedProfiler = new ReflectionClass(get_class($profiler)); |
|
247 | + $reflectedProperty = $reflectedProfiler->getProperty('startTime'); |
|
248 | + $reflectedProperty->setAccessible(true); |
|
249 | + $startTime = $reflectedProperty->getValue($profiler); |
|
250 | + |
|
251 | + $expectedDisplay = new Display(); |
|
252 | + $expectedDisplay->setStartTime($startTime); |
|
253 | + $expectedDisplay->setConsole($console); |
|
254 | + $expectedDisplay->setFileData($profiler->gatherFileData()); |
|
255 | + $expectedDisplay->setMemoryData($profiler->gatherMemoryData()); |
|
256 | + $expectedDisplay->setQueryData($profiler->gatherQueryData()); |
|
257 | + $expectedDisplay->setSpeedData($profiler->gatherSpeedData()); |
|
258 | + ob_start(); |
|
259 | + $expectedDisplay->__invoke(); |
|
260 | + ob_end_clean(); |
|
261 | + |
|
262 | + $display = new Display(); |
|
263 | + $profiler->setConsole($console); |
|
264 | + $profiler->setDisplay($display); |
|
265 | + ob_start(); |
|
266 | + $profiler->display(); |
|
267 | + ob_end_clean(); |
|
268 | + |
|
269 | + $this->assertAttributeEquals($expectedDisplay, 'display', $profiler); |
|
270 | + } |
|
271 | + |
|
272 | + /** |
|
273 | + * @expectedException Exception |
|
274 | + */ |
|
275 | + public function testDisplayNothingSetException() |
|
276 | + { |
|
277 | + $profiler = new PhpQuickProfiler(); |
|
278 | + $profiler->display(); |
|
279 | + } |
|
280 | + |
|
281 | + /** |
|
282 | + * @expectedException Exception |
|
283 | + */ |
|
284 | + public function testDisplayNoConsoleException() |
|
285 | + { |
|
286 | + $display = new Display(); |
|
287 | + $profiler = new PhpQuickProfiler(); |
|
288 | + $profiler->setDisplay($display); |
|
289 | + $profiler->display(); |
|
290 | + } |
|
291 | + |
|
292 | + /** |
|
293 | + * @expectedException Exception |
|
294 | + */ |
|
295 | + public function testDisplayNoDisplayException() |
|
296 | + { |
|
297 | + $console = new Console(); |
|
298 | + $profiler = new PhpQuickProfiler(); |
|
299 | + $profiler->setConsole($console); |
|
300 | + $profiler->display(); |
|
301 | + } |
|
302 | + |
|
303 | + public function dataProfiledQueries() |
|
304 | + { |
|
305 | + return array( |
|
306 | + array( |
|
307 | + 'sql' => "SELECT * FROM testing", |
|
308 | + 'parameters' => array(), |
|
309 | + 'time' => 25 |
|
310 | + ), |
|
311 | + array( |
|
312 | + 'sql' => "SELECT id FROM testing WHERE title = :title", |
|
313 | + 'parameters' => array('title' => 'beta'), |
|
314 | + 'time' => 5 |
|
315 | + ) |
|
316 | + ); |
|
317 | + } |
|
318 | + |
|
319 | + public function dataConnectionDrivers() |
|
320 | + { |
|
321 | + return array( |
|
322 | + array( |
|
323 | + 'driver' => 'mysql', |
|
324 | + 'prefix' => 'EXPLAIN' |
|
325 | + ), |
|
326 | + array( |
|
327 | + 'driver' => 'sqlite', |
|
328 | + 'prefix' => 'EXPLAIN QUERY PLAN' |
|
329 | + ) |
|
330 | + ); |
|
331 | + } |
|
332 | + |
|
333 | + protected function getAccessibleMethod(PhpQuickProfiler $profiler, $methodName) |
|
334 | + { |
|
335 | + $reflectedConsole = new ReflectionClass(get_class($profiler)); |
|
336 | + $reflectedMethod = $reflectedConsole->getMethod($methodName); |
|
337 | + $reflectedMethod->setAccessible(true); |
|
338 | + return $reflectedMethod; |
|
339 | + } |
|
340 | + |
|
341 | + public static function tearDownAfterClass() |
|
342 | + { |
|
343 | + self::$dbConnection = null; |
|
344 | + } |
|
345 | 345 | } |
@@ -14,371 +14,371 @@ |
||
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 | - ); |
|
22 | - |
|
23 | - /** @var array */ |
|
24 | - protected $options; |
|
25 | - |
|
26 | - /** @var double */ |
|
27 | - protected $startTime; |
|
28 | - |
|
29 | - /** @var Console */ |
|
30 | - protected $console; |
|
31 | - |
|
32 | - /** @var array */ |
|
33 | - protected $speedData; |
|
34 | - |
|
35 | - /** @var array */ |
|
36 | - protected $queryData; |
|
37 | - |
|
38 | - /** @var array */ |
|
39 | - protected $memoryData; |
|
40 | - |
|
41 | - /** @var array */ |
|
42 | - protected $fileData; |
|
43 | - |
|
44 | - /** |
|
45 | - * @param array $options |
|
46 | - */ |
|
47 | - public function __construct(array $options = array()) |
|
48 | - { |
|
49 | - $options = array_intersect_key($options, $this->defaults); |
|
50 | - $this->options = array_replace($this->defaults, $options); |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * @param double $startTime |
|
55 | - */ |
|
56 | - public function setStartTime($startTime) |
|
57 | - { |
|
58 | - $this->startTime = $startTime; |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * @param Console $console |
|
63 | - */ |
|
64 | - public function setConsole(Console $console) |
|
65 | - { |
|
66 | - $this->console = $console; |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * Sets memory data |
|
71 | - * |
|
72 | - * @param array $data |
|
73 | - */ |
|
74 | - public function setMemoryData(array $data) |
|
75 | - { |
|
76 | - $this->memoryData = $data; |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * Sets query data |
|
81 | - * |
|
82 | - * @param array $data |
|
83 | - */ |
|
84 | - public function setQueryData(array $data) |
|
85 | - { |
|
86 | - $this->queryData = $data; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Sets speed data |
|
91 | - * |
|
92 | - * @param array $data |
|
93 | - */ |
|
94 | - public function setSpeedData(array $data) |
|
95 | - { |
|
96 | - $this->speedData = $data; |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Sets file data |
|
101 | - * |
|
102 | - * @param array $data |
|
103 | - */ |
|
104 | - public function setFileData(array $data) |
|
105 | - { |
|
106 | - $this->fileData = $data; |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * @return array |
|
111 | - */ |
|
112 | - protected function getConsoleMeta() |
|
113 | - { |
|
114 | - $consoleMeta = array( |
|
115 | - 'log' => 0, |
|
116 | - 'memory' => 0, |
|
117 | - 'error' => 0, |
|
118 | - 'speed' => 0 |
|
119 | - ); |
|
120 | - foreach ($this->console->getLogs() as $log) { |
|
121 | - if (array_key_exists($log['type'], $consoleMeta)) { |
|
122 | - $consoleMeta[$log['type']]++; |
|
123 | - } else { |
|
124 | - $consoleMeta['error']++; |
|
125 | - } |
|
126 | - } |
|
127 | - |
|
128 | - return $consoleMeta; |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * @return array |
|
133 | - */ |
|
134 | - protected function getConsoleMessages() |
|
135 | - { |
|
136 | - $messages = array(); |
|
137 | - foreach ($this->console->getLogs() as $log) { |
|
138 | - switch($log['type']) { |
|
139 | - case 'log': |
|
140 | - $message = array( |
|
141 | - 'message' => print_r($log['data'], true), |
|
142 | - 'type' => 'log' |
|
143 | - ); |
|
144 | - break; |
|
145 | - case 'memory': |
|
146 | - $message = array( |
|
147 | - 'message' => (!empty($log['data_type']) ? "{$log['data_type']}: " : '') . $log['name'], |
|
148 | - 'data' => $this->getReadableMemory($log['data']), |
|
149 | - 'type' => 'memory' |
|
150 | - ); |
|
151 | - break; |
|
152 | - case 'error': |
|
153 | - $message = array( |
|
154 | - 'message' => "Line {$log['line']}: {$log['data']} in {$log['file']}", |
|
155 | - 'type' => 'error' |
|
156 | - ); |
|
157 | - break; |
|
158 | - case 'speed': |
|
159 | - $elapsedTime = $log['data'] - $this->startTime; |
|
160 | - $message = array( |
|
161 | - 'message' => $log['name'], |
|
162 | - 'data' => $this->getReadableTime($elapsedTime), |
|
163 | - 'type' => 'speed' |
|
164 | - ); |
|
165 | - break; |
|
166 | - default: |
|
167 | - $message = array( |
|
168 | - 'message' => "Unrecognized console log type: {$log['type']}", |
|
169 | - 'type' => 'error' |
|
170 | - ); |
|
171 | - break; |
|
172 | - } |
|
173 | - array_push($messages, $message); |
|
174 | - } |
|
175 | - return $messages; |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * @return array |
|
180 | - */ |
|
181 | - protected function getSpeedMeta() |
|
182 | - { |
|
183 | - $elapsedTime = $this->getReadableTime($this->speedData['elapsed']); |
|
184 | - $allowedTime = $this->getReadableTime($this->speedData['allowed'], 0); |
|
185 | - |
|
186 | - return array( |
|
187 | - 'elapsed' => $elapsedTime, |
|
188 | - 'allowed' => $allowedTime, |
|
189 | - ); |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * @return array |
|
194 | - */ |
|
195 | - public function getQueryMeta() |
|
196 | - { |
|
197 | - $queryCount = count($this->queryData); |
|
198 | - $queryTotalTime = array_reduce($this->queryData, function ($sum, $row) { |
|
199 | - return $sum + $row['time']; |
|
200 | - }, 0); |
|
201 | - $queryTotalTime = $this->getReadableTime($queryTotalTime); |
|
202 | - $querySlowestTime = array_reduce($this->queryData, function ($slowest, $row) { |
|
203 | - return ($slowest < $row['time']) ? $row['time'] : $slowest; |
|
204 | - }, 0); |
|
205 | - $querySlowestTime = $this->getReadableTime($querySlowestTime); |
|
206 | - |
|
207 | - return array( |
|
208 | - 'count' => $queryCount, |
|
209 | - 'time' => $queryTotalTime, |
|
210 | - 'slowest' => $querySlowestTime |
|
211 | - ); |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * @return array |
|
216 | - */ |
|
217 | - public function getQueryList() |
|
218 | - { |
|
219 | - $queryList = array(); |
|
220 | - foreach ($this->queryData as $query) { |
|
221 | - array_push($queryList, array( |
|
222 | - 'message' => $query['sql'], |
|
223 | - 'sub_data' => array_filter($query['explain']), |
|
224 | - 'data' => $this->getReadableTime($query['time']) |
|
225 | - )); |
|
226 | - } |
|
227 | - return $queryList; |
|
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * @return array |
|
232 | - */ |
|
233 | - public function getMemoryMeta() |
|
234 | - { |
|
235 | - $usedMemory = $this->getReadableMemory($this->memoryData['used']); |
|
236 | - $allowedMemory = $this->memoryData['allowed']; // todo parse this, maybe? |
|
237 | - |
|
238 | - return array( |
|
239 | - 'used' => $usedMemory, |
|
240 | - 'allowed' => $allowedMemory |
|
241 | - ); |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * @return array |
|
246 | - */ |
|
247 | - protected function getFileMeta() |
|
248 | - { |
|
249 | - $fileCount = count($this->fileData); |
|
250 | - $fileTotalSize = array_reduce($this->fileData, function ($sum, $row) { |
|
251 | - return $sum + $row['size']; |
|
252 | - }, 0); |
|
253 | - $fileTotalSize = $this->getReadableMemory($fileTotalSize); |
|
254 | - $fileLargestSize = array_reduce($this->fileData, function ($largest, $row) { |
|
255 | - return ($largest < $row['size']) ? $row['size'] : $largest; |
|
256 | - }, 0); |
|
257 | - $fileLargestSize = $this->getReadableMemory($fileLargestSize); |
|
258 | - |
|
259 | - return array( |
|
260 | - 'count' => $fileCount, |
|
261 | - 'size' => $fileTotalSize, |
|
262 | - 'largest' => $fileLargestSize |
|
263 | - ); |
|
264 | - } |
|
265 | - |
|
266 | - /** |
|
267 | - * @return array |
|
268 | - */ |
|
269 | - protected function getFileList() |
|
270 | - { |
|
271 | - $fileList = array(); |
|
272 | - foreach ($this->fileData as $file) { |
|
273 | - array_push($fileList, array( |
|
274 | - 'message' => $file['name'], |
|
275 | - 'data' => $this->getReadableMemory($file['size']) |
|
276 | - )); |
|
277 | - } |
|
278 | - return $fileList; |
|
279 | - } |
|
280 | - |
|
281 | - /** |
|
282 | - * Formatter for human-readable time |
|
283 | - * Only handles time up to 60 minutes gracefully |
|
284 | - * |
|
285 | - * @param double $time |
|
286 | - * @param integer $percision |
|
287 | - * @return string |
|
288 | - */ |
|
289 | - protected function getReadableTime($time, $percision = 3) |
|
290 | - { |
|
291 | - $unit = 's'; |
|
292 | - if ($time < 1) { |
|
293 | - $time *= 1000; |
|
294 | - $unit = 'ms'; |
|
295 | - } else if ($time > 60) { |
|
296 | - $time /= 60; |
|
297 | - $unit = 'm'; |
|
298 | - } |
|
299 | - $time = number_format($time, $percision); |
|
300 | - return "{$time} {$unit}"; |
|
301 | - } |
|
302 | - |
|
303 | - /** |
|
304 | - * Formatter for human-readable memory |
|
305 | - * Only handles time up to a few gigs gracefully |
|
306 | - * |
|
307 | - * @param double $size |
|
308 | - * @param integer $percision |
|
309 | - */ |
|
310 | - protected function getReadableMemory($size, $percision = 2) |
|
311 | - { |
|
312 | - $unitOptions = array('b', 'k', 'M', 'G'); |
|
313 | - |
|
314 | - $base = log($size, 1024); |
|
315 | - |
|
316 | - $memory = round(pow(1024, $base - floor($base)), $percision); |
|
317 | - $unit = $unitOptions[floor($base)]; |
|
318 | - return "{$memory} {$unit}"; |
|
319 | - } |
|
320 | - |
|
321 | - /** |
|
322 | - * @param array $messages |
|
323 | - * @param string $type |
|
324 | - * @return array |
|
325 | - */ |
|
326 | - protected function filterMessages($messages, $type) |
|
327 | - { |
|
328 | - return array_filter($messages, function ($message) use ($type) { |
|
329 | - return $message['type'] == $type; |
|
330 | - }); |
|
331 | - } |
|
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 double */ |
|
27 | + protected $startTime; |
|
28 | + |
|
29 | + /** @var Console */ |
|
30 | + protected $console; |
|
31 | + |
|
32 | + /** @var array */ |
|
33 | + protected $speedData; |
|
34 | + |
|
35 | + /** @var array */ |
|
36 | + protected $queryData; |
|
37 | + |
|
38 | + /** @var array */ |
|
39 | + protected $memoryData; |
|
40 | + |
|
41 | + /** @var array */ |
|
42 | + protected $fileData; |
|
43 | + |
|
44 | + /** |
|
45 | + * @param array $options |
|
46 | + */ |
|
47 | + public function __construct(array $options = array()) |
|
48 | + { |
|
49 | + $options = array_intersect_key($options, $this->defaults); |
|
50 | + $this->options = array_replace($this->defaults, $options); |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * @param double $startTime |
|
55 | + */ |
|
56 | + public function setStartTime($startTime) |
|
57 | + { |
|
58 | + $this->startTime = $startTime; |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * @param Console $console |
|
63 | + */ |
|
64 | + public function setConsole(Console $console) |
|
65 | + { |
|
66 | + $this->console = $console; |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * Sets memory data |
|
71 | + * |
|
72 | + * @param array $data |
|
73 | + */ |
|
74 | + public function setMemoryData(array $data) |
|
75 | + { |
|
76 | + $this->memoryData = $data; |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * Sets query data |
|
81 | + * |
|
82 | + * @param array $data |
|
83 | + */ |
|
84 | + public function setQueryData(array $data) |
|
85 | + { |
|
86 | + $this->queryData = $data; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Sets speed data |
|
91 | + * |
|
92 | + * @param array $data |
|
93 | + */ |
|
94 | + public function setSpeedData(array $data) |
|
95 | + { |
|
96 | + $this->speedData = $data; |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Sets file data |
|
101 | + * |
|
102 | + * @param array $data |
|
103 | + */ |
|
104 | + public function setFileData(array $data) |
|
105 | + { |
|
106 | + $this->fileData = $data; |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * @return array |
|
111 | + */ |
|
112 | + protected function getConsoleMeta() |
|
113 | + { |
|
114 | + $consoleMeta = array( |
|
115 | + 'log' => 0, |
|
116 | + 'memory' => 0, |
|
117 | + 'error' => 0, |
|
118 | + 'speed' => 0 |
|
119 | + ); |
|
120 | + foreach ($this->console->getLogs() as $log) { |
|
121 | + if (array_key_exists($log['type'], $consoleMeta)) { |
|
122 | + $consoleMeta[$log['type']]++; |
|
123 | + } else { |
|
124 | + $consoleMeta['error']++; |
|
125 | + } |
|
126 | + } |
|
127 | + |
|
128 | + return $consoleMeta; |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * @return array |
|
133 | + */ |
|
134 | + protected function getConsoleMessages() |
|
135 | + { |
|
136 | + $messages = array(); |
|
137 | + foreach ($this->console->getLogs() as $log) { |
|
138 | + switch($log['type']) { |
|
139 | + case 'log': |
|
140 | + $message = array( |
|
141 | + 'message' => print_r($log['data'], true), |
|
142 | + 'type' => 'log' |
|
143 | + ); |
|
144 | + break; |
|
145 | + case 'memory': |
|
146 | + $message = array( |
|
147 | + 'message' => (!empty($log['data_type']) ? "{$log['data_type']}: " : '') . $log['name'], |
|
148 | + 'data' => $this->getReadableMemory($log['data']), |
|
149 | + 'type' => 'memory' |
|
150 | + ); |
|
151 | + break; |
|
152 | + case 'error': |
|
153 | + $message = array( |
|
154 | + 'message' => "Line {$log['line']}: {$log['data']} in {$log['file']}", |
|
155 | + 'type' => 'error' |
|
156 | + ); |
|
157 | + break; |
|
158 | + case 'speed': |
|
159 | + $elapsedTime = $log['data'] - $this->startTime; |
|
160 | + $message = array( |
|
161 | + 'message' => $log['name'], |
|
162 | + 'data' => $this->getReadableTime($elapsedTime), |
|
163 | + 'type' => 'speed' |
|
164 | + ); |
|
165 | + break; |
|
166 | + default: |
|
167 | + $message = array( |
|
168 | + 'message' => "Unrecognized console log type: {$log['type']}", |
|
169 | + 'type' => 'error' |
|
170 | + ); |
|
171 | + break; |
|
172 | + } |
|
173 | + array_push($messages, $message); |
|
174 | + } |
|
175 | + return $messages; |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * @return array |
|
180 | + */ |
|
181 | + protected function getSpeedMeta() |
|
182 | + { |
|
183 | + $elapsedTime = $this->getReadableTime($this->speedData['elapsed']); |
|
184 | + $allowedTime = $this->getReadableTime($this->speedData['allowed'], 0); |
|
185 | + |
|
186 | + return array( |
|
187 | + 'elapsed' => $elapsedTime, |
|
188 | + 'allowed' => $allowedTime, |
|
189 | + ); |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * @return array |
|
194 | + */ |
|
195 | + public function getQueryMeta() |
|
196 | + { |
|
197 | + $queryCount = count($this->queryData); |
|
198 | + $queryTotalTime = array_reduce($this->queryData, function ($sum, $row) { |
|
199 | + return $sum + $row['time']; |
|
200 | + }, 0); |
|
201 | + $queryTotalTime = $this->getReadableTime($queryTotalTime); |
|
202 | + $querySlowestTime = array_reduce($this->queryData, function ($slowest, $row) { |
|
203 | + return ($slowest < $row['time']) ? $row['time'] : $slowest; |
|
204 | + }, 0); |
|
205 | + $querySlowestTime = $this->getReadableTime($querySlowestTime); |
|
206 | + |
|
207 | + return array( |
|
208 | + 'count' => $queryCount, |
|
209 | + 'time' => $queryTotalTime, |
|
210 | + 'slowest' => $querySlowestTime |
|
211 | + ); |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * @return array |
|
216 | + */ |
|
217 | + public function getQueryList() |
|
218 | + { |
|
219 | + $queryList = array(); |
|
220 | + foreach ($this->queryData as $query) { |
|
221 | + array_push($queryList, array( |
|
222 | + 'message' => $query['sql'], |
|
223 | + 'sub_data' => array_filter($query['explain']), |
|
224 | + 'data' => $this->getReadableTime($query['time']) |
|
225 | + )); |
|
226 | + } |
|
227 | + return $queryList; |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * @return array |
|
232 | + */ |
|
233 | + public function getMemoryMeta() |
|
234 | + { |
|
235 | + $usedMemory = $this->getReadableMemory($this->memoryData['used']); |
|
236 | + $allowedMemory = $this->memoryData['allowed']; // todo parse this, maybe? |
|
237 | + |
|
238 | + return array( |
|
239 | + 'used' => $usedMemory, |
|
240 | + 'allowed' => $allowedMemory |
|
241 | + ); |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * @return array |
|
246 | + */ |
|
247 | + protected function getFileMeta() |
|
248 | + { |
|
249 | + $fileCount = count($this->fileData); |
|
250 | + $fileTotalSize = array_reduce($this->fileData, function ($sum, $row) { |
|
251 | + return $sum + $row['size']; |
|
252 | + }, 0); |
|
253 | + $fileTotalSize = $this->getReadableMemory($fileTotalSize); |
|
254 | + $fileLargestSize = array_reduce($this->fileData, function ($largest, $row) { |
|
255 | + return ($largest < $row['size']) ? $row['size'] : $largest; |
|
256 | + }, 0); |
|
257 | + $fileLargestSize = $this->getReadableMemory($fileLargestSize); |
|
258 | + |
|
259 | + return array( |
|
260 | + 'count' => $fileCount, |
|
261 | + 'size' => $fileTotalSize, |
|
262 | + 'largest' => $fileLargestSize |
|
263 | + ); |
|
264 | + } |
|
265 | + |
|
266 | + /** |
|
267 | + * @return array |
|
268 | + */ |
|
269 | + protected function getFileList() |
|
270 | + { |
|
271 | + $fileList = array(); |
|
272 | + foreach ($this->fileData as $file) { |
|
273 | + array_push($fileList, array( |
|
274 | + 'message' => $file['name'], |
|
275 | + 'data' => $this->getReadableMemory($file['size']) |
|
276 | + )); |
|
277 | + } |
|
278 | + return $fileList; |
|
279 | + } |
|
280 | + |
|
281 | + /** |
|
282 | + * Formatter for human-readable time |
|
283 | + * Only handles time up to 60 minutes gracefully |
|
284 | + * |
|
285 | + * @param double $time |
|
286 | + * @param integer $percision |
|
287 | + * @return string |
|
288 | + */ |
|
289 | + protected function getReadableTime($time, $percision = 3) |
|
290 | + { |
|
291 | + $unit = 's'; |
|
292 | + if ($time < 1) { |
|
293 | + $time *= 1000; |
|
294 | + $unit = 'ms'; |
|
295 | + } else if ($time > 60) { |
|
296 | + $time /= 60; |
|
297 | + $unit = 'm'; |
|
298 | + } |
|
299 | + $time = number_format($time, $percision); |
|
300 | + return "{$time} {$unit}"; |
|
301 | + } |
|
302 | + |
|
303 | + /** |
|
304 | + * Formatter for human-readable memory |
|
305 | + * Only handles time up to a few gigs gracefully |
|
306 | + * |
|
307 | + * @param double $size |
|
308 | + * @param integer $percision |
|
309 | + */ |
|
310 | + protected function getReadableMemory($size, $percision = 2) |
|
311 | + { |
|
312 | + $unitOptions = array('b', 'k', 'M', 'G'); |
|
313 | + |
|
314 | + $base = log($size, 1024); |
|
315 | + |
|
316 | + $memory = round(pow(1024, $base - floor($base)), $percision); |
|
317 | + $unit = $unitOptions[floor($base)]; |
|
318 | + return "{$memory} {$unit}"; |
|
319 | + } |
|
320 | + |
|
321 | + /** |
|
322 | + * @param array $messages |
|
323 | + * @param string $type |
|
324 | + * @return array |
|
325 | + */ |
|
326 | + protected function filterMessages($messages, $type) |
|
327 | + { |
|
328 | + return array_filter($messages, function ($message) use ($type) { |
|
329 | + return $message['type'] == $type; |
|
330 | + }); |
|
331 | + } |
|
332 | 332 | |
333 | - public function __invoke() |
|
334 | - { |
|
335 | - $consoleMeta = $this->getConsoleMeta(); |
|
336 | - $speedMeta = $this->getSpeedMeta(); |
|
337 | - $queryMeta = $this->getQueryMeta(); |
|
338 | - $memoryMeta = $this->getMemoryMeta(); |
|
339 | - $fileMeta = $this->getFileMeta(); |
|
340 | - |
|
341 | - $header = array( |
|
342 | - 'console' => array_sum($consoleMeta), |
|
343 | - 'speed' => $speedMeta['elapsed'], |
|
344 | - 'query' => $queryMeta['count'], |
|
345 | - 'memory' => $memoryMeta['used'], |
|
346 | - 'files' => $fileMeta['count'] |
|
347 | - ); |
|
348 | - |
|
349 | - $consoleMessages = $this->getConsoleMessages(); |
|
350 | - $queryList = $this->getQueryList(); |
|
351 | - $fileList = $this->getFileList(); |
|
352 | - |
|
353 | - $console = array( |
|
354 | - 'meta' => $consoleMeta, |
|
355 | - 'messages' => $consoleMessages |
|
356 | - ); |
|
357 | - |
|
358 | - $speed = array( |
|
359 | - 'meta' => $speedMeta, |
|
360 | - 'messages' => $this->filterMessages($consoleMessages, 'speed') |
|
361 | - ); |
|
362 | - |
|
363 | - $query = array( |
|
364 | - 'meta' => $queryMeta, |
|
365 | - 'messages' => $queryList |
|
366 | - ); |
|
367 | - |
|
368 | - $memory = array( |
|
369 | - 'meta' => $memoryMeta, |
|
370 | - 'messages' => $this->filterMessages($consoleMessages, 'memory') |
|
371 | - ); |
|
372 | - |
|
373 | - $files = array( |
|
374 | - 'meta' => $fileMeta, |
|
375 | - 'messages' => $fileList |
|
376 | - ); |
|
377 | - |
|
378 | - // todo is this really the best way to load these? |
|
379 | - $styles = file_get_contents(__DIR__ . "/../{$this->options['style_path']}"); |
|
380 | - $script = file_get_contents(__DIR__ . "/../{$this->options['script_path']}"); |
|
381 | - |
|
382 | - require_once __DIR__ .'/../asset/display.html'; |
|
383 | - } |
|
333 | + public function __invoke() |
|
334 | + { |
|
335 | + $consoleMeta = $this->getConsoleMeta(); |
|
336 | + $speedMeta = $this->getSpeedMeta(); |
|
337 | + $queryMeta = $this->getQueryMeta(); |
|
338 | + $memoryMeta = $this->getMemoryMeta(); |
|
339 | + $fileMeta = $this->getFileMeta(); |
|
340 | + |
|
341 | + $header = array( |
|
342 | + 'console' => array_sum($consoleMeta), |
|
343 | + 'speed' => $speedMeta['elapsed'], |
|
344 | + 'query' => $queryMeta['count'], |
|
345 | + 'memory' => $memoryMeta['used'], |
|
346 | + 'files' => $fileMeta['count'] |
|
347 | + ); |
|
348 | + |
|
349 | + $consoleMessages = $this->getConsoleMessages(); |
|
350 | + $queryList = $this->getQueryList(); |
|
351 | + $fileList = $this->getFileList(); |
|
352 | + |
|
353 | + $console = array( |
|
354 | + 'meta' => $consoleMeta, |
|
355 | + 'messages' => $consoleMessages |
|
356 | + ); |
|
357 | + |
|
358 | + $speed = array( |
|
359 | + 'meta' => $speedMeta, |
|
360 | + 'messages' => $this->filterMessages($consoleMessages, 'speed') |
|
361 | + ); |
|
362 | + |
|
363 | + $query = array( |
|
364 | + 'meta' => $queryMeta, |
|
365 | + 'messages' => $queryList |
|
366 | + ); |
|
367 | + |
|
368 | + $memory = array( |
|
369 | + 'meta' => $memoryMeta, |
|
370 | + 'messages' => $this->filterMessages($consoleMessages, 'memory') |
|
371 | + ); |
|
372 | + |
|
373 | + $files = array( |
|
374 | + 'meta' => $fileMeta, |
|
375 | + 'messages' => $fileList |
|
376 | + ); |
|
377 | + |
|
378 | + // todo is this really the best way to load these? |
|
379 | + $styles = file_get_contents(__DIR__ . "/../{$this->options['style_path']}"); |
|
380 | + $script = file_get_contents(__DIR__ . "/../{$this->options['script_path']}"); |
|
381 | + |
|
382 | + require_once __DIR__ .'/../asset/display.html'; |
|
383 | + } |
|
384 | 384 | } |
@@ -135,7 +135,7 @@ discard block |
||
135 | 135 | { |
136 | 136 | $messages = array(); |
137 | 137 | foreach ($this->console->getLogs() as $log) { |
138 | - switch($log['type']) { |
|
138 | + switch ($log['type']) { |
|
139 | 139 | case 'log': |
140 | 140 | $message = array( |
141 | 141 | 'message' => print_r($log['data'], true), |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | break; |
145 | 145 | case 'memory': |
146 | 146 | $message = array( |
147 | - 'message' => (!empty($log['data_type']) ? "{$log['data_type']}: " : '') . $log['name'], |
|
147 | + 'message' => (!empty($log['data_type']) ? "{$log['data_type']}: " : '').$log['name'], |
|
148 | 148 | 'data' => $this->getReadableMemory($log['data']), |
149 | 149 | 'type' => 'memory' |
150 | 150 | ); |
@@ -195,11 +195,11 @@ discard block |
||
195 | 195 | public function getQueryMeta() |
196 | 196 | { |
197 | 197 | $queryCount = count($this->queryData); |
198 | - $queryTotalTime = array_reduce($this->queryData, function ($sum, $row) { |
|
198 | + $queryTotalTime = array_reduce($this->queryData, function($sum, $row) { |
|
199 | 199 | return $sum + $row['time']; |
200 | 200 | }, 0); |
201 | 201 | $queryTotalTime = $this->getReadableTime($queryTotalTime); |
202 | - $querySlowestTime = array_reduce($this->queryData, function ($slowest, $row) { |
|
202 | + $querySlowestTime = array_reduce($this->queryData, function($slowest, $row) { |
|
203 | 203 | return ($slowest < $row['time']) ? $row['time'] : $slowest; |
204 | 204 | }, 0); |
205 | 205 | $querySlowestTime = $this->getReadableTime($querySlowestTime); |
@@ -247,11 +247,11 @@ discard block |
||
247 | 247 | protected function getFileMeta() |
248 | 248 | { |
249 | 249 | $fileCount = count($this->fileData); |
250 | - $fileTotalSize = array_reduce($this->fileData, function ($sum, $row) { |
|
250 | + $fileTotalSize = array_reduce($this->fileData, function($sum, $row) { |
|
251 | 251 | return $sum + $row['size']; |
252 | 252 | }, 0); |
253 | 253 | $fileTotalSize = $this->getReadableMemory($fileTotalSize); |
254 | - $fileLargestSize = array_reduce($this->fileData, function ($largest, $row) { |
|
254 | + $fileLargestSize = array_reduce($this->fileData, function($largest, $row) { |
|
255 | 255 | return ($largest < $row['size']) ? $row['size'] : $largest; |
256 | 256 | }, 0); |
257 | 257 | $fileLargestSize = $this->getReadableMemory($fileLargestSize); |
@@ -325,7 +325,7 @@ discard block |
||
325 | 325 | */ |
326 | 326 | protected function filterMessages($messages, $type) |
327 | 327 | { |
328 | - return array_filter($messages, function ($message) use ($type) { |
|
328 | + return array_filter($messages, function($message) use ($type) { |
|
329 | 329 | return $message['type'] == $type; |
330 | 330 | }); |
331 | 331 | } |
@@ -376,9 +376,9 @@ discard block |
||
376 | 376 | ); |
377 | 377 | |
378 | 378 | // todo is this really the best way to load these? |
379 | - $styles = file_get_contents(__DIR__ . "/../{$this->options['style_path']}"); |
|
380 | - $script = file_get_contents(__DIR__ . "/../{$this->options['script_path']}"); |
|
379 | + $styles = file_get_contents(__DIR__."/../{$this->options['style_path']}"); |
|
380 | + $script = file_get_contents(__DIR__."/../{$this->options['script_path']}"); |
|
381 | 381 | |
382 | - require_once __DIR__ .'/../asset/display.html'; |
|
382 | + require_once __DIR__.'/../asset/display.html'; |
|
383 | 383 | } |
384 | 384 | } |