@@ -15,165 +15,165 @@ |
||
| 15 | 15 | class PhpQuickProfiler |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** @var integer */ |
|
| 19 | - protected $startTime; |
|
| 20 | - |
|
| 21 | - /** @var Console */ |
|
| 22 | - protected $console; |
|
| 23 | - |
|
| 24 | - /** @var Display */ |
|
| 25 | - protected $display; |
|
| 26 | - |
|
| 27 | - /** @var array */ |
|
| 28 | - protected $profiledQueries = array(); |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @param integer $startTime |
|
| 32 | - */ |
|
| 33 | - public function __construct($startTime = null) |
|
| 34 | - { |
|
| 35 | - if (is_null($startTime)) { |
|
| 36 | - $startTime = microtime(true); |
|
| 37 | - } |
|
| 38 | - $this->startTime = $startTime; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @param Console $console |
|
| 43 | - */ |
|
| 44 | - public function setConsole(Console $console) |
|
| 45 | - { |
|
| 46 | - $this->console = $console; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * @param Display $display |
|
| 51 | - */ |
|
| 52 | - public function setDisplay(Display $display) |
|
| 53 | - { |
|
| 54 | - $this->display = $display; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Get data about files loaded for the application to current point |
|
| 59 | - * |
|
| 60 | - * @returns array |
|
| 61 | - */ |
|
| 62 | - public function gatherFileData() |
|
| 63 | - { |
|
| 64 | - $files = get_included_files(); |
|
| 65 | - $data = array(); |
|
| 66 | - foreach ($files as $file) { |
|
| 67 | - array_push($data, array( |
|
| 68 | - 'name' => $file, |
|
| 69 | - 'size' => filesize($file) |
|
| 70 | - )); |
|
| 71 | - } |
|
| 72 | - return $data; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Get data about memory usage of the application |
|
| 77 | - * |
|
| 78 | - * @returns array |
|
| 79 | - */ |
|
| 80 | - public function gatherMemoryData() |
|
| 81 | - { |
|
| 82 | - $usedMemory = memory_get_peak_usage(); |
|
| 83 | - $allowedMemory = ini_get('memory_limit'); |
|
| 84 | - return array( |
|
| 85 | - 'used' => $usedMemory, |
|
| 86 | - 'allowed' => $allowedMemory |
|
| 87 | - ); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @param array $profiled_queries |
|
| 92 | - */ |
|
| 93 | - public function setProfiledQueries(array $profiledQueries) |
|
| 94 | - { |
|
| 95 | - $this->profiledQueries = $profiledQueries; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Get data about sql usage of the application |
|
| 100 | - * |
|
| 101 | - * @param object $dbConnection |
|
| 102 | - * @returns array |
|
| 103 | - */ |
|
| 104 | - public function gatherQueryData($dbConnection) |
|
| 105 | - { |
|
| 106 | - if (empty($this->profiledQueries) && property_exists($dbConnection, 'queries')) { |
|
| 107 | - $this->setProfiledQueries($dbConnection->queries); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - $data = array(); |
|
| 111 | - foreach ($this->profiledQueries as $query) { |
|
| 112 | - if ($query['function'] !== 'perform') { |
|
| 113 | - continue; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - array_push($data, array( |
|
| 117 | - 'sql' => $query['statement'], |
|
| 118 | - 'explain' => $this->explainQuery($query['statement'], $query['bind_values']), |
|
| 119 | - 'time' => $query['duration'] |
|
| 120 | - )); |
|
| 121 | - } |
|
| 122 | - return $data; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Attempts to explain a query |
|
| 127 | - * |
|
| 128 | - * @param string $query |
|
| 129 | - * @param array $parameters |
|
| 130 | - * @return array |
|
| 131 | - */ |
|
| 132 | - protected function explainQuery($query, $parameters) |
|
| 133 | - { |
|
| 134 | - $query = "EXPLAIN {$query}"; |
|
| 135 | - try { |
|
| 136 | - $statement = $this->dbConnection->prepare($query); |
|
| 137 | - $statement->execute($parameters); |
|
| 138 | - return $statement->fetch(\PDO::FETCH_ASSOC); |
|
| 139 | - } catch (\Exception $e) { |
|
| 140 | - echo $e->getMessage(); |
|
| 141 | - } |
|
| 142 | - return ''; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Get data about speed of the application |
|
| 147 | - * |
|
| 148 | - * @returns array |
|
| 149 | - */ |
|
| 150 | - public function gatherSpeedData() |
|
| 151 | - { |
|
| 152 | - $elapsedTime = microtime(true) - $this->startTime; |
|
| 153 | - $allowedTime = ini_get('max_execution_time'); |
|
| 154 | - return array( |
|
| 155 | - 'elapsed' => $elapsedTime, |
|
| 156 | - 'allowed' => $allowedTime |
|
| 157 | - ); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Triggers end display of the profiling data |
|
| 162 | - * |
|
| 163 | - * @param object $dbConnection |
|
| 164 | - */ |
|
| 165 | - public function display($dbConnection = null) |
|
| 166 | - { |
|
| 167 | - if (!isset($this->display)) { |
|
| 168 | - throw new Exception('Display object has not been injected into Profiler'); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - $this->display->setConsole($this->console); |
|
| 172 | - $this->display->setFileData($this->gatherFileData()); |
|
| 173 | - $this->display->setMemoryData($this->gatherMemoryData()); |
|
| 174 | - $this->display->setQueryData($this->gatherQueryData($dbConnection)); |
|
| 175 | - $this->display->setSpeedData($this->gatherSpeedData()); |
|
| 176 | - |
|
| 177 | - $this->display(); |
|
| 178 | - } |
|
| 18 | + /** @var integer */ |
|
| 19 | + protected $startTime; |
|
| 20 | + |
|
| 21 | + /** @var Console */ |
|
| 22 | + protected $console; |
|
| 23 | + |
|
| 24 | + /** @var Display */ |
|
| 25 | + protected $display; |
|
| 26 | + |
|
| 27 | + /** @var array */ |
|
| 28 | + protected $profiledQueries = array(); |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @param integer $startTime |
|
| 32 | + */ |
|
| 33 | + public function __construct($startTime = null) |
|
| 34 | + { |
|
| 35 | + if (is_null($startTime)) { |
|
| 36 | + $startTime = microtime(true); |
|
| 37 | + } |
|
| 38 | + $this->startTime = $startTime; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @param Console $console |
|
| 43 | + */ |
|
| 44 | + public function setConsole(Console $console) |
|
| 45 | + { |
|
| 46 | + $this->console = $console; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * @param Display $display |
|
| 51 | + */ |
|
| 52 | + public function setDisplay(Display $display) |
|
| 53 | + { |
|
| 54 | + $this->display = $display; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Get data about files loaded for the application to current point |
|
| 59 | + * |
|
| 60 | + * @returns array |
|
| 61 | + */ |
|
| 62 | + public function gatherFileData() |
|
| 63 | + { |
|
| 64 | + $files = get_included_files(); |
|
| 65 | + $data = array(); |
|
| 66 | + foreach ($files as $file) { |
|
| 67 | + array_push($data, array( |
|
| 68 | + 'name' => $file, |
|
| 69 | + 'size' => filesize($file) |
|
| 70 | + )); |
|
| 71 | + } |
|
| 72 | + return $data; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Get data about memory usage of the application |
|
| 77 | + * |
|
| 78 | + * @returns array |
|
| 79 | + */ |
|
| 80 | + public function gatherMemoryData() |
|
| 81 | + { |
|
| 82 | + $usedMemory = memory_get_peak_usage(); |
|
| 83 | + $allowedMemory = ini_get('memory_limit'); |
|
| 84 | + return array( |
|
| 85 | + 'used' => $usedMemory, |
|
| 86 | + 'allowed' => $allowedMemory |
|
| 87 | + ); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @param array $profiled_queries |
|
| 92 | + */ |
|
| 93 | + public function setProfiledQueries(array $profiledQueries) |
|
| 94 | + { |
|
| 95 | + $this->profiledQueries = $profiledQueries; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Get data about sql usage of the application |
|
| 100 | + * |
|
| 101 | + * @param object $dbConnection |
|
| 102 | + * @returns array |
|
| 103 | + */ |
|
| 104 | + public function gatherQueryData($dbConnection) |
|
| 105 | + { |
|
| 106 | + if (empty($this->profiledQueries) && property_exists($dbConnection, 'queries')) { |
|
| 107 | + $this->setProfiledQueries($dbConnection->queries); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + $data = array(); |
|
| 111 | + foreach ($this->profiledQueries as $query) { |
|
| 112 | + if ($query['function'] !== 'perform') { |
|
| 113 | + continue; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + array_push($data, array( |
|
| 117 | + 'sql' => $query['statement'], |
|
| 118 | + 'explain' => $this->explainQuery($query['statement'], $query['bind_values']), |
|
| 119 | + 'time' => $query['duration'] |
|
| 120 | + )); |
|
| 121 | + } |
|
| 122 | + return $data; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Attempts to explain a query |
|
| 127 | + * |
|
| 128 | + * @param string $query |
|
| 129 | + * @param array $parameters |
|
| 130 | + * @return array |
|
| 131 | + */ |
|
| 132 | + protected function explainQuery($query, $parameters) |
|
| 133 | + { |
|
| 134 | + $query = "EXPLAIN {$query}"; |
|
| 135 | + try { |
|
| 136 | + $statement = $this->dbConnection->prepare($query); |
|
| 137 | + $statement->execute($parameters); |
|
| 138 | + return $statement->fetch(\PDO::FETCH_ASSOC); |
|
| 139 | + } catch (\Exception $e) { |
|
| 140 | + echo $e->getMessage(); |
|
| 141 | + } |
|
| 142 | + return ''; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Get data about speed of the application |
|
| 147 | + * |
|
| 148 | + * @returns array |
|
| 149 | + */ |
|
| 150 | + public function gatherSpeedData() |
|
| 151 | + { |
|
| 152 | + $elapsedTime = microtime(true) - $this->startTime; |
|
| 153 | + $allowedTime = ini_get('max_execution_time'); |
|
| 154 | + return array( |
|
| 155 | + 'elapsed' => $elapsedTime, |
|
| 156 | + 'allowed' => $allowedTime |
|
| 157 | + ); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Triggers end display of the profiling data |
|
| 162 | + * |
|
| 163 | + * @param object $dbConnection |
|
| 164 | + */ |
|
| 165 | + public function display($dbConnection = null) |
|
| 166 | + { |
|
| 167 | + if (!isset($this->display)) { |
|
| 168 | + throw new Exception('Display object has not been injected into Profiler'); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + $this->display->setConsole($this->console); |
|
| 172 | + $this->display->setFileData($this->gatherFileData()); |
|
| 173 | + $this->display->setMemoryData($this->gatherMemoryData()); |
|
| 174 | + $this->display->setQueryData($this->gatherQueryData($dbConnection)); |
|
| 175 | + $this->display->setSpeedData($this->gatherSpeedData()); |
|
| 176 | + |
|
| 177 | + $this->display(); |
|
| 178 | + } |
|
| 179 | 179 | } |