@@ -15,136 +15,136 @@ |
||
| 15 | 15 | class PhpQuickProfiler |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** @var Console */ |
|
| 19 | - protected $console; |
|
| 20 | - |
|
| 21 | - /** @var integer */ |
|
| 22 | - protected $startTime; |
|
| 23 | - |
|
| 24 | - /** @var object */ |
|
| 25 | - protected $pdo; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @param Console $console |
|
| 29 | - * @param object $pdo |
|
| 30 | - * @param integer $startTime |
|
| 31 | - */ |
|
| 32 | - public function __construct(Console $console, $pdo = null, $startTime = null) |
|
| 33 | - { |
|
| 34 | - $this->console = $console; |
|
| 35 | - $this->pdo = $pdo; |
|
| 36 | - |
|
| 37 | - if (is_null($startTime)) { |
|
| 38 | - $startTime = microtime(true); |
|
| 39 | - } |
|
| 40 | - $this->startTime = $startTime; |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Get data about files loaded for the application to current point |
|
| 45 | - * |
|
| 46 | - * @returns array |
|
| 47 | - */ |
|
| 48 | - public function gatherFileData() |
|
| 49 | - { |
|
| 50 | - $files = get_included_files(); |
|
| 51 | - $data = array(); |
|
| 52 | - foreach ($files as $file) { |
|
| 53 | - array_push($data, array( |
|
| 54 | - 'name' => $file, |
|
| 55 | - 'size' => filesize($file) |
|
| 56 | - )); |
|
| 57 | - } |
|
| 58 | - return $data; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Get data about memory usage of the application |
|
| 63 | - * |
|
| 64 | - * @returns array |
|
| 65 | - */ |
|
| 66 | - public function gatherMemoryData() |
|
| 67 | - { |
|
| 68 | - $usedMemory = memory_get_peak_usage(); |
|
| 69 | - $allowedMemory = ini_get('memory_limit'); |
|
| 70 | - return array( |
|
| 71 | - 'used' => $usedMemory, |
|
| 72 | - 'allowed' => $allowedMemory |
|
| 73 | - ); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Get data about sql usage of the application |
|
| 78 | - * |
|
| 79 | - * @param array $profiledQueries |
|
| 80 | - * @returns array |
|
| 81 | - */ |
|
| 82 | - public function gatherQueryData(array $profiledQueries) |
|
| 83 | - { |
|
| 84 | - $data = array(); |
|
| 85 | - foreach ($profiledQueries as $query) { |
|
| 86 | - if ($query['function'] !== 'perform') { |
|
| 87 | - continue; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - array_push($data, array( |
|
| 91 | - 'sql' => $query['statement'], |
|
| 92 | - 'explain' => $this->explainQuery($query['statement'], $query['bind_values']), |
|
| 93 | - 'time' => $query['duration'] |
|
| 94 | - )); |
|
| 95 | - } |
|
| 96 | - return $data; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Attempts to explain a query |
|
| 101 | - * |
|
| 102 | - * @param string $query |
|
| 103 | - * @param array $parameters |
|
| 104 | - * @return array |
|
| 105 | - */ |
|
| 106 | - protected function explainQuery($query, $parameters) |
|
| 107 | - { |
|
| 108 | - $query = "EXPLAIN {$query}"; |
|
| 109 | - try { |
|
| 110 | - $statement = $this->pdo->prepare($query); |
|
| 111 | - $statement->execute($parameters); |
|
| 112 | - return $statement->fetch(\PDO::FETCH_ASSOC); |
|
| 113 | - } catch (\Exception $e) { |
|
| 114 | - echo $e->getMessage(); |
|
| 115 | - } |
|
| 116 | - return ''; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Get data about speed of the application |
|
| 121 | - * |
|
| 122 | - * @returns array |
|
| 123 | - */ |
|
| 124 | - public function gatherSpeedData() |
|
| 125 | - { |
|
| 126 | - $elapsedTime = microtime(true) - $this->startTime; |
|
| 127 | - $allowedTime = ini_get('max_execution_time'); |
|
| 128 | - return array( |
|
| 129 | - 'elapsed' => $elapsedTime, |
|
| 130 | - 'allowed' => $allowedTime |
|
| 131 | - ); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Triggers end display of the profiling data |
|
| 136 | - * |
|
| 137 | - * @param Display $display |
|
| 138 | - * @param array $profiledQueries |
|
| 139 | - */ |
|
| 140 | - public function display(Display $display, array $profiledQueries = array()) |
|
| 141 | - { |
|
| 142 | - $display->setConsole($this->console); |
|
| 143 | - $display->setFileData($this->gatherFileData()); |
|
| 144 | - $display->setMemoryData($this->gatherMemoryData()); |
|
| 145 | - $display->setQueryData($this->gatherQueryData($profiledQueries)); |
|
| 146 | - $display->setSpeedData($this->gatherSpeedData()); |
|
| 147 | - |
|
| 148 | - $display(); |
|
| 149 | - } |
|
| 18 | + /** @var Console */ |
|
| 19 | + protected $console; |
|
| 20 | + |
|
| 21 | + /** @var integer */ |
|
| 22 | + protected $startTime; |
|
| 23 | + |
|
| 24 | + /** @var object */ |
|
| 25 | + protected $pdo; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @param Console $console |
|
| 29 | + * @param object $pdo |
|
| 30 | + * @param integer $startTime |
|
| 31 | + */ |
|
| 32 | + public function __construct(Console $console, $pdo = null, $startTime = null) |
|
| 33 | + { |
|
| 34 | + $this->console = $console; |
|
| 35 | + $this->pdo = $pdo; |
|
| 36 | + |
|
| 37 | + if (is_null($startTime)) { |
|
| 38 | + $startTime = microtime(true); |
|
| 39 | + } |
|
| 40 | + $this->startTime = $startTime; |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Get data about files loaded for the application to current point |
|
| 45 | + * |
|
| 46 | + * @returns array |
|
| 47 | + */ |
|
| 48 | + public function gatherFileData() |
|
| 49 | + { |
|
| 50 | + $files = get_included_files(); |
|
| 51 | + $data = array(); |
|
| 52 | + foreach ($files as $file) { |
|
| 53 | + array_push($data, array( |
|
| 54 | + 'name' => $file, |
|
| 55 | + 'size' => filesize($file) |
|
| 56 | + )); |
|
| 57 | + } |
|
| 58 | + return $data; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Get data about memory usage of the application |
|
| 63 | + * |
|
| 64 | + * @returns array |
|
| 65 | + */ |
|
| 66 | + public function gatherMemoryData() |
|
| 67 | + { |
|
| 68 | + $usedMemory = memory_get_peak_usage(); |
|
| 69 | + $allowedMemory = ini_get('memory_limit'); |
|
| 70 | + return array( |
|
| 71 | + 'used' => $usedMemory, |
|
| 72 | + 'allowed' => $allowedMemory |
|
| 73 | + ); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Get data about sql usage of the application |
|
| 78 | + * |
|
| 79 | + * @param array $profiledQueries |
|
| 80 | + * @returns array |
|
| 81 | + */ |
|
| 82 | + public function gatherQueryData(array $profiledQueries) |
|
| 83 | + { |
|
| 84 | + $data = array(); |
|
| 85 | + foreach ($profiledQueries as $query) { |
|
| 86 | + if ($query['function'] !== 'perform') { |
|
| 87 | + continue; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + array_push($data, array( |
|
| 91 | + 'sql' => $query['statement'], |
|
| 92 | + 'explain' => $this->explainQuery($query['statement'], $query['bind_values']), |
|
| 93 | + 'time' => $query['duration'] |
|
| 94 | + )); |
|
| 95 | + } |
|
| 96 | + return $data; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Attempts to explain a query |
|
| 101 | + * |
|
| 102 | + * @param string $query |
|
| 103 | + * @param array $parameters |
|
| 104 | + * @return array |
|
| 105 | + */ |
|
| 106 | + protected function explainQuery($query, $parameters) |
|
| 107 | + { |
|
| 108 | + $query = "EXPLAIN {$query}"; |
|
| 109 | + try { |
|
| 110 | + $statement = $this->pdo->prepare($query); |
|
| 111 | + $statement->execute($parameters); |
|
| 112 | + return $statement->fetch(\PDO::FETCH_ASSOC); |
|
| 113 | + } catch (\Exception $e) { |
|
| 114 | + echo $e->getMessage(); |
|
| 115 | + } |
|
| 116 | + return ''; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Get data about speed of the application |
|
| 121 | + * |
|
| 122 | + * @returns array |
|
| 123 | + */ |
|
| 124 | + public function gatherSpeedData() |
|
| 125 | + { |
|
| 126 | + $elapsedTime = microtime(true) - $this->startTime; |
|
| 127 | + $allowedTime = ini_get('max_execution_time'); |
|
| 128 | + return array( |
|
| 129 | + 'elapsed' => $elapsedTime, |
|
| 130 | + 'allowed' => $allowedTime |
|
| 131 | + ); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Triggers end display of the profiling data |
|
| 136 | + * |
|
| 137 | + * @param Display $display |
|
| 138 | + * @param array $profiledQueries |
|
| 139 | + */ |
|
| 140 | + public function display(Display $display, array $profiledQueries = array()) |
|
| 141 | + { |
|
| 142 | + $display->setConsole($this->console); |
|
| 143 | + $display->setFileData($this->gatherFileData()); |
|
| 144 | + $display->setMemoryData($this->gatherMemoryData()); |
|
| 145 | + $display->setQueryData($this->gatherQueryData($profiledQueries)); |
|
| 146 | + $display->setSpeedData($this->gatherSpeedData()); |
|
| 147 | + |
|
| 148 | + $display(); |
|
| 149 | + } |
|
| 150 | 150 | } |
@@ -14,204 +14,204 @@ |
||
| 14 | 14 | class Display |
| 15 | 15 | { |
| 16 | 16 | |
| 17 | - /** @var array */ |
|
| 18 | - protected $output; |
|
| 19 | - |
|
| 20 | - public function __construct() |
|
| 21 | - { |
|
| 22 | - } |
|
| 23 | - |
|
| 24 | - public function setConsole(Console $console) |
|
| 25 | - { |
|
| 26 | - $console_data = array( |
|
| 27 | - 'messages' => array(), |
|
| 28 | - 'count' => array( |
|
| 29 | - 'log' => 0, |
|
| 30 | - 'memory' => 0, |
|
| 31 | - 'error' => 0, |
|
| 32 | - 'speed' => 0 |
|
| 33 | - ) |
|
| 34 | - ); |
|
| 35 | - foreach ($console->getLogs() as $log) { |
|
| 36 | - switch($log['type']) { |
|
| 37 | - case 'log': |
|
| 38 | - $message = array( |
|
| 39 | - 'data' => print_r($log['data'], true), |
|
| 40 | - 'type' => 'log' |
|
| 41 | - ); |
|
| 42 | - $console_data['count']['log']++; |
|
| 43 | - break; |
|
| 44 | - case 'memory': |
|
| 45 | - $message = array( |
|
| 46 | - 'name' => $log['name'], |
|
| 47 | - 'data' => self::getReadableMemory($log['data']), |
|
| 48 | - 'type' => 'memory' |
|
| 49 | - ); |
|
| 50 | - if (!empty($log['data_type'])) { |
|
| 51 | - $message['data_type'] = $log['data_type']; |
|
| 52 | - } |
|
| 53 | - $console_data['count']['memory']++; |
|
| 54 | - break; |
|
| 55 | - case 'error': |
|
| 56 | - $message = array( |
|
| 57 | - 'data' => $log['data'], |
|
| 58 | - 'file' => $log['file'], |
|
| 59 | - 'line' => $log['line'], |
|
| 60 | - 'type' => 'error' |
|
| 61 | - ); |
|
| 62 | - $console_data['count']['error']++; |
|
| 63 | - break; |
|
| 64 | - case 'speed': |
|
| 65 | - $elapsedTime = $log['data'] - $this->startTime; |
|
| 66 | - $message = array( |
|
| 67 | - 'name' => $log['name'], |
|
| 68 | - 'data' => self::getReadableTime($elapsedTime), |
|
| 69 | - 'type' => 'speed' |
|
| 70 | - ); |
|
| 71 | - $console_data['count']['speed']++; |
|
| 72 | - break; |
|
| 73 | - default: |
|
| 74 | - $message = array( |
|
| 75 | - 'data' => "Unrecognized console log type: {$log['type']}", |
|
| 76 | - 'type' => 'error' |
|
| 77 | - ); |
|
| 78 | - $console_data['count']['error']++; |
|
| 79 | - break; |
|
| 80 | - } |
|
| 81 | - array_push($console_data['messages'], $message); |
|
| 82 | - } |
|
| 83 | - $this->output['console'] = $console_data; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Sets file data |
|
| 88 | - * |
|
| 89 | - * @param array $data |
|
| 90 | - */ |
|
| 91 | - public function setFileData(array $data) |
|
| 92 | - { |
|
| 93 | - $fileData = array( |
|
| 94 | - 'fileList' => array(), |
|
| 95 | - 'fileTotals' => array( |
|
| 96 | - 'count' => count($data), |
|
| 97 | - 'size' => 0, |
|
| 98 | - 'largest' => 0 |
|
| 99 | - ) |
|
| 100 | - ); |
|
| 101 | - |
|
| 102 | - foreach ($data as $file) { |
|
| 103 | - array_push($fileData['fileList'], array( |
|
| 104 | - 'name' => $file['name'], |
|
| 105 | - 'size' => self::getReadableMemory($file['size']) |
|
| 106 | - )); |
|
| 107 | - |
|
| 108 | - $fileData['fileTotals']['size'] += $file['size']; |
|
| 109 | - if ($file['size'] > $fileData['fileTotals']['largest']) { |
|
| 110 | - $fileData['fileTotals']['largest'] = $file['size']; |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - $fileData['fileTotals']['size'] = self::getReadableMemory($fileData['fileTotals']['size']); |
|
| 115 | - $fileData['fileTotals']['largest'] = self::getReadableMemory($fileData['fileTotals']['largest']); |
|
| 116 | - |
|
| 117 | - $this->output['files'] = $fileData['fileList']; |
|
| 118 | - $this->output['fileTotals'] = $fileData['fileTotals']; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Sets memory data |
|
| 123 | - * |
|
| 124 | - * @param array $data |
|
| 125 | - */ |
|
| 126 | - public function setMemoryData(array $data) |
|
| 127 | - { |
|
| 128 | - $this->output['memory'] = array( |
|
| 129 | - 'used' => self::getReadableMemory($data['used']), |
|
| 130 | - 'allowed' => $data['allowed'] |
|
| 131 | - ); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - public function setQueryData(array $data) |
|
| 135 | - { |
|
| 136 | - $queryData = array( |
|
| 137 | - 'queries' => array(), |
|
| 138 | - 'queryTotals' => array( |
|
| 139 | - 'count' => count($data), |
|
| 140 | - 'time' => 0, |
|
| 141 | - ) |
|
| 142 | - ); |
|
| 143 | - |
|
| 144 | - foreach ($data as $query) { |
|
| 145 | - array_push($queryData['queries'], array( |
|
| 146 | - 'sql' => $query['sql'], |
|
| 147 | - 'explain' => $query['explain'], |
|
| 148 | - 'time' => self::getReadableTime($query['time']) |
|
| 149 | - )); |
|
| 150 | - |
|
| 151 | - $queryData['queryTotals']['time'] += $query['time']; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - $queryData['queryTotals']['time'] = self::getReadableTime($queryData['queryTotals']['time']); |
|
| 155 | - |
|
| 156 | - $this->output['queries'] = $queryData['queries']; |
|
| 157 | - $this->output['queryTotals'] = $queryData['queryTotals']; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Sets speed data |
|
| 162 | - * |
|
| 163 | - * @param array $data |
|
| 164 | - */ |
|
| 165 | - public function setSpeedData(array $data) |
|
| 166 | - { |
|
| 167 | - $this->output['speed'] = array( |
|
| 168 | - 'elapsed' => self::getReadableTime($data['elapsed']), |
|
| 169 | - 'allowed' => self::getReadableTime($data['allowed'], 0) |
|
| 170 | - ); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * Static formatter for human-readable time |
|
| 175 | - * Only handles time up to 60 minutes gracefully |
|
| 176 | - * |
|
| 177 | - * @param double $time |
|
| 178 | - * @param integer $decimals |
|
| 179 | - * @return string |
|
| 180 | - */ |
|
| 181 | - public static function getReadableTime($time, $decimals = 3) |
|
| 182 | - { |
|
| 183 | - $unit = 's'; |
|
| 184 | - if ($time < 1) { |
|
| 185 | - $time *= 1000; |
|
| 186 | - $unit = 'ms'; |
|
| 187 | - } else if ($time > 60) { |
|
| 188 | - $time /= 60; |
|
| 189 | - $unit = 'm'; |
|
| 190 | - } |
|
| 191 | - $time = number_format($time, $decimals); |
|
| 192 | - return "{$time} {$unit}"; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Static formatter for human-readable memory |
|
| 197 | - * |
|
| 198 | - * @param double $size |
|
| 199 | - * @param integer $decimals |
|
| 200 | - */ |
|
| 201 | - public static function getReadableMemory($size, $decimals = 2) |
|
| 202 | - { |
|
| 203 | - $unitOptions = array('b', 'k', 'M', 'G'); |
|
| 204 | - |
|
| 205 | - $base = log($size, 1024); |
|
| 206 | - |
|
| 207 | - $memory = round(pow(1024, $base - floor($base)), $decimals); |
|
| 208 | - $unit = $unitOptions[floor($base)]; |
|
| 209 | - return "{$memory} {$unit}"; |
|
| 210 | - } |
|
| 17 | + /** @var array */ |
|
| 18 | + protected $output; |
|
| 19 | + |
|
| 20 | + public function __construct() |
|
| 21 | + { |
|
| 22 | + } |
|
| 23 | + |
|
| 24 | + public function setConsole(Console $console) |
|
| 25 | + { |
|
| 26 | + $console_data = array( |
|
| 27 | + 'messages' => array(), |
|
| 28 | + 'count' => array( |
|
| 29 | + 'log' => 0, |
|
| 30 | + 'memory' => 0, |
|
| 31 | + 'error' => 0, |
|
| 32 | + 'speed' => 0 |
|
| 33 | + ) |
|
| 34 | + ); |
|
| 35 | + foreach ($console->getLogs() as $log) { |
|
| 36 | + switch($log['type']) { |
|
| 37 | + case 'log': |
|
| 38 | + $message = array( |
|
| 39 | + 'data' => print_r($log['data'], true), |
|
| 40 | + 'type' => 'log' |
|
| 41 | + ); |
|
| 42 | + $console_data['count']['log']++; |
|
| 43 | + break; |
|
| 44 | + case 'memory': |
|
| 45 | + $message = array( |
|
| 46 | + 'name' => $log['name'], |
|
| 47 | + 'data' => self::getReadableMemory($log['data']), |
|
| 48 | + 'type' => 'memory' |
|
| 49 | + ); |
|
| 50 | + if (!empty($log['data_type'])) { |
|
| 51 | + $message['data_type'] = $log['data_type']; |
|
| 52 | + } |
|
| 53 | + $console_data['count']['memory']++; |
|
| 54 | + break; |
|
| 55 | + case 'error': |
|
| 56 | + $message = array( |
|
| 57 | + 'data' => $log['data'], |
|
| 58 | + 'file' => $log['file'], |
|
| 59 | + 'line' => $log['line'], |
|
| 60 | + 'type' => 'error' |
|
| 61 | + ); |
|
| 62 | + $console_data['count']['error']++; |
|
| 63 | + break; |
|
| 64 | + case 'speed': |
|
| 65 | + $elapsedTime = $log['data'] - $this->startTime; |
|
| 66 | + $message = array( |
|
| 67 | + 'name' => $log['name'], |
|
| 68 | + 'data' => self::getReadableTime($elapsedTime), |
|
| 69 | + 'type' => 'speed' |
|
| 70 | + ); |
|
| 71 | + $console_data['count']['speed']++; |
|
| 72 | + break; |
|
| 73 | + default: |
|
| 74 | + $message = array( |
|
| 75 | + 'data' => "Unrecognized console log type: {$log['type']}", |
|
| 76 | + 'type' => 'error' |
|
| 77 | + ); |
|
| 78 | + $console_data['count']['error']++; |
|
| 79 | + break; |
|
| 80 | + } |
|
| 81 | + array_push($console_data['messages'], $message); |
|
| 82 | + } |
|
| 83 | + $this->output['console'] = $console_data; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Sets file data |
|
| 88 | + * |
|
| 89 | + * @param array $data |
|
| 90 | + */ |
|
| 91 | + public function setFileData(array $data) |
|
| 92 | + { |
|
| 93 | + $fileData = array( |
|
| 94 | + 'fileList' => array(), |
|
| 95 | + 'fileTotals' => array( |
|
| 96 | + 'count' => count($data), |
|
| 97 | + 'size' => 0, |
|
| 98 | + 'largest' => 0 |
|
| 99 | + ) |
|
| 100 | + ); |
|
| 101 | + |
|
| 102 | + foreach ($data as $file) { |
|
| 103 | + array_push($fileData['fileList'], array( |
|
| 104 | + 'name' => $file['name'], |
|
| 105 | + 'size' => self::getReadableMemory($file['size']) |
|
| 106 | + )); |
|
| 107 | + |
|
| 108 | + $fileData['fileTotals']['size'] += $file['size']; |
|
| 109 | + if ($file['size'] > $fileData['fileTotals']['largest']) { |
|
| 110 | + $fileData['fileTotals']['largest'] = $file['size']; |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + $fileData['fileTotals']['size'] = self::getReadableMemory($fileData['fileTotals']['size']); |
|
| 115 | + $fileData['fileTotals']['largest'] = self::getReadableMemory($fileData['fileTotals']['largest']); |
|
| 116 | + |
|
| 117 | + $this->output['files'] = $fileData['fileList']; |
|
| 118 | + $this->output['fileTotals'] = $fileData['fileTotals']; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Sets memory data |
|
| 123 | + * |
|
| 124 | + * @param array $data |
|
| 125 | + */ |
|
| 126 | + public function setMemoryData(array $data) |
|
| 127 | + { |
|
| 128 | + $this->output['memory'] = array( |
|
| 129 | + 'used' => self::getReadableMemory($data['used']), |
|
| 130 | + 'allowed' => $data['allowed'] |
|
| 131 | + ); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + public function setQueryData(array $data) |
|
| 135 | + { |
|
| 136 | + $queryData = array( |
|
| 137 | + 'queries' => array(), |
|
| 138 | + 'queryTotals' => array( |
|
| 139 | + 'count' => count($data), |
|
| 140 | + 'time' => 0, |
|
| 141 | + ) |
|
| 142 | + ); |
|
| 143 | + |
|
| 144 | + foreach ($data as $query) { |
|
| 145 | + array_push($queryData['queries'], array( |
|
| 146 | + 'sql' => $query['sql'], |
|
| 147 | + 'explain' => $query['explain'], |
|
| 148 | + 'time' => self::getReadableTime($query['time']) |
|
| 149 | + )); |
|
| 150 | + |
|
| 151 | + $queryData['queryTotals']['time'] += $query['time']; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + $queryData['queryTotals']['time'] = self::getReadableTime($queryData['queryTotals']['time']); |
|
| 155 | + |
|
| 156 | + $this->output['queries'] = $queryData['queries']; |
|
| 157 | + $this->output['queryTotals'] = $queryData['queryTotals']; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Sets speed data |
|
| 162 | + * |
|
| 163 | + * @param array $data |
|
| 164 | + */ |
|
| 165 | + public function setSpeedData(array $data) |
|
| 166 | + { |
|
| 167 | + $this->output['speed'] = array( |
|
| 168 | + 'elapsed' => self::getReadableTime($data['elapsed']), |
|
| 169 | + 'allowed' => self::getReadableTime($data['allowed'], 0) |
|
| 170 | + ); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * Static formatter for human-readable time |
|
| 175 | + * Only handles time up to 60 minutes gracefully |
|
| 176 | + * |
|
| 177 | + * @param double $time |
|
| 178 | + * @param integer $decimals |
|
| 179 | + * @return string |
|
| 180 | + */ |
|
| 181 | + public static function getReadableTime($time, $decimals = 3) |
|
| 182 | + { |
|
| 183 | + $unit = 's'; |
|
| 184 | + if ($time < 1) { |
|
| 185 | + $time *= 1000; |
|
| 186 | + $unit = 'ms'; |
|
| 187 | + } else if ($time > 60) { |
|
| 188 | + $time /= 60; |
|
| 189 | + $unit = 'm'; |
|
| 190 | + } |
|
| 191 | + $time = number_format($time, $decimals); |
|
| 192 | + return "{$time} {$unit}"; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Static formatter for human-readable memory |
|
| 197 | + * |
|
| 198 | + * @param double $size |
|
| 199 | + * @param integer $decimals |
|
| 200 | + */ |
|
| 201 | + public static function getReadableMemory($size, $decimals = 2) |
|
| 202 | + { |
|
| 203 | + $unitOptions = array('b', 'k', 'M', 'G'); |
|
| 204 | + |
|
| 205 | + $base = log($size, 1024); |
|
| 206 | + |
|
| 207 | + $memory = round(pow(1024, $base - floor($base)), $decimals); |
|
| 208 | + $unit = $unitOptions[floor($base)]; |
|
| 209 | + return "{$memory} {$unit}"; |
|
| 210 | + } |
|
| 211 | 211 | |
| 212 | - public function __invoke() |
|
| 213 | - { |
|
| 214 | - $output = $this->output; |
|
| 215 | - require_once __DIR__ .'/../asset/display.tpl.php'; |
|
| 216 | - } |
|
| 212 | + public function __invoke() |
|
| 213 | + { |
|
| 214 | + $output = $this->output; |
|
| 215 | + require_once __DIR__ .'/../asset/display.tpl.php'; |
|
| 216 | + } |
|
| 217 | 217 | } |