@@ -8,64 +8,64 @@ |
||
| 8 | 8 | class DisplayTest extends PHPUnit_Framework_TestCase |
| 9 | 9 | { |
| 10 | 10 | |
| 11 | - public function testConstruct() |
|
| 12 | - { |
|
| 13 | - $display = new Display(); |
|
| 14 | - $reflectedDisplay = new ReflectionClass(get_class($display)); |
|
| 15 | - $reflectedProperty = $reflectedDisplay->getProperty('defaults'); |
|
| 16 | - $reflectedProperty->setAccessible(true); |
|
| 17 | - $defaults = $reflectedProperty->getValue($display); |
|
| 11 | + public function testConstruct() |
|
| 12 | + { |
|
| 13 | + $display = new Display(); |
|
| 14 | + $reflectedDisplay = new ReflectionClass(get_class($display)); |
|
| 15 | + $reflectedProperty = $reflectedDisplay->getProperty('defaults'); |
|
| 16 | + $reflectedProperty->setAccessible(true); |
|
| 17 | + $defaults = $reflectedProperty->getValue($display); |
|
| 18 | 18 | |
| 19 | - $display = new Display(); |
|
| 20 | - $this->assertAttributeEquals($defaults, 'options', $display); |
|
| 19 | + $display = new Display(); |
|
| 20 | + $this->assertAttributeEquals($defaults, 'options', $display); |
|
| 21 | 21 | |
| 22 | - $options = array( |
|
| 23 | - 'script_path' => 'testing/testing.js', |
|
| 24 | - 'fake_key' => 'foo bar' |
|
| 25 | - ); |
|
| 26 | - $expectedOptions = array_intersect_key($options, $defaults); |
|
| 27 | - $expectedOptions = array_replace($defaults, $expectedOptions); |
|
| 28 | - $display = new Display($options); |
|
| 29 | - $this->assertAttributeEquals($expectedOptions, 'options', $display); |
|
| 30 | - } |
|
| 22 | + $options = array( |
|
| 23 | + 'script_path' => 'testing/testing.js', |
|
| 24 | + 'fake_key' => 'foo bar' |
|
| 25 | + ); |
|
| 26 | + $expectedOptions = array_intersect_key($options, $defaults); |
|
| 27 | + $expectedOptions = array_replace($defaults, $expectedOptions); |
|
| 28 | + $display = new Display($options); |
|
| 29 | + $this->assertAttributeEquals($expectedOptions, 'options', $display); |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - public function testGetReadableTime() |
|
| 33 | - { |
|
| 34 | - $timeTest = array( |
|
| 35 | - '.032432' => '32.432 ms', |
|
| 36 | - '24.3781' => '24.378 s', |
|
| 37 | - '145.123' => '2.419 m' |
|
| 38 | - ); |
|
| 39 | - $display = new Display(); |
|
| 40 | - $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableTime'); |
|
| 32 | + public function testGetReadableTime() |
|
| 33 | + { |
|
| 34 | + $timeTest = array( |
|
| 35 | + '.032432' => '32.432 ms', |
|
| 36 | + '24.3781' => '24.378 s', |
|
| 37 | + '145.123' => '2.419 m' |
|
| 38 | + ); |
|
| 39 | + $display = new Display(); |
|
| 40 | + $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableTime'); |
|
| 41 | 41 | |
| 42 | - foreach ($timeTest as $rawTime => $expectedTime) { |
|
| 43 | - $readableTime = $reflectedMethod->invokeArgs($display, array($rawTime)); |
|
| 44 | - $this->assertEquals($expectedTime, $readableTime); |
|
| 45 | - } |
|
| 46 | - } |
|
| 42 | + foreach ($timeTest as $rawTime => $expectedTime) { |
|
| 43 | + $readableTime = $reflectedMethod->invokeArgs($display, array($rawTime)); |
|
| 44 | + $this->assertEquals($expectedTime, $readableTime); |
|
| 45 | + } |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - public function testGetReadableMemory() |
|
| 49 | - { |
|
| 50 | - $memoryTest = array( |
|
| 51 | - '314' => '314 b', |
|
| 52 | - '7403' => '7.23 k', |
|
| 53 | - '2589983' => '2.47 M' |
|
| 54 | - ); |
|
| 55 | - $display = new Display(); |
|
| 56 | - $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableMemory'); |
|
| 48 | + public function testGetReadableMemory() |
|
| 49 | + { |
|
| 50 | + $memoryTest = array( |
|
| 51 | + '314' => '314 b', |
|
| 52 | + '7403' => '7.23 k', |
|
| 53 | + '2589983' => '2.47 M' |
|
| 54 | + ); |
|
| 55 | + $display = new Display(); |
|
| 56 | + $reflectedMethod = $this->getAccessibleMethod($display, 'getReadableMemory'); |
|
| 57 | 57 | |
| 58 | - foreach ($memoryTest as $rawMemory => $expectedMemory) { |
|
| 59 | - $readableMemory = $reflectedMethod->invokeArgs($display, array($rawMemory)); |
|
| 60 | - $this->assertEquals($expectedMemory, $readableMemory); |
|
| 61 | - } |
|
| 62 | - } |
|
| 58 | + foreach ($memoryTest as $rawMemory => $expectedMemory) { |
|
| 59 | + $readableMemory = $reflectedMethod->invokeArgs($display, array($rawMemory)); |
|
| 60 | + $this->assertEquals($expectedMemory, $readableMemory); |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - protected function getAccessibleMethod(Display $display, $methodName) |
|
| 65 | - { |
|
| 66 | - $reflectedConsole = new ReflectionClass(get_class($display)); |
|
| 67 | - $reflectedMethod = $reflectedConsole->getMethod($methodName); |
|
| 68 | - $reflectedMethod->setAccessible(true); |
|
| 69 | - return $reflectedMethod; |
|
| 70 | - } |
|
| 64 | + protected function getAccessibleMethod(Display $display, $methodName) |
|
| 65 | + { |
|
| 66 | + $reflectedConsole = new ReflectionClass(get_class($display)); |
|
| 67 | + $reflectedMethod = $reflectedConsole->getMethod($methodName); |
|
| 68 | + $reflectedMethod->setAccessible(true); |
|
| 69 | + return $reflectedMethod; |
|
| 70 | + } |
|
| 71 | 71 | } |
@@ -14,305 +14,305 @@ |
||
| 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 Console */ |
|
| 27 | - protected $console; |
|
| 28 | - |
|
| 29 | - /** @var array */ |
|
| 30 | - protected $speedData; |
|
| 31 | - |
|
| 32 | - /** @var array */ |
|
| 33 | - protected $queryData; |
|
| 34 | - |
|
| 35 | - /** @var array */ |
|
| 36 | - protected $memoryData; |
|
| 37 | - |
|
| 38 | - /** @var array */ |
|
| 39 | - protected $fileData; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @param array $options |
|
| 43 | - */ |
|
| 44 | - public function __construct(array $options = array()) |
|
| 45 | - { |
|
| 46 | - $options = array_intersect_key($options, $this->defaults); |
|
| 47 | - $this->options = array_replace($this->defaults, $options); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @param Console $console |
|
| 52 | - */ |
|
| 53 | - public function setConsole(Console $console) |
|
| 54 | - { |
|
| 55 | - $this->console = $console; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @return array |
|
| 60 | - */ |
|
| 61 | - protected function formatConsoleData() |
|
| 62 | - { |
|
| 63 | - $console_data = array( |
|
| 64 | - 'messages' => array(), |
|
| 65 | - 'meta' => array( |
|
| 66 | - 'log' => 0, |
|
| 67 | - 'memory' => 0, |
|
| 68 | - 'error' => 0, |
|
| 69 | - 'speed' => 0 |
|
| 70 | - ) |
|
| 71 | - ); |
|
| 72 | - foreach ($this->console->getLogs() as $log) { |
|
| 73 | - switch($log['type']) { |
|
| 74 | - case 'log': |
|
| 75 | - $message = array( |
|
| 76 | - 'message' => print_r($log['data'], true), |
|
| 77 | - 'type' => 'log' |
|
| 78 | - ); |
|
| 79 | - $console_data['meta']['log']++; |
|
| 80 | - break; |
|
| 81 | - case 'memory': |
|
| 82 | - $message = array( |
|
| 83 | - 'message' => (!empty($log['data_type']) ? "{$log['data_type']}: " : '') . $log['name'], |
|
| 84 | - 'data' => $this->getReadableMemory($log['data']), |
|
| 85 | - 'type' => 'memory' |
|
| 86 | - ); |
|
| 87 | - $console_data['meta']['memory']++; |
|
| 88 | - break; |
|
| 89 | - case 'error': |
|
| 90 | - $message = array( |
|
| 91 | - 'message' => "Line {$log['line']}: {$log['data']} in {$log['file']}", |
|
| 92 | - 'type' => 'error' |
|
| 93 | - ); |
|
| 94 | - $console_data['meta']['error']++; |
|
| 95 | - break; |
|
| 96 | - case 'speed': |
|
| 97 | - $elapsedTime = $log['data'] - $this->startTime; |
|
| 98 | - $message = array( |
|
| 99 | - 'message' => $log['name'], |
|
| 100 | - 'data' => $this->getReadableTime($elapsedTime), |
|
| 101 | - 'type' => 'speed' |
|
| 102 | - ); |
|
| 103 | - $console_data['meta']['speed']++; |
|
| 104 | - break; |
|
| 105 | - default: |
|
| 106 | - $message = array( |
|
| 107 | - 'message' => "Unrecognized console log type: {$log['type']}", |
|
| 108 | - 'type' => 'error' |
|
| 109 | - ); |
|
| 110 | - $console_data['meta']['error']++; |
|
| 111 | - break; |
|
| 112 | - } |
|
| 113 | - array_push($console_data['messages'], $message); |
|
| 114 | - } |
|
| 115 | - return $console_data; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Sets file data |
|
| 120 | - * |
|
| 121 | - * @param array $data |
|
| 122 | - */ |
|
| 123 | - public function setFileData(array $data) |
|
| 124 | - { |
|
| 125 | - $this->fileData = $data; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * @return array |
|
| 130 | - */ |
|
| 131 | - protected function formatFileData() |
|
| 132 | - { |
|
| 133 | - $fileData = array( |
|
| 134 | - 'messages' => array(), |
|
| 135 | - 'meta' => array( |
|
| 136 | - 'count' => count($this->fileData), |
|
| 137 | - 'size' => 0, |
|
| 138 | - 'largest' => 0 |
|
| 139 | - ) |
|
| 140 | - ); |
|
| 141 | - |
|
| 142 | - foreach ($this->fileData as $file) { |
|
| 143 | - array_push($fileData['messages'], array( |
|
| 144 | - 'message' => $file['name'], |
|
| 145 | - 'data' => $this->getReadableMemory($file['size']) |
|
| 146 | - )); |
|
| 147 | - |
|
| 148 | - $fileData['meta']['size'] += $file['size']; |
|
| 149 | - if ($file['size'] > $fileData['meta']['largest']) { |
|
| 150 | - $fileData['meta']['largest'] = $file['size']; |
|
| 151 | - } |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - $fileData['meta']['size'] = $this->getReadableMemory($fileData['meta']['size']); |
|
| 155 | - $fileData['meta']['largest'] = $this->getReadableMemory($fileData['meta']['largest']); |
|
| 156 | - |
|
| 157 | - return $fileData; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Sets memory data |
|
| 162 | - * |
|
| 163 | - * @param array $data |
|
| 164 | - */ |
|
| 165 | - public function setMemoryData(array $data) |
|
| 166 | - { |
|
| 167 | - $this->memoryData = $data; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * @return array |
|
| 172 | - */ |
|
| 173 | - public function formatMemoryData() |
|
| 174 | - { |
|
| 175 | - return array( |
|
| 176 | - 'meta' => array( |
|
| 177 | - 'used' => $this->getReadableMemory($this->memoryData['used']), |
|
| 178 | - 'allowed' => $this->memoryData['allowed'] |
|
| 179 | - ) |
|
| 180 | - ); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Sets query data |
|
| 185 | - * |
|
| 186 | - * @param array $data |
|
| 187 | - */ |
|
| 188 | - public function setQueryData(array $data) |
|
| 189 | - { |
|
| 190 | - $this->queryData = $data; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * @return array |
|
| 195 | - */ |
|
| 196 | - public function formatQueryData() |
|
| 197 | - { |
|
| 198 | - $queryData = array( |
|
| 199 | - 'messages' => array(), |
|
| 200 | - 'meta' => array( |
|
| 201 | - 'count' => count($this->queryData), |
|
| 202 | - 'time' => 0, |
|
| 203 | - 'slowest' => 0 |
|
| 204 | - ) |
|
| 205 | - ); |
|
| 206 | - |
|
| 207 | - foreach ($this->queryData as $query) { |
|
| 208 | - array_push($queryData['messages'], array( |
|
| 209 | - 'message' => $query['sql'], |
|
| 210 | - 'sub_data' => array_filter($query['explain']), |
|
| 211 | - 'data' => $this->getReadableTime($query['time']) |
|
| 212 | - )); |
|
| 213 | - $queryData['meta']['time'] += $query['time']; |
|
| 214 | - if ($query['time'] > $queryData['meta']['slowest']) { |
|
| 215 | - $queryData['meta']['slowest'] = $query['time']; |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - $queryData['meta']['time'] = $this->getReadableTime($queryData['meta']['time']); |
|
| 220 | - $queryData['meta']['slowest'] = $this->getReadableTime($queryData['meta']['slowest']); |
|
| 221 | - |
|
| 222 | - return $queryData; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Sets speed data |
|
| 227 | - * |
|
| 228 | - * @param array $data |
|
| 229 | - */ |
|
| 230 | - public function setSpeedData(array $data) |
|
| 231 | - { |
|
| 232 | - $this->speedData = $data; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * @return array |
|
| 237 | - */ |
|
| 238 | - protected function formatSpeedData() |
|
| 239 | - { |
|
| 240 | - return array( |
|
| 241 | - 'meta' => array( |
|
| 242 | - 'elapsed' => $this->getReadableTime($this->speedData['elapsed']), |
|
| 243 | - 'allowed' => $this->getReadableTime($this->speedData['allowed'], 0) |
|
| 244 | - ) |
|
| 245 | - ); |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - /** |
|
| 249 | - * Formatter for human-readable time |
|
| 250 | - * Only handles time up to 60 minutes gracefully |
|
| 251 | - * |
|
| 252 | - * @param double $time |
|
| 253 | - * @param integer $percision |
|
| 254 | - * @return string |
|
| 255 | - */ |
|
| 256 | - protected function getReadableTime($time, $percision = 3) |
|
| 257 | - { |
|
| 258 | - $unit = 's'; |
|
| 259 | - if ($time < 1) { |
|
| 260 | - $time *= 1000; |
|
| 261 | - $unit = 'ms'; |
|
| 262 | - } else if ($time > 60) { |
|
| 263 | - $time /= 60; |
|
| 264 | - $unit = 'm'; |
|
| 265 | - } |
|
| 266 | - $time = number_format($time, $percision); |
|
| 267 | - return "{$time} {$unit}"; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * Formatter for human-readable memory |
|
| 272 | - * Only handles time up to a few gigs gracefully |
|
| 273 | - * |
|
| 274 | - * @param double $size |
|
| 275 | - * @param integer $percision |
|
| 276 | - */ |
|
| 277 | - protected function getReadableMemory($size, $percision = 2) |
|
| 278 | - { |
|
| 279 | - $unitOptions = array('b', 'k', 'M', 'G'); |
|
| 280 | - |
|
| 281 | - $base = log($size, 1024); |
|
| 282 | - |
|
| 283 | - $memory = round(pow(1024, $base - floor($base)), $percision); |
|
| 284 | - $unit = $unitOptions[floor($base)]; |
|
| 285 | - return "{$memory} {$unit}"; |
|
| 286 | - } |
|
| 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 Console */ |
|
| 27 | + protected $console; |
|
| 28 | + |
|
| 29 | + /** @var array */ |
|
| 30 | + protected $speedData; |
|
| 31 | + |
|
| 32 | + /** @var array */ |
|
| 33 | + protected $queryData; |
|
| 34 | + |
|
| 35 | + /** @var array */ |
|
| 36 | + protected $memoryData; |
|
| 37 | + |
|
| 38 | + /** @var array */ |
|
| 39 | + protected $fileData; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @param array $options |
|
| 43 | + */ |
|
| 44 | + public function __construct(array $options = array()) |
|
| 45 | + { |
|
| 46 | + $options = array_intersect_key($options, $this->defaults); |
|
| 47 | + $this->options = array_replace($this->defaults, $options); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @param Console $console |
|
| 52 | + */ |
|
| 53 | + public function setConsole(Console $console) |
|
| 54 | + { |
|
| 55 | + $this->console = $console; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @return array |
|
| 60 | + */ |
|
| 61 | + protected function formatConsoleData() |
|
| 62 | + { |
|
| 63 | + $console_data = array( |
|
| 64 | + 'messages' => array(), |
|
| 65 | + 'meta' => array( |
|
| 66 | + 'log' => 0, |
|
| 67 | + 'memory' => 0, |
|
| 68 | + 'error' => 0, |
|
| 69 | + 'speed' => 0 |
|
| 70 | + ) |
|
| 71 | + ); |
|
| 72 | + foreach ($this->console->getLogs() as $log) { |
|
| 73 | + switch($log['type']) { |
|
| 74 | + case 'log': |
|
| 75 | + $message = array( |
|
| 76 | + 'message' => print_r($log['data'], true), |
|
| 77 | + 'type' => 'log' |
|
| 78 | + ); |
|
| 79 | + $console_data['meta']['log']++; |
|
| 80 | + break; |
|
| 81 | + case 'memory': |
|
| 82 | + $message = array( |
|
| 83 | + 'message' => (!empty($log['data_type']) ? "{$log['data_type']}: " : '') . $log['name'], |
|
| 84 | + 'data' => $this->getReadableMemory($log['data']), |
|
| 85 | + 'type' => 'memory' |
|
| 86 | + ); |
|
| 87 | + $console_data['meta']['memory']++; |
|
| 88 | + break; |
|
| 89 | + case 'error': |
|
| 90 | + $message = array( |
|
| 91 | + 'message' => "Line {$log['line']}: {$log['data']} in {$log['file']}", |
|
| 92 | + 'type' => 'error' |
|
| 93 | + ); |
|
| 94 | + $console_data['meta']['error']++; |
|
| 95 | + break; |
|
| 96 | + case 'speed': |
|
| 97 | + $elapsedTime = $log['data'] - $this->startTime; |
|
| 98 | + $message = array( |
|
| 99 | + 'message' => $log['name'], |
|
| 100 | + 'data' => $this->getReadableTime($elapsedTime), |
|
| 101 | + 'type' => 'speed' |
|
| 102 | + ); |
|
| 103 | + $console_data['meta']['speed']++; |
|
| 104 | + break; |
|
| 105 | + default: |
|
| 106 | + $message = array( |
|
| 107 | + 'message' => "Unrecognized console log type: {$log['type']}", |
|
| 108 | + 'type' => 'error' |
|
| 109 | + ); |
|
| 110 | + $console_data['meta']['error']++; |
|
| 111 | + break; |
|
| 112 | + } |
|
| 113 | + array_push($console_data['messages'], $message); |
|
| 114 | + } |
|
| 115 | + return $console_data; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Sets file data |
|
| 120 | + * |
|
| 121 | + * @param array $data |
|
| 122 | + */ |
|
| 123 | + public function setFileData(array $data) |
|
| 124 | + { |
|
| 125 | + $this->fileData = $data; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * @return array |
|
| 130 | + */ |
|
| 131 | + protected function formatFileData() |
|
| 132 | + { |
|
| 133 | + $fileData = array( |
|
| 134 | + 'messages' => array(), |
|
| 135 | + 'meta' => array( |
|
| 136 | + 'count' => count($this->fileData), |
|
| 137 | + 'size' => 0, |
|
| 138 | + 'largest' => 0 |
|
| 139 | + ) |
|
| 140 | + ); |
|
| 141 | + |
|
| 142 | + foreach ($this->fileData as $file) { |
|
| 143 | + array_push($fileData['messages'], array( |
|
| 144 | + 'message' => $file['name'], |
|
| 145 | + 'data' => $this->getReadableMemory($file['size']) |
|
| 146 | + )); |
|
| 147 | + |
|
| 148 | + $fileData['meta']['size'] += $file['size']; |
|
| 149 | + if ($file['size'] > $fileData['meta']['largest']) { |
|
| 150 | + $fileData['meta']['largest'] = $file['size']; |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + $fileData['meta']['size'] = $this->getReadableMemory($fileData['meta']['size']); |
|
| 155 | + $fileData['meta']['largest'] = $this->getReadableMemory($fileData['meta']['largest']); |
|
| 156 | + |
|
| 157 | + return $fileData; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Sets memory data |
|
| 162 | + * |
|
| 163 | + * @param array $data |
|
| 164 | + */ |
|
| 165 | + public function setMemoryData(array $data) |
|
| 166 | + { |
|
| 167 | + $this->memoryData = $data; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * @return array |
|
| 172 | + */ |
|
| 173 | + public function formatMemoryData() |
|
| 174 | + { |
|
| 175 | + return array( |
|
| 176 | + 'meta' => array( |
|
| 177 | + 'used' => $this->getReadableMemory($this->memoryData['used']), |
|
| 178 | + 'allowed' => $this->memoryData['allowed'] |
|
| 179 | + ) |
|
| 180 | + ); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Sets query data |
|
| 185 | + * |
|
| 186 | + * @param array $data |
|
| 187 | + */ |
|
| 188 | + public function setQueryData(array $data) |
|
| 189 | + { |
|
| 190 | + $this->queryData = $data; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * @return array |
|
| 195 | + */ |
|
| 196 | + public function formatQueryData() |
|
| 197 | + { |
|
| 198 | + $queryData = array( |
|
| 199 | + 'messages' => array(), |
|
| 200 | + 'meta' => array( |
|
| 201 | + 'count' => count($this->queryData), |
|
| 202 | + 'time' => 0, |
|
| 203 | + 'slowest' => 0 |
|
| 204 | + ) |
|
| 205 | + ); |
|
| 206 | + |
|
| 207 | + foreach ($this->queryData as $query) { |
|
| 208 | + array_push($queryData['messages'], array( |
|
| 209 | + 'message' => $query['sql'], |
|
| 210 | + 'sub_data' => array_filter($query['explain']), |
|
| 211 | + 'data' => $this->getReadableTime($query['time']) |
|
| 212 | + )); |
|
| 213 | + $queryData['meta']['time'] += $query['time']; |
|
| 214 | + if ($query['time'] > $queryData['meta']['slowest']) { |
|
| 215 | + $queryData['meta']['slowest'] = $query['time']; |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + $queryData['meta']['time'] = $this->getReadableTime($queryData['meta']['time']); |
|
| 220 | + $queryData['meta']['slowest'] = $this->getReadableTime($queryData['meta']['slowest']); |
|
| 221 | + |
|
| 222 | + return $queryData; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Sets speed data |
|
| 227 | + * |
|
| 228 | + * @param array $data |
|
| 229 | + */ |
|
| 230 | + public function setSpeedData(array $data) |
|
| 231 | + { |
|
| 232 | + $this->speedData = $data; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * @return array |
|
| 237 | + */ |
|
| 238 | + protected function formatSpeedData() |
|
| 239 | + { |
|
| 240 | + return array( |
|
| 241 | + 'meta' => array( |
|
| 242 | + 'elapsed' => $this->getReadableTime($this->speedData['elapsed']), |
|
| 243 | + 'allowed' => $this->getReadableTime($this->speedData['allowed'], 0) |
|
| 244 | + ) |
|
| 245 | + ); |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + /** |
|
| 249 | + * Formatter for human-readable time |
|
| 250 | + * Only handles time up to 60 minutes gracefully |
|
| 251 | + * |
|
| 252 | + * @param double $time |
|
| 253 | + * @param integer $percision |
|
| 254 | + * @return string |
|
| 255 | + */ |
|
| 256 | + protected function getReadableTime($time, $percision = 3) |
|
| 257 | + { |
|
| 258 | + $unit = 's'; |
|
| 259 | + if ($time < 1) { |
|
| 260 | + $time *= 1000; |
|
| 261 | + $unit = 'ms'; |
|
| 262 | + } else if ($time > 60) { |
|
| 263 | + $time /= 60; |
|
| 264 | + $unit = 'm'; |
|
| 265 | + } |
|
| 266 | + $time = number_format($time, $percision); |
|
| 267 | + return "{$time} {$unit}"; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * Formatter for human-readable memory |
|
| 272 | + * Only handles time up to a few gigs gracefully |
|
| 273 | + * |
|
| 274 | + * @param double $size |
|
| 275 | + * @param integer $percision |
|
| 276 | + */ |
|
| 277 | + protected function getReadableMemory($size, $percision = 2) |
|
| 278 | + { |
|
| 279 | + $unitOptions = array('b', 'k', 'M', 'G'); |
|
| 280 | + |
|
| 281 | + $base = log($size, 1024); |
|
| 282 | + |
|
| 283 | + $memory = round(pow(1024, $base - floor($base)), $percision); |
|
| 284 | + $unit = $unitOptions[floor($base)]; |
|
| 285 | + return "{$memory} {$unit}"; |
|
| 286 | + } |
|
| 287 | 287 | |
| 288 | - public function __invoke() |
|
| 289 | - { |
|
| 290 | - $console= $this->formatConsoleData(); |
|
| 291 | - $speed= $this->formatSpeedData(); |
|
| 292 | - $query= $this->formatQueryData(); |
|
| 293 | - $memory= $this->formatMemoryData(); |
|
| 294 | - $files= $this->formatFileData(); |
|
| 295 | - |
|
| 296 | - $header = array( |
|
| 297 | - 'console' => count($console['messages']), |
|
| 298 | - 'speed' => $speed['meta']['elapsed'], |
|
| 299 | - 'query' => $query['meta']['count'], |
|
| 300 | - 'memory' => $memory['meta']['used'], |
|
| 301 | - 'files' => $files['meta']['count'] |
|
| 302 | - ); |
|
| 303 | - |
|
| 304 | - $speed['messages'] = array_filter($console['messages'], function ($message) { |
|
| 305 | - return $message['type'] == 'speed'; |
|
| 306 | - }); |
|
| 307 | - |
|
| 308 | - $memory['messages'] = array_filter($console['messages'], function ($message) { |
|
| 309 | - return $message['type'] == 'memory'; |
|
| 310 | - }); |
|
| 311 | - |
|
| 312 | - // todo is this really the best way to load these? |
|
| 313 | - $styles = file_get_contents(__DIR__ . "./../{$this->options['style_path']}"); |
|
| 314 | - $script = file_get_contents(__DIR__ . "./../{$this->options['script_path']}"); |
|
| 315 | - |
|
| 316 | - require_once __DIR__ .'/../asset/display.html'; |
|
| 317 | - } |
|
| 288 | + public function __invoke() |
|
| 289 | + { |
|
| 290 | + $console= $this->formatConsoleData(); |
|
| 291 | + $speed= $this->formatSpeedData(); |
|
| 292 | + $query= $this->formatQueryData(); |
|
| 293 | + $memory= $this->formatMemoryData(); |
|
| 294 | + $files= $this->formatFileData(); |
|
| 295 | + |
|
| 296 | + $header = array( |
|
| 297 | + 'console' => count($console['messages']), |
|
| 298 | + 'speed' => $speed['meta']['elapsed'], |
|
| 299 | + 'query' => $query['meta']['count'], |
|
| 300 | + 'memory' => $memory['meta']['used'], |
|
| 301 | + 'files' => $files['meta']['count'] |
|
| 302 | + ); |
|
| 303 | + |
|
| 304 | + $speed['messages'] = array_filter($console['messages'], function ($message) { |
|
| 305 | + return $message['type'] == 'speed'; |
|
| 306 | + }); |
|
| 307 | + |
|
| 308 | + $memory['messages'] = array_filter($console['messages'], function ($message) { |
|
| 309 | + return $message['type'] == 'memory'; |
|
| 310 | + }); |
|
| 311 | + |
|
| 312 | + // todo is this really the best way to load these? |
|
| 313 | + $styles = file_get_contents(__DIR__ . "./../{$this->options['style_path']}"); |
|
| 314 | + $script = file_get_contents(__DIR__ . "./../{$this->options['script_path']}"); |
|
| 315 | + |
|
| 316 | + require_once __DIR__ .'/../asset/display.html'; |
|
| 317 | + } |
|
| 318 | 318 | } |
@@ -70,7 +70,7 @@ discard block |
||
| 70 | 70 | ) |
| 71 | 71 | ); |
| 72 | 72 | foreach ($this->console->getLogs() as $log) { |
| 73 | - switch($log['type']) { |
|
| 73 | + switch ($log['type']) { |
|
| 74 | 74 | case 'log': |
| 75 | 75 | $message = array( |
| 76 | 76 | 'message' => print_r($log['data'], true), |
@@ -80,7 +80,7 @@ discard block |
||
| 80 | 80 | break; |
| 81 | 81 | case 'memory': |
| 82 | 82 | $message = array( |
| 83 | - 'message' => (!empty($log['data_type']) ? "{$log['data_type']}: " : '') . $log['name'], |
|
| 83 | + 'message' => (!empty($log['data_type']) ? "{$log['data_type']}: " : '').$log['name'], |
|
| 84 | 84 | 'data' => $this->getReadableMemory($log['data']), |
| 85 | 85 | 'type' => 'memory' |
| 86 | 86 | ); |
@@ -287,11 +287,11 @@ discard block |
||
| 287 | 287 | |
| 288 | 288 | public function __invoke() |
| 289 | 289 | { |
| 290 | - $console= $this->formatConsoleData(); |
|
| 291 | - $speed= $this->formatSpeedData(); |
|
| 292 | - $query= $this->formatQueryData(); |
|
| 293 | - $memory= $this->formatMemoryData(); |
|
| 294 | - $files= $this->formatFileData(); |
|
| 290 | + $console = $this->formatConsoleData(); |
|
| 291 | + $speed = $this->formatSpeedData(); |
|
| 292 | + $query = $this->formatQueryData(); |
|
| 293 | + $memory = $this->formatMemoryData(); |
|
| 294 | + $files = $this->formatFileData(); |
|
| 295 | 295 | |
| 296 | 296 | $header = array( |
| 297 | 297 | 'console' => count($console['messages']), |
@@ -301,18 +301,18 @@ discard block |
||
| 301 | 301 | 'files' => $files['meta']['count'] |
| 302 | 302 | ); |
| 303 | 303 | |
| 304 | - $speed['messages'] = array_filter($console['messages'], function ($message) { |
|
| 304 | + $speed['messages'] = array_filter($console['messages'], function($message) { |
|
| 305 | 305 | return $message['type'] == 'speed'; |
| 306 | 306 | }); |
| 307 | 307 | |
| 308 | - $memory['messages'] = array_filter($console['messages'], function ($message) { |
|
| 308 | + $memory['messages'] = array_filter($console['messages'], function($message) { |
|
| 309 | 309 | return $message['type'] == 'memory'; |
| 310 | 310 | }); |
| 311 | 311 | |
| 312 | 312 | // todo is this really the best way to load these? |
| 313 | - $styles = file_get_contents(__DIR__ . "./../{$this->options['style_path']}"); |
|
| 314 | - $script = file_get_contents(__DIR__ . "./../{$this->options['script_path']}"); |
|
| 313 | + $styles = file_get_contents(__DIR__."./../{$this->options['style_path']}"); |
|
| 314 | + $script = file_get_contents(__DIR__."./../{$this->options['script_path']}"); |
|
| 315 | 315 | |
| 316 | - require_once __DIR__ .'/../asset/display.html'; |
|
| 316 | + require_once __DIR__.'/../asset/display.html'; |
|
| 317 | 317 | } |
| 318 | 318 | } |