@@ -17,194 +17,194 @@ |
||
17 | 17 | class PhpQuickProfiler |
18 | 18 | { |
19 | 19 | |
20 | - /** @var double */ |
|
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 double $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 $profiledQueries |
|
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 = null) |
|
107 | - { |
|
108 | - if (is_null($dbConnection)) { |
|
109 | - return array(); |
|
110 | - } |
|
111 | - |
|
112 | - if (empty($this->profiledQueries) && property_exists($dbConnection, 'queries')) { |
|
113 | - $this->setProfiledQueries($dbConnection->queries); |
|
114 | - } |
|
115 | - |
|
116 | - $data = array(); |
|
117 | - foreach ($this->profiledQueries as $query) { |
|
118 | - array_push($data, array( |
|
119 | - 'sql' => $query['sql'], |
|
120 | - 'explain' => $this->explainQuery($dbConnection, $query['sql'], $query['parameters']), |
|
121 | - 'time' => $query['time'] |
|
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 | - * @throws Exception |
|
134 | - * @return array |
|
135 | - */ |
|
136 | - protected function explainQuery($dbConnection, $query, $parameters = array()) |
|
137 | - { |
|
138 | - $driver = $dbConnection->getAttribute(\PDO::ATTR_DRIVER_NAME); |
|
139 | - $query = $this->getExplainQuery($query, $driver); |
|
140 | - $statement = $dbConnection->prepare($query); |
|
141 | - if ($statement === false) { |
|
142 | - throw new Exception('Invalid query passed to explainQuery method'); |
|
143 | - } |
|
144 | - $statement->execute($parameters); |
|
145 | - $result = $statement->fetch(\PDO::FETCH_ASSOC); |
|
146 | - if ($result === false) { |
|
147 | - throw new Exception('Query could not be explained with given parameters'); |
|
148 | - } |
|
149 | - return $result; |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Attempts to figure out what kind of explain query format the db wants |
|
154 | - * |
|
155 | - * @param string $query |
|
156 | - * @param string $driver |
|
157 | - * @throws Exception |
|
158 | - * @return string |
|
159 | - */ |
|
160 | - protected function getExplainQuery($query, $driver) |
|
161 | - { |
|
162 | - if ($driver == 'mysql') { |
|
163 | - return "EXPLAIN {$query}"; |
|
164 | - } elseif ($driver == 'sqlite') { |
|
165 | - return "EXPLAIN QUERY PLAN {$query}"; |
|
166 | - } |
|
167 | - throw new Exception('Could not process db driver'); |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * Get data about speed of the application |
|
172 | - * |
|
173 | - * @returns array |
|
174 | - */ |
|
175 | - public function gatherSpeedData() |
|
176 | - { |
|
177 | - $elapsedTime = microtime(true) - $this->startTime; |
|
178 | - $elapsedTime = round($elapsedTime, 3); |
|
179 | - $allowedTime = ini_get('max_execution_time'); |
|
180 | - return array( |
|
181 | - 'elapsed' => $elapsedTime, |
|
182 | - 'allowed' => $allowedTime |
|
183 | - ); |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * Triggers end display of the profiling data |
|
188 | - * |
|
189 | - * @param object $dbConnection |
|
190 | - * @throws Exception |
|
191 | - */ |
|
192 | - public function display($dbConnection = null) |
|
193 | - { |
|
194 | - if (!isset($this->display)) { |
|
195 | - throw new Exception('Display object has not been injected into Profiler'); |
|
196 | - } |
|
197 | - if (!isset($this->console)) { |
|
198 | - throw new Exception('Console object has not been injected into Profiler'); |
|
199 | - } |
|
200 | - |
|
201 | - $this->display->setStartTime($this->startTime); |
|
202 | - $this->display->setConsole($this->console); |
|
203 | - $this->display->setFileData($this->gatherFileData()); |
|
204 | - $this->display->setMemoryData($this->gatherMemoryData()); |
|
205 | - $this->display->setQueryData($this->gatherQueryData($dbConnection)); |
|
206 | - $this->display->setSpeedData($this->gatherSpeedData()); |
|
207 | - |
|
208 | - $this->display->__invoke(); |
|
209 | - } |
|
20 | + /** @var double */ |
|
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 double $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 $profiledQueries |
|
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 = null) |
|
107 | + { |
|
108 | + if (is_null($dbConnection)) { |
|
109 | + return array(); |
|
110 | + } |
|
111 | + |
|
112 | + if (empty($this->profiledQueries) && property_exists($dbConnection, 'queries')) { |
|
113 | + $this->setProfiledQueries($dbConnection->queries); |
|
114 | + } |
|
115 | + |
|
116 | + $data = array(); |
|
117 | + foreach ($this->profiledQueries as $query) { |
|
118 | + array_push($data, array( |
|
119 | + 'sql' => $query['sql'], |
|
120 | + 'explain' => $this->explainQuery($dbConnection, $query['sql'], $query['parameters']), |
|
121 | + 'time' => $query['time'] |
|
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 | + * @throws Exception |
|
134 | + * @return array |
|
135 | + */ |
|
136 | + protected function explainQuery($dbConnection, $query, $parameters = array()) |
|
137 | + { |
|
138 | + $driver = $dbConnection->getAttribute(\PDO::ATTR_DRIVER_NAME); |
|
139 | + $query = $this->getExplainQuery($query, $driver); |
|
140 | + $statement = $dbConnection->prepare($query); |
|
141 | + if ($statement === false) { |
|
142 | + throw new Exception('Invalid query passed to explainQuery method'); |
|
143 | + } |
|
144 | + $statement->execute($parameters); |
|
145 | + $result = $statement->fetch(\PDO::FETCH_ASSOC); |
|
146 | + if ($result === false) { |
|
147 | + throw new Exception('Query could not be explained with given parameters'); |
|
148 | + } |
|
149 | + return $result; |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Attempts to figure out what kind of explain query format the db wants |
|
154 | + * |
|
155 | + * @param string $query |
|
156 | + * @param string $driver |
|
157 | + * @throws Exception |
|
158 | + * @return string |
|
159 | + */ |
|
160 | + protected function getExplainQuery($query, $driver) |
|
161 | + { |
|
162 | + if ($driver == 'mysql') { |
|
163 | + return "EXPLAIN {$query}"; |
|
164 | + } elseif ($driver == 'sqlite') { |
|
165 | + return "EXPLAIN QUERY PLAN {$query}"; |
|
166 | + } |
|
167 | + throw new Exception('Could not process db driver'); |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * Get data about speed of the application |
|
172 | + * |
|
173 | + * @returns array |
|
174 | + */ |
|
175 | + public function gatherSpeedData() |
|
176 | + { |
|
177 | + $elapsedTime = microtime(true) - $this->startTime; |
|
178 | + $elapsedTime = round($elapsedTime, 3); |
|
179 | + $allowedTime = ini_get('max_execution_time'); |
|
180 | + return array( |
|
181 | + 'elapsed' => $elapsedTime, |
|
182 | + 'allowed' => $allowedTime |
|
183 | + ); |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * Triggers end display of the profiling data |
|
188 | + * |
|
189 | + * @param object $dbConnection |
|
190 | + * @throws Exception |
|
191 | + */ |
|
192 | + public function display($dbConnection = null) |
|
193 | + { |
|
194 | + if (!isset($this->display)) { |
|
195 | + throw new Exception('Display object has not been injected into Profiler'); |
|
196 | + } |
|
197 | + if (!isset($this->console)) { |
|
198 | + throw new Exception('Console object has not been injected into Profiler'); |
|
199 | + } |
|
200 | + |
|
201 | + $this->display->setStartTime($this->startTime); |
|
202 | + $this->display->setConsole($this->console); |
|
203 | + $this->display->setFileData($this->gatherFileData()); |
|
204 | + $this->display->setMemoryData($this->gatherMemoryData()); |
|
205 | + $this->display->setQueryData($this->gatherQueryData($dbConnection)); |
|
206 | + $this->display->setSpeedData($this->gatherSpeedData()); |
|
207 | + |
|
208 | + $this->display->__invoke(); |
|
209 | + } |
|
210 | 210 | } |
@@ -14,333 +14,333 @@ |
||
14 | 14 | class Display |
15 | 15 | { |
16 | 16 | |
17 | - /** @var array */ |
|
18 | - protected $defaults = array( |
|
19 | - 'script_path' => 'asset/script.js', |
|
20 | - 'style_path' => 'asset/style.css' |
|
21 | - ); |
|
22 | - |
|
23 | - /** @var array */ |
|
24 | - protected $options; |
|
25 | - |
|
26 | - /** @var double */ |
|
27 | - protected $startTime; |
|
28 | - |
|
29 | - /** @var Console */ |
|
30 | - protected $console; |
|
31 | - |
|
32 | - /** @var array */ |
|
33 | - protected $speedData; |
|
34 | - |
|
35 | - /** @var array */ |
|
36 | - protected $queryData; |
|
37 | - |
|
38 | - /** @var array */ |
|
39 | - protected $memoryData; |
|
40 | - |
|
41 | - /** @var array */ |
|
42 | - protected $fileData; |
|
43 | - |
|
44 | - /** |
|
45 | - * @param array $options |
|
46 | - */ |
|
47 | - public function __construct(array $options = array()) |
|
48 | - { |
|
49 | - $options = array_intersect_key($options, $this->defaults); |
|
50 | - $this->options = array_replace($this->defaults, $options); |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * @param double $startTime |
|
55 | - */ |
|
56 | - public function setStartTime($startTime) |
|
57 | - { |
|
58 | - $this->startTime = $startTime; |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * @param Console $console |
|
63 | - */ |
|
64 | - public function setConsole(Console $console) |
|
65 | - { |
|
66 | - $this->console = $console; |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * @return array |
|
71 | - */ |
|
72 | - protected function formatConsoleData() |
|
73 | - { |
|
74 | - $console_data = array( |
|
75 | - 'messages' => array(), |
|
76 | - 'meta' => array( |
|
77 | - 'log' => 0, |
|
78 | - 'memory' => 0, |
|
79 | - 'error' => 0, |
|
80 | - 'speed' => 0 |
|
81 | - ) |
|
82 | - ); |
|
83 | - foreach ($this->console->getLogs() as $log) { |
|
84 | - switch($log['type']) { |
|
85 | - case 'log': |
|
86 | - $message = array( |
|
87 | - 'message' => print_r($log['data'], true), |
|
88 | - 'type' => 'log' |
|
89 | - ); |
|
90 | - $console_data['meta']['log']++; |
|
91 | - break; |
|
92 | - case 'memory': |
|
93 | - $message = array( |
|
94 | - 'message' => (!empty($log['data_type']) ? "{$log['data_type']}: " : '') . $log['name'], |
|
95 | - 'data' => $this->getReadableMemory($log['data']), |
|
96 | - 'type' => 'memory' |
|
97 | - ); |
|
98 | - $console_data['meta']['memory']++; |
|
99 | - break; |
|
100 | - case 'error': |
|
101 | - $message = array( |
|
102 | - 'message' => "Line {$log['line']}: {$log['data']} in {$log['file']}", |
|
103 | - 'type' => 'error' |
|
104 | - ); |
|
105 | - $console_data['meta']['error']++; |
|
106 | - break; |
|
107 | - case 'speed': |
|
108 | - $elapsedTime = $log['data'] - $this->startTime; |
|
109 | - $message = array( |
|
110 | - 'message' => $log['name'], |
|
111 | - 'data' => $this->getReadableTime($elapsedTime), |
|
112 | - 'type' => 'speed' |
|
113 | - ); |
|
114 | - $console_data['meta']['speed']++; |
|
115 | - break; |
|
116 | - default: |
|
117 | - $message = array( |
|
118 | - 'message' => "Unrecognized console log type: {$log['type']}", |
|
119 | - 'type' => 'error' |
|
120 | - ); |
|
121 | - $console_data['meta']['error']++; |
|
122 | - break; |
|
123 | - } |
|
124 | - array_push($console_data['messages'], $message); |
|
125 | - } |
|
126 | - return $console_data; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Sets speed data |
|
131 | - * |
|
132 | - * @param array $data |
|
133 | - */ |
|
134 | - public function setSpeedData(array $data) |
|
135 | - { |
|
136 | - $this->speedData = $data; |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * @return array |
|
141 | - */ |
|
142 | - protected function getSpeedMeta() |
|
143 | - { |
|
144 | - $elapsedTime = $this->getReadableTime($this->speedData['elapsed']); |
|
145 | - $allowedTime = $this->getReadableTime($this->speedData['allowed'], 0); |
|
146 | - |
|
147 | - return array( |
|
148 | - 'elapsed' => $elapsedTime, |
|
149 | - 'allowed' => $allowedTime, |
|
150 | - ); |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Sets file data |
|
155 | - * |
|
156 | - * @param array $data |
|
157 | - */ |
|
158 | - public function setFileData(array $data) |
|
159 | - { |
|
160 | - $this->fileData = $data; |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * @return array |
|
165 | - */ |
|
166 | - protected function formatFileData() |
|
167 | - { |
|
168 | - $fileData = array( |
|
169 | - 'messages' => array(), |
|
170 | - 'meta' => array( |
|
171 | - 'count' => count($this->fileData), |
|
172 | - 'size' => 0, |
|
173 | - 'largest' => 0 |
|
174 | - ) |
|
175 | - ); |
|
176 | - |
|
177 | - foreach ($this->fileData as $file) { |
|
178 | - array_push($fileData['messages'], array( |
|
179 | - 'message' => $file['name'], |
|
180 | - 'data' => $this->getReadableMemory($file['size']) |
|
181 | - )); |
|
182 | - |
|
183 | - $fileData['meta']['size'] += $file['size']; |
|
184 | - if ($file['size'] > $fileData['meta']['largest']) { |
|
185 | - $fileData['meta']['largest'] = $file['size']; |
|
186 | - } |
|
187 | - } |
|
188 | - |
|
189 | - $fileData['meta']['size'] = $this->getReadableMemory($fileData['meta']['size']); |
|
190 | - $fileData['meta']['largest'] = $this->getReadableMemory($fileData['meta']['largest']); |
|
191 | - |
|
192 | - return $fileData; |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * Sets memory data |
|
197 | - * |
|
198 | - * @param array $data |
|
199 | - */ |
|
200 | - public function setMemoryData(array $data) |
|
201 | - { |
|
202 | - $this->memoryData = $data; |
|
203 | - } |
|
204 | - |
|
205 | - /** |
|
206 | - * @return array |
|
207 | - */ |
|
208 | - public function getMemoryMeta() |
|
209 | - { |
|
210 | - $usedMemory = $this->getReadableMemory($this->memoryData['used']); |
|
211 | - $allowedMemory = $this->memoryData['allowed']; // todo parse this, maybe? |
|
212 | - |
|
213 | - return array( |
|
214 | - 'used' => $usedMemory, |
|
215 | - 'allowed' => $allowedMemory |
|
216 | - ); |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * Sets query data |
|
221 | - * |
|
222 | - * @param array $data |
|
223 | - */ |
|
224 | - public function setQueryData(array $data) |
|
225 | - { |
|
226 | - $this->queryData = $data; |
|
227 | - } |
|
228 | - |
|
229 | - /** |
|
230 | - * @return array |
|
231 | - */ |
|
232 | - public function formatQueryData() |
|
233 | - { |
|
234 | - $queryData = array( |
|
235 | - 'messages' => array(), |
|
236 | - 'meta' => array( |
|
237 | - 'count' => count($this->queryData), |
|
238 | - 'time' => 0, |
|
239 | - 'slowest' => 0 |
|
240 | - ) |
|
241 | - ); |
|
242 | - |
|
243 | - foreach ($this->queryData as $query) { |
|
244 | - array_push($queryData['messages'], array( |
|
245 | - 'message' => $query['sql'], |
|
246 | - 'sub_data' => array_filter($query['explain']), |
|
247 | - 'data' => $this->getReadableTime($query['time']) |
|
248 | - )); |
|
249 | - $queryData['meta']['time'] += $query['time']; |
|
250 | - if ($query['time'] > $queryData['meta']['slowest']) { |
|
251 | - $queryData['meta']['slowest'] = $query['time']; |
|
252 | - } |
|
253 | - } |
|
254 | - |
|
255 | - $queryData['meta']['time'] = $this->getReadableTime($queryData['meta']['time']); |
|
256 | - $queryData['meta']['slowest'] = $this->getReadableTime($queryData['meta']['slowest']); |
|
257 | - |
|
258 | - return $queryData; |
|
259 | - } |
|
260 | - |
|
261 | - |
|
262 | - /** |
|
263 | - * Formatter for human-readable time |
|
264 | - * Only handles time up to 60 minutes gracefully |
|
265 | - * |
|
266 | - * @param double $time |
|
267 | - * @param integer $percision |
|
268 | - * @return string |
|
269 | - */ |
|
270 | - protected function getReadableTime($time, $percision = 3) |
|
271 | - { |
|
272 | - $unit = 's'; |
|
273 | - if ($time < 1) { |
|
274 | - $time *= 1000; |
|
275 | - $unit = 'ms'; |
|
276 | - } else if ($time > 60) { |
|
277 | - $time /= 60; |
|
278 | - $unit = 'm'; |
|
279 | - } |
|
280 | - $time = number_format($time, $percision); |
|
281 | - return "{$time} {$unit}"; |
|
282 | - } |
|
283 | - |
|
284 | - /** |
|
285 | - * Formatter for human-readable memory |
|
286 | - * Only handles time up to a few gigs gracefully |
|
287 | - * |
|
288 | - * @param double $size |
|
289 | - * @param integer $percision |
|
290 | - */ |
|
291 | - protected function getReadableMemory($size, $percision = 2) |
|
292 | - { |
|
293 | - $unitOptions = array('b', 'k', 'M', 'G'); |
|
294 | - |
|
295 | - $base = log($size, 1024); |
|
296 | - |
|
297 | - $memory = round(pow(1024, $base - floor($base)), $percision); |
|
298 | - $unit = $unitOptions[floor($base)]; |
|
299 | - return "{$memory} {$unit}"; |
|
300 | - } |
|
301 | - |
|
302 | - /** |
|
303 | - * @param array $messages |
|
304 | - * @param string $type |
|
305 | - * @return array |
|
306 | - */ |
|
307 | - protected function filterMessages($messages, $type) |
|
308 | - { |
|
309 | - return array_filter($messages, function ($message) use ($type) { |
|
310 | - return $message['type'] == $type; |
|
311 | - }); |
|
312 | - } |
|
17 | + /** @var array */ |
|
18 | + protected $defaults = array( |
|
19 | + 'script_path' => 'asset/script.js', |
|
20 | + 'style_path' => 'asset/style.css' |
|
21 | + ); |
|
22 | + |
|
23 | + /** @var array */ |
|
24 | + protected $options; |
|
25 | + |
|
26 | + /** @var double */ |
|
27 | + protected $startTime; |
|
28 | + |
|
29 | + /** @var Console */ |
|
30 | + protected $console; |
|
31 | + |
|
32 | + /** @var array */ |
|
33 | + protected $speedData; |
|
34 | + |
|
35 | + /** @var array */ |
|
36 | + protected $queryData; |
|
37 | + |
|
38 | + /** @var array */ |
|
39 | + protected $memoryData; |
|
40 | + |
|
41 | + /** @var array */ |
|
42 | + protected $fileData; |
|
43 | + |
|
44 | + /** |
|
45 | + * @param array $options |
|
46 | + */ |
|
47 | + public function __construct(array $options = array()) |
|
48 | + { |
|
49 | + $options = array_intersect_key($options, $this->defaults); |
|
50 | + $this->options = array_replace($this->defaults, $options); |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * @param double $startTime |
|
55 | + */ |
|
56 | + public function setStartTime($startTime) |
|
57 | + { |
|
58 | + $this->startTime = $startTime; |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * @param Console $console |
|
63 | + */ |
|
64 | + public function setConsole(Console $console) |
|
65 | + { |
|
66 | + $this->console = $console; |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * @return array |
|
71 | + */ |
|
72 | + protected function formatConsoleData() |
|
73 | + { |
|
74 | + $console_data = array( |
|
75 | + 'messages' => array(), |
|
76 | + 'meta' => array( |
|
77 | + 'log' => 0, |
|
78 | + 'memory' => 0, |
|
79 | + 'error' => 0, |
|
80 | + 'speed' => 0 |
|
81 | + ) |
|
82 | + ); |
|
83 | + foreach ($this->console->getLogs() as $log) { |
|
84 | + switch($log['type']) { |
|
85 | + case 'log': |
|
86 | + $message = array( |
|
87 | + 'message' => print_r($log['data'], true), |
|
88 | + 'type' => 'log' |
|
89 | + ); |
|
90 | + $console_data['meta']['log']++; |
|
91 | + break; |
|
92 | + case 'memory': |
|
93 | + $message = array( |
|
94 | + 'message' => (!empty($log['data_type']) ? "{$log['data_type']}: " : '') . $log['name'], |
|
95 | + 'data' => $this->getReadableMemory($log['data']), |
|
96 | + 'type' => 'memory' |
|
97 | + ); |
|
98 | + $console_data['meta']['memory']++; |
|
99 | + break; |
|
100 | + case 'error': |
|
101 | + $message = array( |
|
102 | + 'message' => "Line {$log['line']}: {$log['data']} in {$log['file']}", |
|
103 | + 'type' => 'error' |
|
104 | + ); |
|
105 | + $console_data['meta']['error']++; |
|
106 | + break; |
|
107 | + case 'speed': |
|
108 | + $elapsedTime = $log['data'] - $this->startTime; |
|
109 | + $message = array( |
|
110 | + 'message' => $log['name'], |
|
111 | + 'data' => $this->getReadableTime($elapsedTime), |
|
112 | + 'type' => 'speed' |
|
113 | + ); |
|
114 | + $console_data['meta']['speed']++; |
|
115 | + break; |
|
116 | + default: |
|
117 | + $message = array( |
|
118 | + 'message' => "Unrecognized console log type: {$log['type']}", |
|
119 | + 'type' => 'error' |
|
120 | + ); |
|
121 | + $console_data['meta']['error']++; |
|
122 | + break; |
|
123 | + } |
|
124 | + array_push($console_data['messages'], $message); |
|
125 | + } |
|
126 | + return $console_data; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Sets speed data |
|
131 | + * |
|
132 | + * @param array $data |
|
133 | + */ |
|
134 | + public function setSpeedData(array $data) |
|
135 | + { |
|
136 | + $this->speedData = $data; |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * @return array |
|
141 | + */ |
|
142 | + protected function getSpeedMeta() |
|
143 | + { |
|
144 | + $elapsedTime = $this->getReadableTime($this->speedData['elapsed']); |
|
145 | + $allowedTime = $this->getReadableTime($this->speedData['allowed'], 0); |
|
146 | + |
|
147 | + return array( |
|
148 | + 'elapsed' => $elapsedTime, |
|
149 | + 'allowed' => $allowedTime, |
|
150 | + ); |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Sets file data |
|
155 | + * |
|
156 | + * @param array $data |
|
157 | + */ |
|
158 | + public function setFileData(array $data) |
|
159 | + { |
|
160 | + $this->fileData = $data; |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * @return array |
|
165 | + */ |
|
166 | + protected function formatFileData() |
|
167 | + { |
|
168 | + $fileData = array( |
|
169 | + 'messages' => array(), |
|
170 | + 'meta' => array( |
|
171 | + 'count' => count($this->fileData), |
|
172 | + 'size' => 0, |
|
173 | + 'largest' => 0 |
|
174 | + ) |
|
175 | + ); |
|
176 | + |
|
177 | + foreach ($this->fileData as $file) { |
|
178 | + array_push($fileData['messages'], array( |
|
179 | + 'message' => $file['name'], |
|
180 | + 'data' => $this->getReadableMemory($file['size']) |
|
181 | + )); |
|
182 | + |
|
183 | + $fileData['meta']['size'] += $file['size']; |
|
184 | + if ($file['size'] > $fileData['meta']['largest']) { |
|
185 | + $fileData['meta']['largest'] = $file['size']; |
|
186 | + } |
|
187 | + } |
|
188 | + |
|
189 | + $fileData['meta']['size'] = $this->getReadableMemory($fileData['meta']['size']); |
|
190 | + $fileData['meta']['largest'] = $this->getReadableMemory($fileData['meta']['largest']); |
|
191 | + |
|
192 | + return $fileData; |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * Sets memory data |
|
197 | + * |
|
198 | + * @param array $data |
|
199 | + */ |
|
200 | + public function setMemoryData(array $data) |
|
201 | + { |
|
202 | + $this->memoryData = $data; |
|
203 | + } |
|
204 | + |
|
205 | + /** |
|
206 | + * @return array |
|
207 | + */ |
|
208 | + public function getMemoryMeta() |
|
209 | + { |
|
210 | + $usedMemory = $this->getReadableMemory($this->memoryData['used']); |
|
211 | + $allowedMemory = $this->memoryData['allowed']; // todo parse this, maybe? |
|
212 | + |
|
213 | + return array( |
|
214 | + 'used' => $usedMemory, |
|
215 | + 'allowed' => $allowedMemory |
|
216 | + ); |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * Sets query data |
|
221 | + * |
|
222 | + * @param array $data |
|
223 | + */ |
|
224 | + public function setQueryData(array $data) |
|
225 | + { |
|
226 | + $this->queryData = $data; |
|
227 | + } |
|
228 | + |
|
229 | + /** |
|
230 | + * @return array |
|
231 | + */ |
|
232 | + public function formatQueryData() |
|
233 | + { |
|
234 | + $queryData = array( |
|
235 | + 'messages' => array(), |
|
236 | + 'meta' => array( |
|
237 | + 'count' => count($this->queryData), |
|
238 | + 'time' => 0, |
|
239 | + 'slowest' => 0 |
|
240 | + ) |
|
241 | + ); |
|
242 | + |
|
243 | + foreach ($this->queryData as $query) { |
|
244 | + array_push($queryData['messages'], array( |
|
245 | + 'message' => $query['sql'], |
|
246 | + 'sub_data' => array_filter($query['explain']), |
|
247 | + 'data' => $this->getReadableTime($query['time']) |
|
248 | + )); |
|
249 | + $queryData['meta']['time'] += $query['time']; |
|
250 | + if ($query['time'] > $queryData['meta']['slowest']) { |
|
251 | + $queryData['meta']['slowest'] = $query['time']; |
|
252 | + } |
|
253 | + } |
|
254 | + |
|
255 | + $queryData['meta']['time'] = $this->getReadableTime($queryData['meta']['time']); |
|
256 | + $queryData['meta']['slowest'] = $this->getReadableTime($queryData['meta']['slowest']); |
|
257 | + |
|
258 | + return $queryData; |
|
259 | + } |
|
260 | + |
|
261 | + |
|
262 | + /** |
|
263 | + * Formatter for human-readable time |
|
264 | + * Only handles time up to 60 minutes gracefully |
|
265 | + * |
|
266 | + * @param double $time |
|
267 | + * @param integer $percision |
|
268 | + * @return string |
|
269 | + */ |
|
270 | + protected function getReadableTime($time, $percision = 3) |
|
271 | + { |
|
272 | + $unit = 's'; |
|
273 | + if ($time < 1) { |
|
274 | + $time *= 1000; |
|
275 | + $unit = 'ms'; |
|
276 | + } else if ($time > 60) { |
|
277 | + $time /= 60; |
|
278 | + $unit = 'm'; |
|
279 | + } |
|
280 | + $time = number_format($time, $percision); |
|
281 | + return "{$time} {$unit}"; |
|
282 | + } |
|
283 | + |
|
284 | + /** |
|
285 | + * Formatter for human-readable memory |
|
286 | + * Only handles time up to a few gigs gracefully |
|
287 | + * |
|
288 | + * @param double $size |
|
289 | + * @param integer $percision |
|
290 | + */ |
|
291 | + protected function getReadableMemory($size, $percision = 2) |
|
292 | + { |
|
293 | + $unitOptions = array('b', 'k', 'M', 'G'); |
|
294 | + |
|
295 | + $base = log($size, 1024); |
|
296 | + |
|
297 | + $memory = round(pow(1024, $base - floor($base)), $percision); |
|
298 | + $unit = $unitOptions[floor($base)]; |
|
299 | + return "{$memory} {$unit}"; |
|
300 | + } |
|
301 | + |
|
302 | + /** |
|
303 | + * @param array $messages |
|
304 | + * @param string $type |
|
305 | + * @return array |
|
306 | + */ |
|
307 | + protected function filterMessages($messages, $type) |
|
308 | + { |
|
309 | + return array_filter($messages, function ($message) use ($type) { |
|
310 | + return $message['type'] == $type; |
|
311 | + }); |
|
312 | + } |
|
313 | 313 | |
314 | - public function __invoke() |
|
315 | - { |
|
316 | - $console = $this->formatConsoleData(); |
|
317 | - $speedMeta = $this->getSpeedMeta(); |
|
318 | - $query= $this->formatQueryData(); |
|
319 | - $memoryMeta = $this->getMemoryMeta(); |
|
320 | - $files= $this->formatFileData(); |
|
321 | - |
|
322 | - $header = array( |
|
323 | - 'console' => count($console['messages']), |
|
324 | - 'speed' => $speedMeta['elapsed'], |
|
325 | - 'query' => $query['meta']['count'], |
|
326 | - 'memory' => $memoryMeta['used'], |
|
327 | - 'files' => $files['meta']['count'] |
|
328 | - ); |
|
329 | - |
|
330 | - $speed = array( |
|
331 | - 'meta' => $speedMeta, |
|
332 | - 'messages' => $this->filterMessages($console['messages'], 'speed') |
|
333 | - ); |
|
334 | - |
|
335 | - $memory = array( |
|
336 | - 'meta' => $memoryMeta, |
|
337 | - 'messages' => $this->filterMessages($console['messages'], 'memory') |
|
338 | - ); |
|
339 | - |
|
340 | - // todo is this really the best way to load these? |
|
341 | - $styles = file_get_contents(__DIR__ . "/../{$this->options['style_path']}"); |
|
342 | - $script = file_get_contents(__DIR__ . "/../{$this->options['script_path']}"); |
|
343 | - |
|
344 | - require_once __DIR__ .'/../asset/display.html'; |
|
345 | - } |
|
314 | + public function __invoke() |
|
315 | + { |
|
316 | + $console = $this->formatConsoleData(); |
|
317 | + $speedMeta = $this->getSpeedMeta(); |
|
318 | + $query= $this->formatQueryData(); |
|
319 | + $memoryMeta = $this->getMemoryMeta(); |
|
320 | + $files= $this->formatFileData(); |
|
321 | + |
|
322 | + $header = array( |
|
323 | + 'console' => count($console['messages']), |
|
324 | + 'speed' => $speedMeta['elapsed'], |
|
325 | + 'query' => $query['meta']['count'], |
|
326 | + 'memory' => $memoryMeta['used'], |
|
327 | + 'files' => $files['meta']['count'] |
|
328 | + ); |
|
329 | + |
|
330 | + $speed = array( |
|
331 | + 'meta' => $speedMeta, |
|
332 | + 'messages' => $this->filterMessages($console['messages'], 'speed') |
|
333 | + ); |
|
334 | + |
|
335 | + $memory = array( |
|
336 | + 'meta' => $memoryMeta, |
|
337 | + 'messages' => $this->filterMessages($console['messages'], 'memory') |
|
338 | + ); |
|
339 | + |
|
340 | + // todo is this really the best way to load these? |
|
341 | + $styles = file_get_contents(__DIR__ . "/../{$this->options['style_path']}"); |
|
342 | + $script = file_get_contents(__DIR__ . "/../{$this->options['script_path']}"); |
|
343 | + |
|
344 | + require_once __DIR__ .'/../asset/display.html'; |
|
345 | + } |
|
346 | 346 | } |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | ) |
82 | 82 | ); |
83 | 83 | foreach ($this->console->getLogs() as $log) { |
84 | - switch($log['type']) { |
|
84 | + switch ($log['type']) { |
|
85 | 85 | case 'log': |
86 | 86 | $message = array( |
87 | 87 | 'message' => print_r($log['data'], true), |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | break; |
92 | 92 | case 'memory': |
93 | 93 | $message = array( |
94 | - 'message' => (!empty($log['data_type']) ? "{$log['data_type']}: " : '') . $log['name'], |
|
94 | + 'message' => (!empty($log['data_type']) ? "{$log['data_type']}: " : '').$log['name'], |
|
95 | 95 | 'data' => $this->getReadableMemory($log['data']), |
96 | 96 | 'type' => 'memory' |
97 | 97 | ); |
@@ -306,7 +306,7 @@ discard block |
||
306 | 306 | */ |
307 | 307 | protected function filterMessages($messages, $type) |
308 | 308 | { |
309 | - return array_filter($messages, function ($message) use ($type) { |
|
309 | + return array_filter($messages, function($message) use ($type) { |
|
310 | 310 | return $message['type'] == $type; |
311 | 311 | }); |
312 | 312 | } |
@@ -315,9 +315,9 @@ discard block |
||
315 | 315 | { |
316 | 316 | $console = $this->formatConsoleData(); |
317 | 317 | $speedMeta = $this->getSpeedMeta(); |
318 | - $query= $this->formatQueryData(); |
|
318 | + $query = $this->formatQueryData(); |
|
319 | 319 | $memoryMeta = $this->getMemoryMeta(); |
320 | - $files= $this->formatFileData(); |
|
320 | + $files = $this->formatFileData(); |
|
321 | 321 | |
322 | 322 | $header = array( |
323 | 323 | 'console' => count($console['messages']), |
@@ -338,9 +338,9 @@ discard block |
||
338 | 338 | ); |
339 | 339 | |
340 | 340 | // todo is this really the best way to load these? |
341 | - $styles = file_get_contents(__DIR__ . "/../{$this->options['style_path']}"); |
|
342 | - $script = file_get_contents(__DIR__ . "/../{$this->options['script_path']}"); |
|
341 | + $styles = file_get_contents(__DIR__."/../{$this->options['style_path']}"); |
|
342 | + $script = file_get_contents(__DIR__."/../{$this->options['script_path']}"); |
|
343 | 343 | |
344 | - require_once __DIR__ .'/../asset/display.html'; |
|
344 | + require_once __DIR__.'/../asset/display.html'; |
|
345 | 345 | } |
346 | 346 | } |