@@ -5,29 +5,29 @@ |
||
| 5 | 5 | class DisplayTest extends PHPUnit_Framework_TestCase |
| 6 | 6 | { |
| 7 | 7 | |
| 8 | - public function testGetReadableTime() |
|
| 9 | - { |
|
| 10 | - $test_input = array( |
|
| 11 | - '.032432' => '32.432 ms', |
|
| 12 | - '24.3781' => '24.378 s', |
|
| 13 | - '145.123' => '2.419 m' |
|
| 14 | - ); |
|
| 8 | + public function testGetReadableTime() |
|
| 9 | + { |
|
| 10 | + $test_input = array( |
|
| 11 | + '.032432' => '32.432 ms', |
|
| 12 | + '24.3781' => '24.378 s', |
|
| 13 | + '145.123' => '2.419 m' |
|
| 14 | + ); |
|
| 15 | 15 | |
| 16 | - foreach ($test_input as $input => $expected_return) { |
|
| 17 | - $this->assertEquals($expected_return, Display::getReadableTime($input)); |
|
| 18 | - } |
|
| 19 | - } |
|
| 16 | + foreach ($test_input as $input => $expected_return) { |
|
| 17 | + $this->assertEquals($expected_return, Display::getReadableTime($input)); |
|
| 18 | + } |
|
| 19 | + } |
|
| 20 | 20 | |
| 21 | - public function testGetReadableMemory() |
|
| 22 | - { |
|
| 23 | - $test_input = array( |
|
| 24 | - '314' => '314 b', |
|
| 25 | - '7403' => '7.23 k', |
|
| 26 | - '2589983' => '2.47 M' |
|
| 27 | - ); |
|
| 21 | + public function testGetReadableMemory() |
|
| 22 | + { |
|
| 23 | + $test_input = array( |
|
| 24 | + '314' => '314 b', |
|
| 25 | + '7403' => '7.23 k', |
|
| 26 | + '2589983' => '2.47 M' |
|
| 27 | + ); |
|
| 28 | 28 | |
| 29 | - foreach ($test_input as $input => $expected_return) { |
|
| 30 | - $this->assertEquals($expected_return, Display::getReadableMemory($input)); |
|
| 31 | - } |
|
| 32 | - } |
|
| 29 | + foreach ($test_input as $input => $expected_return) { |
|
| 30 | + $this->assertEquals($expected_return, Display::getReadableMemory($input)); |
|
| 31 | + } |
|
| 32 | + } |
|
| 33 | 33 | } |
@@ -14,243 +14,243 @@ |
||
| 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' => self::getReadableMemory($log['data']), |
|
| 62 | - 'type' => 'memory' |
|
| 63 | - ); |
|
| 64 | - $console_data['meta']['memory']++; |
|
| 65 | - break; |
|
| 66 | - case 'error': |
|
| 67 | - $message = array( |
|
| 68 | - 'message' => "Line {$log['line']}: {$log['data']} in {$log['file']}", |
|
| 69 | - 'type' => 'error' |
|
| 70 | - ); |
|
| 71 | - $console_data['meta']['error']++; |
|
| 72 | - break; |
|
| 73 | - case 'speed': |
|
| 74 | - $elapsedTime = $log['data'] - $this->startTime; |
|
| 75 | - $message = array( |
|
| 76 | - 'message' => $log['name'], |
|
| 77 | - 'data' => self::getReadableTime($elapsedTime), |
|
| 78 | - 'type' => 'speed' |
|
| 79 | - ); |
|
| 80 | - $console_data['meta']['speed']++; |
|
| 81 | - break; |
|
| 82 | - 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' => self::getReadableMemory($log['data']), |
|
| 62 | + 'type' => 'memory' |
|
| 63 | + ); |
|
| 64 | + $console_data['meta']['memory']++; |
|
| 65 | + break; |
|
| 66 | + case 'error': |
|
| 67 | + $message = array( |
|
| 68 | + 'message' => "Line {$log['line']}: {$log['data']} in {$log['file']}", |
|
| 69 | + 'type' => 'error' |
|
| 70 | + ); |
|
| 71 | + $console_data['meta']['error']++; |
|
| 72 | + break; |
|
| 73 | + case 'speed': |
|
| 74 | + $elapsedTime = $log['data'] - $this->startTime; |
|
| 75 | + $message = array( |
|
| 76 | + 'message' => $log['name'], |
|
| 77 | + 'data' => self::getReadableTime($elapsedTime), |
|
| 78 | + 'type' => 'speed' |
|
| 79 | + ); |
|
| 80 | + $console_data['meta']['speed']++; |
|
| 81 | + break; |
|
| 82 | + 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' => self::getReadableMemory($file['size']) |
|
| 115 | - )); |
|
| 111 | + foreach ($data as $file) { |
|
| 112 | + array_push($fileData['messages'], array( |
|
| 113 | + 'message' => $file['name'], |
|
| 114 | + 'data' => self::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'] = self::getReadableMemory($fileData['meta']['size']); |
|
| 124 | - $fileData['meta']['largest'] = self::getReadableMemory($fileData['meta']['largest']); |
|
| 123 | + $fileData['meta']['size'] = self::getReadableMemory($fileData['meta']['size']); |
|
| 124 | + $fileData['meta']['largest'] = self::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' => self::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' => self::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' => self::getReadableTime($query['time']) |
|
| 158 | - )); |
|
| 159 | - $queryData['meta']['time'] += $query['time']; |
|
| 160 | - if ($query['time'] > $queryData['meta']['slowest']) { |
|
| 161 | - $queryData['meta']['slowest'] = $query['time']; |
|
| 162 | - } |
|
| 163 | - } |
|
| 153 | + foreach ($data as $query) { |
|
| 154 | + array_push($queryData['messages'], array( |
|
| 155 | + 'message' => $query['sql'], |
|
| 156 | + 'sub_data' => array_filter($query['explain']), |
|
| 157 | + 'data' => self::getReadableTime($query['time']) |
|
| 158 | + )); |
|
| 159 | + $queryData['meta']['time'] += $query['time']; |
|
| 160 | + if ($query['time'] > $queryData['meta']['slowest']) { |
|
| 161 | + $queryData['meta']['slowest'] = $query['time']; |
|
| 162 | + } |
|
| 163 | + } |
|
| 164 | 164 | |
| 165 | - $queryData['meta']['time'] = self::getReadableTime($queryData['meta']['time']); |
|
| 166 | - $queryData['meta']['slowest'] = self::getReadableTime($queryData['meta']['slowest']); |
|
| 165 | + $queryData['meta']['time'] = self::getReadableTime($queryData['meta']['time']); |
|
| 166 | + $queryData['meta']['slowest'] = self::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' => self::getReadableTime($data['elapsed']), |
|
| 180 | - 'allowed' => self::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' => self::getReadableTime($data['elapsed']), |
|
| 180 | + 'allowed' => self::getReadableTime($data['allowed'], 0) |
|
| 181 | + ); |
|
| 182 | + } |
|
| 183 | 183 | |
| 184 | - /** |
|
| 185 | - * Static formatter for human-readable time |
|
| 186 | - * Only handles time up to 60 minutes gracefully |
|
| 187 | - * |
|
| 188 | - * @param double $time |
|
| 189 | - * @param integer $decimals |
|
| 190 | - * @return string |
|
| 191 | - */ |
|
| 192 | - public static function getReadableTime($time, $decimals = 3) |
|
| 193 | - { |
|
| 194 | - $unit = 's'; |
|
| 195 | - if ($time < 1) { |
|
| 196 | - $time *= 1000; |
|
| 197 | - $unit = 'ms'; |
|
| 198 | - } else if ($time > 60) { |
|
| 199 | - $time /= 60; |
|
| 200 | - $unit = 'm'; |
|
| 201 | - } |
|
| 202 | - $time = number_format($time, $decimals); |
|
| 203 | - return "{$time} {$unit}"; |
|
| 204 | - } |
|
| 184 | + /** |
|
| 185 | + * Static formatter for human-readable time |
|
| 186 | + * Only handles time up to 60 minutes gracefully |
|
| 187 | + * |
|
| 188 | + * @param double $time |
|
| 189 | + * @param integer $decimals |
|
| 190 | + * @return string |
|
| 191 | + */ |
|
| 192 | + public static function getReadableTime($time, $decimals = 3) |
|
| 193 | + { |
|
| 194 | + $unit = 's'; |
|
| 195 | + if ($time < 1) { |
|
| 196 | + $time *= 1000; |
|
| 197 | + $unit = 'ms'; |
|
| 198 | + } else if ($time > 60) { |
|
| 199 | + $time /= 60; |
|
| 200 | + $unit = 'm'; |
|
| 201 | + } |
|
| 202 | + $time = number_format($time, $decimals); |
|
| 203 | + return "{$time} {$unit}"; |
|
| 204 | + } |
|
| 205 | 205 | |
| 206 | - /** |
|
| 207 | - * Static formatter for human-readable memory |
|
| 208 | - * |
|
| 209 | - * @param double $size |
|
| 210 | - * @param integer $decimals |
|
| 211 | - */ |
|
| 212 | - public static function getReadableMemory($size, $decimals = 2) |
|
| 213 | - { |
|
| 214 | - $unitOptions = array('b', 'k', 'M', 'G'); |
|
| 206 | + /** |
|
| 207 | + * Static formatter for human-readable memory |
|
| 208 | + * |
|
| 209 | + * @param double $size |
|
| 210 | + * @param integer $decimals |
|
| 211 | + */ |
|
| 212 | + public static function getReadableMemory($size, $decimals = 2) |
|
| 213 | + { |
|
| 214 | + $unitOptions = array('b', 'k', 'M', 'G'); |
|
| 215 | 215 | |
| 216 | - $base = log($size, 1024); |
|
| 216 | + $base = log($size, 1024); |
|
| 217 | 217 | |
| 218 | - $memory = round(pow(1024, $base - floor($base)), $decimals); |
|
| 219 | - $unit = $unitOptions[floor($base)]; |
|
| 220 | - return "{$memory} {$unit}"; |
|
| 221 | - } |
|
| 218 | + $memory = round(pow(1024, $base - floor($base)), $decimals); |
|
| 219 | + $unit = $unitOptions[floor($base)]; |
|
| 220 | + return "{$memory} {$unit}"; |
|
| 221 | + } |
|
| 222 | 222 | |
| 223 | - public function __invoke() |
|
| 224 | - { |
|
| 225 | - $output = $this->output; |
|
| 226 | - $header = array( |
|
| 227 | - 'console' => count($output['console']['messages']), |
|
| 228 | - 'speed' => $output['speed']['meta']['elapsed'], |
|
| 229 | - 'query' => $output['query']['meta']['count'], |
|
| 230 | - 'memory' => $output['memory']['meta']['used'], |
|
| 231 | - 'files' => $output['files']['meta']['count'] |
|
| 232 | - ); |
|
| 223 | + public function __invoke() |
|
| 224 | + { |
|
| 225 | + $output = $this->output; |
|
| 226 | + $header = array( |
|
| 227 | + 'console' => count($output['console']['messages']), |
|
| 228 | + 'speed' => $output['speed']['meta']['elapsed'], |
|
| 229 | + 'query' => $output['query']['meta']['count'], |
|
| 230 | + 'memory' => $output['memory']['meta']['used'], |
|
| 231 | + 'files' => $output['files']['meta']['count'] |
|
| 232 | + ); |
|
| 233 | 233 | |
| 234 | - $console = $output['console']; |
|
| 234 | + $console = $output['console']; |
|
| 235 | 235 | |
| 236 | - $speed = $output['speed']; |
|
| 237 | - $speed['messages'] = array_filter($console['messages'], function ($message) { |
|
| 238 | - return $message['type'] == 'speed'; |
|
| 239 | - }); |
|
| 236 | + $speed = $output['speed']; |
|
| 237 | + $speed['messages'] = array_filter($console['messages'], function ($message) { |
|
| 238 | + return $message['type'] == 'speed'; |
|
| 239 | + }); |
|
| 240 | 240 | |
| 241 | - $query = $output['query']; |
|
| 241 | + $query = $output['query']; |
|
| 242 | 242 | |
| 243 | - $memory = $output['memory']; |
|
| 244 | - $memory['messages'] = array_filter($console['messages'], function ($message) { |
|
| 245 | - return $message['type'] == 'memory'; |
|
| 246 | - }); |
|
| 243 | + $memory = $output['memory']; |
|
| 244 | + $memory['messages'] = array_filter($console['messages'], function ($message) { |
|
| 245 | + return $message['type'] == 'memory'; |
|
| 246 | + }); |
|
| 247 | 247 | |
| 248 | - $files = $output['files']; |
|
| 248 | + $files = $output['files']; |
|
| 249 | 249 | |
| 250 | - // todo is this really the best way to load these? |
|
| 251 | - $styles = file_get_contents(__DIR__ . "./../{$this->options['style_path']}"); |
|
| 252 | - $script = file_get_contents(__DIR__ . "./../{$this->options['script_path']}"); |
|
| 250 | + // todo is this really the best way to load these? |
|
| 251 | + $styles = file_get_contents(__DIR__ . "./../{$this->options['style_path']}"); |
|
| 252 | + $script = file_get_contents(__DIR__ . "./../{$this->options['script_path']}"); |
|
| 253 | 253 | |
| 254 | - require_once __DIR__ .'/../asset/display.html'; |
|
| 255 | - } |
|
| 254 | + require_once __DIR__ .'/../asset/display.html'; |
|
| 255 | + } |
|
| 256 | 256 | } |
@@ -15,88 +15,88 @@ |
||
| 15 | 15 | class Console |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** @var array */ |
|
| 19 | - protected $store = array(); |
|
| 18 | + /** @var array */ |
|
| 19 | + protected $store = array(); |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Logs data to the console |
|
| 23 | - * Accepts any data type |
|
| 24 | - * |
|
| 25 | - * @param mixed $data |
|
| 26 | - */ |
|
| 27 | - public function log($data) |
|
| 28 | - { |
|
| 29 | - array_push($this->store, array( |
|
| 30 | - 'data' => $data, |
|
| 31 | - 'type' => 'log' |
|
| 32 | - )); |
|
| 33 | - } |
|
| 21 | + /** |
|
| 22 | + * Logs data to the console |
|
| 23 | + * Accepts any data type |
|
| 24 | + * |
|
| 25 | + * @param mixed $data |
|
| 26 | + */ |
|
| 27 | + public function log($data) |
|
| 28 | + { |
|
| 29 | + array_push($this->store, array( |
|
| 30 | + 'data' => $data, |
|
| 31 | + 'type' => 'log' |
|
| 32 | + )); |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Logs memory usage of a variable |
|
| 37 | - * If no parameter is passed in, logs current memory usage |
|
| 38 | - * |
|
| 39 | - * @param mixed $object |
|
| 40 | - * @param string $name |
|
| 41 | - */ |
|
| 42 | - public function logMemory($object = null, $name = 'PHP') |
|
| 43 | - { |
|
| 44 | - $memory = memory_get_usage(); |
|
| 45 | - $dataType = ''; |
|
| 46 | - if (!is_null($object)) { |
|
| 47 | - $memory = strlen(serialize($object)); |
|
| 48 | - $dataType = gettype($object); |
|
| 49 | - } |
|
| 35 | + /** |
|
| 36 | + * Logs memory usage of a variable |
|
| 37 | + * If no parameter is passed in, logs current memory usage |
|
| 38 | + * |
|
| 39 | + * @param mixed $object |
|
| 40 | + * @param string $name |
|
| 41 | + */ |
|
| 42 | + public function logMemory($object = null, $name = 'PHP') |
|
| 43 | + { |
|
| 44 | + $memory = memory_get_usage(); |
|
| 45 | + $dataType = ''; |
|
| 46 | + if (!is_null($object)) { |
|
| 47 | + $memory = strlen(serialize($object)); |
|
| 48 | + $dataType = gettype($object); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - array_push($this->store, array( |
|
| 52 | - 'name' => $name, |
|
| 53 | - 'data' => $memory, |
|
| 54 | - 'data_type' => $dataType, |
|
| 55 | - 'type' => 'memory' |
|
| 56 | - )); |
|
| 57 | - } |
|
| 51 | + array_push($this->store, array( |
|
| 52 | + 'name' => $name, |
|
| 53 | + 'data' => $memory, |
|
| 54 | + 'data_type' => $dataType, |
|
| 55 | + 'type' => 'memory' |
|
| 56 | + )); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Logs exception with optional message override |
|
| 61 | - * |
|
| 62 | - * @param Exception $exception |
|
| 63 | - * @param string $message |
|
| 64 | - */ |
|
| 65 | - public function logError(Exception $exception, $message = '') |
|
| 66 | - { |
|
| 67 | - if (empty($message)) { |
|
| 68 | - $message = $exception->getMessage(); |
|
| 69 | - } |
|
| 59 | + /** |
|
| 60 | + * Logs exception with optional message override |
|
| 61 | + * |
|
| 62 | + * @param Exception $exception |
|
| 63 | + * @param string $message |
|
| 64 | + */ |
|
| 65 | + public function logError(Exception $exception, $message = '') |
|
| 66 | + { |
|
| 67 | + if (empty($message)) { |
|
| 68 | + $message = $exception->getMessage(); |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - array_push($this->store, array( |
|
| 72 | - 'data' => $message, |
|
| 73 | - 'file' => $exception->getFile(), |
|
| 74 | - 'line' => $exception->getLine(), |
|
| 75 | - 'type' => 'error' |
|
| 76 | - )); |
|
| 77 | - } |
|
| 71 | + array_push($this->store, array( |
|
| 72 | + 'data' => $message, |
|
| 73 | + 'file' => $exception->getFile(), |
|
| 74 | + 'line' => $exception->getLine(), |
|
| 75 | + 'type' => 'error' |
|
| 76 | + )); |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - /** |
|
| 80 | - * Logs current time with optional message |
|
| 81 | - * |
|
| 82 | - * @param string $name |
|
| 83 | - */ |
|
| 84 | - public function logSpeed($name = 'Point in Time') |
|
| 85 | - { |
|
| 86 | - array_push($this->store, array( |
|
| 87 | - 'data' => microtime(true), |
|
| 88 | - 'name' => $name, |
|
| 89 | - 'type' => 'speed' |
|
| 90 | - )); |
|
| 91 | - } |
|
| 79 | + /** |
|
| 80 | + * Logs current time with optional message |
|
| 81 | + * |
|
| 82 | + * @param string $name |
|
| 83 | + */ |
|
| 84 | + public function logSpeed($name = 'Point in Time') |
|
| 85 | + { |
|
| 86 | + array_push($this->store, array( |
|
| 87 | + 'data' => microtime(true), |
|
| 88 | + 'name' => $name, |
|
| 89 | + 'type' => 'speed' |
|
| 90 | + )); |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Returns the collected logs |
|
| 95 | - * |
|
| 96 | - * @returns array |
|
| 97 | - */ |
|
| 98 | - public function getLogs() |
|
| 99 | - { |
|
| 100 | - return $this->store; |
|
| 101 | - } |
|
| 93 | + /** |
|
| 94 | + * Returns the collected logs |
|
| 95 | + * |
|
| 96 | + * @returns array |
|
| 97 | + */ |
|
| 98 | + public function getLogs() |
|
| 99 | + { |
|
| 100 | + return $this->store; |
|
| 101 | + } |
|
| 102 | 102 | } |
@@ -88,7 +88,7 @@ |
||
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | /** |
| 91 | - * @param array $profiled_queries |
|
| 91 | + * @param array $profiledQueries |
|
| 92 | 92 | */ |
| 93 | 93 | public function setProfiledQueries(array $profiledQueries) |
| 94 | 94 | { |
@@ -17,166 +17,166 @@ |
||
| 17 | 17 | class PhpQuickProfiler |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** @var integer */ |
|
| 21 | - protected $startTime; |
|
| 22 | - |
|
| 23 | - /** @var Console */ |
|
| 24 | - protected $console; |
|
| 25 | - |
|
| 26 | - /** @var Display */ |
|
| 27 | - protected $display; |
|
| 28 | - |
|
| 29 | - /** @var array */ |
|
| 30 | - protected $profiledQueries = array(); |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @param integer $startTime |
|
| 34 | - */ |
|
| 35 | - public function __construct($startTime = null) |
|
| 36 | - { |
|
| 37 | - if (is_null($startTime)) { |
|
| 38 | - $startTime = microtime(true); |
|
| 39 | - } |
|
| 40 | - $this->startTime = $startTime; |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @param Console $console |
|
| 45 | - */ |
|
| 46 | - public function setConsole(Console $console) |
|
| 47 | - { |
|
| 48 | - $this->console = $console; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @param Display $display |
|
| 53 | - */ |
|
| 54 | - public function setDisplay(Display $display) |
|
| 55 | - { |
|
| 56 | - $this->display = $display; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Get data about files loaded for the application to current point |
|
| 61 | - * |
|
| 62 | - * @returns array |
|
| 63 | - */ |
|
| 64 | - public function gatherFileData() |
|
| 65 | - { |
|
| 66 | - $files = get_included_files(); |
|
| 67 | - $data = array(); |
|
| 68 | - foreach ($files as $file) { |
|
| 69 | - array_push($data, array( |
|
| 70 | - 'name' => $file, |
|
| 71 | - 'size' => filesize($file) |
|
| 72 | - )); |
|
| 73 | - } |
|
| 74 | - return $data; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Get data about memory usage of the application |
|
| 79 | - * |
|
| 80 | - * @returns array |
|
| 81 | - */ |
|
| 82 | - public function gatherMemoryData() |
|
| 83 | - { |
|
| 84 | - $usedMemory = memory_get_peak_usage(); |
|
| 85 | - $allowedMemory = ini_get('memory_limit'); |
|
| 86 | - return array( |
|
| 87 | - 'used' => $usedMemory, |
|
| 88 | - 'allowed' => $allowedMemory |
|
| 89 | - ); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * @param array $profiled_queries |
|
| 94 | - */ |
|
| 95 | - public function setProfiledQueries(array $profiledQueries) |
|
| 96 | - { |
|
| 97 | - $this->profiledQueries = $profiledQueries; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Get data about sql usage of the application |
|
| 102 | - * |
|
| 103 | - * @param object $dbConnection |
|
| 104 | - * @returns array |
|
| 105 | - */ |
|
| 106 | - public function gatherQueryData($dbConnection) |
|
| 107 | - { |
|
| 108 | - if (empty($this->profiledQueries) && property_exists($dbConnection, 'queries')) { |
|
| 109 | - $this->setProfiledQueries($dbConnection->queries); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - $data = array(); |
|
| 113 | - foreach ($this->profiledQueries as $query) { |
|
| 114 | - if ($query['function'] !== 'perform') { |
|
| 115 | - continue; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - array_push($data, array( |
|
| 119 | - 'sql' => $query['statement'], |
|
| 120 | - 'explain' => $this->explainQuery($dbConnection, $query['statement'], $query['bind_values']), |
|
| 121 | - 'time' => $query['duration'] |
|
| 122 | - )); |
|
| 123 | - } |
|
| 124 | - return $data; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Attempts to explain a query |
|
| 129 | - * |
|
| 130 | - * @param object $dbConnection |
|
| 131 | - * @param string $query |
|
| 132 | - * @param array $parameters |
|
| 133 | - * @return array |
|
| 134 | - */ |
|
| 135 | - protected function explainQuery($dbConnection, $query, $parameters) |
|
| 136 | - { |
|
| 137 | - $query = "EXPLAIN {$query}"; |
|
| 138 | - try { |
|
| 139 | - $statement = $dbConnection->prepare($query); |
|
| 140 | - $statement->execute($parameters); |
|
| 141 | - return $statement->fetch(\PDO::FETCH_ASSOC); |
|
| 142 | - } catch (Exception $e) { |
|
| 143 | - echo $e->getMessage(); |
|
| 144 | - } |
|
| 145 | - return ''; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * Get data about speed of the application |
|
| 150 | - * |
|
| 151 | - * @returns array |
|
| 152 | - */ |
|
| 153 | - public function gatherSpeedData() |
|
| 154 | - { |
|
| 155 | - $elapsedTime = microtime(true) - $this->startTime; |
|
| 156 | - $allowedTime = ini_get('max_execution_time'); |
|
| 157 | - return array( |
|
| 158 | - 'elapsed' => $elapsedTime, |
|
| 159 | - 'allowed' => $allowedTime |
|
| 160 | - ); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Triggers end display of the profiling data |
|
| 165 | - * |
|
| 166 | - * @param object $dbConnection |
|
| 167 | - */ |
|
| 168 | - public function display($dbConnection = null) |
|
| 169 | - { |
|
| 170 | - if (!isset($this->display)) { |
|
| 171 | - throw new Exception('Display object has not been injected into Profiler'); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - $this->display->setConsole($this->console); |
|
| 175 | - $this->display->setFileData($this->gatherFileData()); |
|
| 176 | - $this->display->setMemoryData($this->gatherMemoryData()); |
|
| 177 | - $this->display->setQueryData($this->gatherQueryData($dbConnection)); |
|
| 178 | - $this->display->setSpeedData($this->gatherSpeedData()); |
|
| 179 | - |
|
| 180 | - $this->display->__invoke(); |
|
| 181 | - } |
|
| 20 | + /** @var integer */ |
|
| 21 | + protected $startTime; |
|
| 22 | + |
|
| 23 | + /** @var Console */ |
|
| 24 | + protected $console; |
|
| 25 | + |
|
| 26 | + /** @var Display */ |
|
| 27 | + protected $display; |
|
| 28 | + |
|
| 29 | + /** @var array */ |
|
| 30 | + protected $profiledQueries = array(); |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @param integer $startTime |
|
| 34 | + */ |
|
| 35 | + public function __construct($startTime = null) |
|
| 36 | + { |
|
| 37 | + if (is_null($startTime)) { |
|
| 38 | + $startTime = microtime(true); |
|
| 39 | + } |
|
| 40 | + $this->startTime = $startTime; |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @param Console $console |
|
| 45 | + */ |
|
| 46 | + public function setConsole(Console $console) |
|
| 47 | + { |
|
| 48 | + $this->console = $console; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @param Display $display |
|
| 53 | + */ |
|
| 54 | + public function setDisplay(Display $display) |
|
| 55 | + { |
|
| 56 | + $this->display = $display; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Get data about files loaded for the application to current point |
|
| 61 | + * |
|
| 62 | + * @returns array |
|
| 63 | + */ |
|
| 64 | + public function gatherFileData() |
|
| 65 | + { |
|
| 66 | + $files = get_included_files(); |
|
| 67 | + $data = array(); |
|
| 68 | + foreach ($files as $file) { |
|
| 69 | + array_push($data, array( |
|
| 70 | + 'name' => $file, |
|
| 71 | + 'size' => filesize($file) |
|
| 72 | + )); |
|
| 73 | + } |
|
| 74 | + return $data; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Get data about memory usage of the application |
|
| 79 | + * |
|
| 80 | + * @returns array |
|
| 81 | + */ |
|
| 82 | + public function gatherMemoryData() |
|
| 83 | + { |
|
| 84 | + $usedMemory = memory_get_peak_usage(); |
|
| 85 | + $allowedMemory = ini_get('memory_limit'); |
|
| 86 | + return array( |
|
| 87 | + 'used' => $usedMemory, |
|
| 88 | + 'allowed' => $allowedMemory |
|
| 89 | + ); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * @param array $profiled_queries |
|
| 94 | + */ |
|
| 95 | + public function setProfiledQueries(array $profiledQueries) |
|
| 96 | + { |
|
| 97 | + $this->profiledQueries = $profiledQueries; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Get data about sql usage of the application |
|
| 102 | + * |
|
| 103 | + * @param object $dbConnection |
|
| 104 | + * @returns array |
|
| 105 | + */ |
|
| 106 | + public function gatherQueryData($dbConnection) |
|
| 107 | + { |
|
| 108 | + if (empty($this->profiledQueries) && property_exists($dbConnection, 'queries')) { |
|
| 109 | + $this->setProfiledQueries($dbConnection->queries); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + $data = array(); |
|
| 113 | + foreach ($this->profiledQueries as $query) { |
|
| 114 | + if ($query['function'] !== 'perform') { |
|
| 115 | + continue; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + array_push($data, array( |
|
| 119 | + 'sql' => $query['statement'], |
|
| 120 | + 'explain' => $this->explainQuery($dbConnection, $query['statement'], $query['bind_values']), |
|
| 121 | + 'time' => $query['duration'] |
|
| 122 | + )); |
|
| 123 | + } |
|
| 124 | + return $data; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Attempts to explain a query |
|
| 129 | + * |
|
| 130 | + * @param object $dbConnection |
|
| 131 | + * @param string $query |
|
| 132 | + * @param array $parameters |
|
| 133 | + * @return array |
|
| 134 | + */ |
|
| 135 | + protected function explainQuery($dbConnection, $query, $parameters) |
|
| 136 | + { |
|
| 137 | + $query = "EXPLAIN {$query}"; |
|
| 138 | + try { |
|
| 139 | + $statement = $dbConnection->prepare($query); |
|
| 140 | + $statement->execute($parameters); |
|
| 141 | + return $statement->fetch(\PDO::FETCH_ASSOC); |
|
| 142 | + } catch (Exception $e) { |
|
| 143 | + echo $e->getMessage(); |
|
| 144 | + } |
|
| 145 | + return ''; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * Get data about speed of the application |
|
| 150 | + * |
|
| 151 | + * @returns array |
|
| 152 | + */ |
|
| 153 | + public function gatherSpeedData() |
|
| 154 | + { |
|
| 155 | + $elapsedTime = microtime(true) - $this->startTime; |
|
| 156 | + $allowedTime = ini_get('max_execution_time'); |
|
| 157 | + return array( |
|
| 158 | + 'elapsed' => $elapsedTime, |
|
| 159 | + 'allowed' => $allowedTime |
|
| 160 | + ); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Triggers end display of the profiling data |
|
| 165 | + * |
|
| 166 | + * @param object $dbConnection |
|
| 167 | + */ |
|
| 168 | + public function display($dbConnection = null) |
|
| 169 | + { |
|
| 170 | + if (!isset($this->display)) { |
|
| 171 | + throw new Exception('Display object has not been injected into Profiler'); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + $this->display->setConsole($this->console); |
|
| 175 | + $this->display->setFileData($this->gatherFileData()); |
|
| 176 | + $this->display->setMemoryData($this->gatherMemoryData()); |
|
| 177 | + $this->display->setQueryData($this->gatherQueryData($dbConnection)); |
|
| 178 | + $this->display->setSpeedData($this->gatherSpeedData()); |
|
| 179 | + |
|
| 180 | + $this->display->__invoke(); |
|
| 181 | + } |
|
| 182 | 182 | } |
@@ -9,108 +9,108 @@ |
||
| 9 | 9 | class ConsoleTest extends PHPUnit_Framework_TestCase |
| 10 | 10 | { |
| 11 | 11 | |
| 12 | - public function testLog() |
|
| 13 | - { |
|
| 14 | - $data = array( |
|
| 15 | - 'key' => 'value' |
|
| 16 | - ); |
|
| 17 | - |
|
| 18 | - $console = new Console(); |
|
| 19 | - $console->log($data); |
|
| 20 | - $store = $this->getProtectedStore($console); |
|
| 21 | - $log = array_pop($store); |
|
| 22 | - |
|
| 23 | - $this->assertSame($data, $log['data']); |
|
| 24 | - $this->assertEquals('log', $log['type']); |
|
| 25 | - } |
|
| 26 | - |
|
| 27 | - public function testLogMemory() |
|
| 28 | - { |
|
| 29 | - $data = array( |
|
| 30 | - 'key' => 'value' |
|
| 31 | - ); |
|
| 32 | - $memory = strlen(serialize($data)); |
|
| 33 | - $name = 'Test Array'; |
|
| 34 | - |
|
| 35 | - $console = new Console(); |
|
| 36 | - $console->logMemory($data, $name); |
|
| 37 | - $store = $this->getProtectedStore($console); |
|
| 38 | - $log = array_pop($store); |
|
| 39 | - |
|
| 40 | - $this->assertEquals($name, $log['name']); |
|
| 41 | - $this->assertEquals($memory, $log['data']); |
|
| 42 | - $this->assertEquals('array', $log['data_type']); |
|
| 43 | - $this->assertEquals('memory', $log['type']); |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - public function testLogError() |
|
| 47 | - { |
|
| 48 | - $error = new Exception('Test Exception'); |
|
| 49 | - |
|
| 50 | - $console = new Console(); |
|
| 51 | - $console->logError($error); |
|
| 52 | - $store = $this->getProtectedStore($console); |
|
| 53 | - $log = array_pop($store); |
|
| 54 | - |
|
| 55 | - $this->assertEquals($error->getMessage(), $log['data']); |
|
| 56 | - $this->assertEquals($error->getFile(), $log['file']); |
|
| 57 | - $this->assertEquals($error->getLine(), $log['line']); |
|
| 58 | - $this->assertEquals('error', $log['type']); |
|
| 59 | - |
|
| 60 | - $error = new Exception('Test Exception'); |
|
| 61 | - $message = 'override message'; |
|
| 62 | - |
|
| 63 | - $console = new Console(); |
|
| 64 | - $console->logError($error, $message); |
|
| 65 | - $store = $this->getProtectedStore($console); |
|
| 66 | - $log = array_pop($store); |
|
| 67 | - |
|
| 68 | - $this->assertEquals($message, $log['data']); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - public function testLogSpeed() |
|
| 72 | - { |
|
| 73 | - $name = 'Test Speed'; |
|
| 74 | - |
|
| 75 | - $console = new Console(); |
|
| 76 | - $console->logSpeed($name); |
|
| 77 | - $store = $this->getProtectedStore($console); |
|
| 78 | - $log = array_pop($store); |
|
| 79 | - |
|
| 80 | - $this->assertEquals($name, $log['name']); |
|
| 81 | - $this->assertEquals('speed', $log['type']); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - public function testGetLogs() |
|
| 85 | - { |
|
| 86 | - $store = array( |
|
| 87 | - array( |
|
| 88 | - 'data' => 'a string', |
|
| 89 | - 'type' => 'log' |
|
| 90 | - ), |
|
| 91 | - array( |
|
| 92 | - 'name' => '', |
|
| 93 | - 'data' => 123, |
|
| 94 | - 'data_type' => 'array', |
|
| 95 | - 'type' => 'memory' |
|
| 96 | - ) |
|
| 97 | - ); |
|
| 98 | - |
|
| 99 | - $console = new Console(); |
|
| 100 | - |
|
| 101 | - $reflectedConsole = new ReflectionClass(get_class($console)); |
|
| 102 | - $reflectedProperty = $reflectedConsole->getProperty('store'); |
|
| 103 | - $reflectedProperty->setAccessible(true); |
|
| 104 | - $reflectedProperty->setValue($console, $store); |
|
| 105 | - |
|
| 106 | - $this->assertSame($store, $console->getLogs()); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - protected function getProtectedStore(Console $console) |
|
| 110 | - { |
|
| 111 | - $reflectedConsole = new ReflectionClass(get_class($console)); |
|
| 112 | - $reflectedProperty = $reflectedConsole->getProperty('store'); |
|
| 113 | - $reflectedProperty->setAccessible(true); |
|
| 114 | - return $reflectedProperty->getValue($console); |
|
| 115 | - } |
|
| 12 | + public function testLog() |
|
| 13 | + { |
|
| 14 | + $data = array( |
|
| 15 | + 'key' => 'value' |
|
| 16 | + ); |
|
| 17 | + |
|
| 18 | + $console = new Console(); |
|
| 19 | + $console->log($data); |
|
| 20 | + $store = $this->getProtectedStore($console); |
|
| 21 | + $log = array_pop($store); |
|
| 22 | + |
|
| 23 | + $this->assertSame($data, $log['data']); |
|
| 24 | + $this->assertEquals('log', $log['type']); |
|
| 25 | + } |
|
| 26 | + |
|
| 27 | + public function testLogMemory() |
|
| 28 | + { |
|
| 29 | + $data = array( |
|
| 30 | + 'key' => 'value' |
|
| 31 | + ); |
|
| 32 | + $memory = strlen(serialize($data)); |
|
| 33 | + $name = 'Test Array'; |
|
| 34 | + |
|
| 35 | + $console = new Console(); |
|
| 36 | + $console->logMemory($data, $name); |
|
| 37 | + $store = $this->getProtectedStore($console); |
|
| 38 | + $log = array_pop($store); |
|
| 39 | + |
|
| 40 | + $this->assertEquals($name, $log['name']); |
|
| 41 | + $this->assertEquals($memory, $log['data']); |
|
| 42 | + $this->assertEquals('array', $log['data_type']); |
|
| 43 | + $this->assertEquals('memory', $log['type']); |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + public function testLogError() |
|
| 47 | + { |
|
| 48 | + $error = new Exception('Test Exception'); |
|
| 49 | + |
|
| 50 | + $console = new Console(); |
|
| 51 | + $console->logError($error); |
|
| 52 | + $store = $this->getProtectedStore($console); |
|
| 53 | + $log = array_pop($store); |
|
| 54 | + |
|
| 55 | + $this->assertEquals($error->getMessage(), $log['data']); |
|
| 56 | + $this->assertEquals($error->getFile(), $log['file']); |
|
| 57 | + $this->assertEquals($error->getLine(), $log['line']); |
|
| 58 | + $this->assertEquals('error', $log['type']); |
|
| 59 | + |
|
| 60 | + $error = new Exception('Test Exception'); |
|
| 61 | + $message = 'override message'; |
|
| 62 | + |
|
| 63 | + $console = new Console(); |
|
| 64 | + $console->logError($error, $message); |
|
| 65 | + $store = $this->getProtectedStore($console); |
|
| 66 | + $log = array_pop($store); |
|
| 67 | + |
|
| 68 | + $this->assertEquals($message, $log['data']); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + public function testLogSpeed() |
|
| 72 | + { |
|
| 73 | + $name = 'Test Speed'; |
|
| 74 | + |
|
| 75 | + $console = new Console(); |
|
| 76 | + $console->logSpeed($name); |
|
| 77 | + $store = $this->getProtectedStore($console); |
|
| 78 | + $log = array_pop($store); |
|
| 79 | + |
|
| 80 | + $this->assertEquals($name, $log['name']); |
|
| 81 | + $this->assertEquals('speed', $log['type']); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + public function testGetLogs() |
|
| 85 | + { |
|
| 86 | + $store = array( |
|
| 87 | + array( |
|
| 88 | + 'data' => 'a string', |
|
| 89 | + 'type' => 'log' |
|
| 90 | + ), |
|
| 91 | + array( |
|
| 92 | + 'name' => '', |
|
| 93 | + 'data' => 123, |
|
| 94 | + 'data_type' => 'array', |
|
| 95 | + 'type' => 'memory' |
|
| 96 | + ) |
|
| 97 | + ); |
|
| 98 | + |
|
| 99 | + $console = new Console(); |
|
| 100 | + |
|
| 101 | + $reflectedConsole = new ReflectionClass(get_class($console)); |
|
| 102 | + $reflectedProperty = $reflectedConsole->getProperty('store'); |
|
| 103 | + $reflectedProperty->setAccessible(true); |
|
| 104 | + $reflectedProperty->setValue($console, $store); |
|
| 105 | + |
|
| 106 | + $this->assertSame($store, $console->getLogs()); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + protected function getProtectedStore(Console $console) |
|
| 110 | + { |
|
| 111 | + $reflectedConsole = new ReflectionClass(get_class($console)); |
|
| 112 | + $reflectedProperty = $reflectedConsole->getProperty('store'); |
|
| 113 | + $reflectedProperty->setAccessible(true); |
|
| 114 | + return $reflectedProperty->getValue($console); |
|
| 115 | + } |
|
| 116 | 116 | } |
@@ -7,107 +7,107 @@ |
||
| 7 | 7 | // namespace hack on microtime functionality |
| 8 | 8 | function microtime() |
| 9 | 9 | { |
| 10 | - return 1450355136.5706; |
|
| 10 | + return 1450355136.5706; |
|
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | // namespace hack on included files functionality |
| 14 | 14 | function get_included_files() |
| 15 | 15 | { |
| 16 | - return array( |
|
| 17 | - 'index.php', |
|
| 18 | - 'src/Class.php' |
|
| 19 | - ); |
|
| 16 | + return array( |
|
| 17 | + 'index.php', |
|
| 18 | + 'src/Class.php' |
|
| 19 | + ); |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | // namespace hack on filesize |
| 23 | 23 | function filesize($filename) |
| 24 | 24 | { |
| 25 | - return strlen($filename) * 100; |
|
| 25 | + return strlen($filename) * 100; |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | // namespace hack on memory usage |
| 29 | 29 | function memory_get_peak_usage() |
| 30 | 30 | { |
| 31 | - return 123456789; |
|
| 31 | + return 123456789; |
|
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | // namespace hack on ini settings |
| 35 | 35 | function ini_get($setting) |
| 36 | 36 | { |
| 37 | - if ($setting == 'memory_limit') { |
|
| 38 | - return '128M'; |
|
| 39 | - } |
|
| 40 | - return \ini_get($setting); |
|
| 37 | + if ($setting == 'memory_limit') { |
|
| 38 | + return '128M'; |
|
| 39 | + } |
|
| 40 | + return \ini_get($setting); |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | class PhpQuickProfilerTest extends PHPUnit_Framework_TestCase |
| 44 | 44 | { |
| 45 | 45 | |
| 46 | - public function testConstruct() |
|
| 47 | - { |
|
| 48 | - $startTime = microtime(true); |
|
| 49 | - |
|
| 50 | - $profiler = new PhpQuickProfiler(); |
|
| 51 | - $this->assertAttributeEquals($startTime, 'startTime', $profiler); |
|
| 52 | - |
|
| 53 | - $profiler = new PhpQuickProfiler($startTime); |
|
| 54 | - $this->assertAttributeEquals($startTime, 'startTime', $profiler); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - public function testSetConsole() |
|
| 58 | - { |
|
| 59 | - $console = new Console(); |
|
| 60 | - $profiler = new PhpQuickProfiler(); |
|
| 61 | - $profiler->setConsole($console); |
|
| 62 | - |
|
| 63 | - $this->assertAttributeSame($console, 'console', $profiler); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - public function testSetDisplay() |
|
| 67 | - { |
|
| 68 | - $display = new Display(); |
|
| 69 | - $profiler = new PhpQuickProfiler(); |
|
| 70 | - $profiler->setDisplay($display); |
|
| 71 | - |
|
| 72 | - $this->assertAttributeSame($display, 'display', $profiler); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - public function testGatherFileData() |
|
| 76 | - { |
|
| 77 | - $files = get_included_files(); |
|
| 78 | - $profiler = new PhpQuickProfiler(); |
|
| 79 | - $gatheredFileData = $profiler->gatherFileData(); |
|
| 80 | - |
|
| 81 | - foreach ($gatheredFileData as $fileData) { |
|
| 82 | - $this->assertArrayHasKey('name', $fileData); |
|
| 83 | - $this->assertContains($fileData['name'], $files); |
|
| 84 | - $this->assertArrayHasKey('size', $fileData); |
|
| 85 | - $this->assertEquals($fileData['size'], filesize($fileData['name'])); |
|
| 86 | - } |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - public function testGatherMemoryData() |
|
| 90 | - { |
|
| 91 | - $memory_usage = memory_get_peak_usage(); |
|
| 92 | - $allowed_limit = ini_get('memory_limit'); |
|
| 93 | - $profiler = new PhpQuickProfiler(); |
|
| 94 | - $gatheredMemoryData = $profiler->gatherMemoryData(); |
|
| 95 | - |
|
| 96 | - $this->assertArrayHasKey('used', $gatheredMemoryData); |
|
| 97 | - $this->assertEquals($memory_usage, $gatheredMemoryData['used']); |
|
| 98 | - $this->assertArrayHasKey('allowed', $gatheredMemoryData); |
|
| 99 | - $this->assertEquals($allowed_limit, $gatheredMemoryData['allowed']); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - public function testSetProfiledQueries() |
|
| 103 | - { |
|
| 104 | - $profiledQueries = array( |
|
| 105 | - 'sql' => 'SELECT * FROM example', |
|
| 106 | - 'time' => 25 |
|
| 107 | - ); |
|
| 108 | - $profiler = new PhpQuickProfiler(); |
|
| 109 | - $profiler->setProfiledQueries($profiledQueries); |
|
| 110 | - |
|
| 111 | - $this->assertAttributeEquals($profiledQueries, 'profiledQueries', $profiler); |
|
| 112 | - } |
|
| 46 | + public function testConstruct() |
|
| 47 | + { |
|
| 48 | + $startTime = microtime(true); |
|
| 49 | + |
|
| 50 | + $profiler = new PhpQuickProfiler(); |
|
| 51 | + $this->assertAttributeEquals($startTime, 'startTime', $profiler); |
|
| 52 | + |
|
| 53 | + $profiler = new PhpQuickProfiler($startTime); |
|
| 54 | + $this->assertAttributeEquals($startTime, 'startTime', $profiler); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + public function testSetConsole() |
|
| 58 | + { |
|
| 59 | + $console = new Console(); |
|
| 60 | + $profiler = new PhpQuickProfiler(); |
|
| 61 | + $profiler->setConsole($console); |
|
| 62 | + |
|
| 63 | + $this->assertAttributeSame($console, 'console', $profiler); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + public function testSetDisplay() |
|
| 67 | + { |
|
| 68 | + $display = new Display(); |
|
| 69 | + $profiler = new PhpQuickProfiler(); |
|
| 70 | + $profiler->setDisplay($display); |
|
| 71 | + |
|
| 72 | + $this->assertAttributeSame($display, 'display', $profiler); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + public function testGatherFileData() |
|
| 76 | + { |
|
| 77 | + $files = get_included_files(); |
|
| 78 | + $profiler = new PhpQuickProfiler(); |
|
| 79 | + $gatheredFileData = $profiler->gatherFileData(); |
|
| 80 | + |
|
| 81 | + foreach ($gatheredFileData as $fileData) { |
|
| 82 | + $this->assertArrayHasKey('name', $fileData); |
|
| 83 | + $this->assertContains($fileData['name'], $files); |
|
| 84 | + $this->assertArrayHasKey('size', $fileData); |
|
| 85 | + $this->assertEquals($fileData['size'], filesize($fileData['name'])); |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + public function testGatherMemoryData() |
|
| 90 | + { |
|
| 91 | + $memory_usage = memory_get_peak_usage(); |
|
| 92 | + $allowed_limit = ini_get('memory_limit'); |
|
| 93 | + $profiler = new PhpQuickProfiler(); |
|
| 94 | + $gatheredMemoryData = $profiler->gatherMemoryData(); |
|
| 95 | + |
|
| 96 | + $this->assertArrayHasKey('used', $gatheredMemoryData); |
|
| 97 | + $this->assertEquals($memory_usage, $gatheredMemoryData['used']); |
|
| 98 | + $this->assertArrayHasKey('allowed', $gatheredMemoryData); |
|
| 99 | + $this->assertEquals($allowed_limit, $gatheredMemoryData['allowed']); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + public function testSetProfiledQueries() |
|
| 103 | + { |
|
| 104 | + $profiledQueries = array( |
|
| 105 | + 'sql' => 'SELECT * FROM example', |
|
| 106 | + 'time' => 25 |
|
| 107 | + ); |
|
| 108 | + $profiler = new PhpQuickProfiler(); |
|
| 109 | + $profiler->setProfiledQueries($profiledQueries); |
|
| 110 | + |
|
| 111 | + $this->assertAttributeEquals($profiledQueries, 'profiledQueries', $profiler); |
|
| 112 | + } |
|
| 113 | 113 | } |