@@ -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 | { |
@@ -15,168 +15,168 @@ |
||
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 $db |
|
102 | - * @returns array |
|
103 | - */ |
|
104 | - public function gatherQueryData($db) |
|
105 | - { |
|
106 | - if ( |
|
107 | - empty($this->profiledQueries) && |
|
108 | - property_exists($db, 'queries') |
|
109 | - ) { |
|
110 | - $this->setProfiledQueries($db->queries); |
|
111 | - } |
|
112 | - |
|
113 | - $data = array(); |
|
114 | - foreach ($this->profiledQueries as $query) { |
|
115 | - if ($query['function'] !== 'perform') { |
|
116 | - continue; |
|
117 | - } |
|
118 | - |
|
119 | - array_push($data, array( |
|
120 | - 'sql' => $query['statement'], |
|
121 | - 'explain' => $this->explainQuery($query['statement'], $query['bind_values']), |
|
122 | - 'time' => $query['duration'] |
|
123 | - )); |
|
124 | - } |
|
125 | - return $data; |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * Attempts to explain a query |
|
130 | - * |
|
131 | - * @param string $query |
|
132 | - * @param array $parameters |
|
133 | - * @return array |
|
134 | - */ |
|
135 | - protected function explainQuery($query, $parameters) |
|
136 | - { |
|
137 | - $query = "EXPLAIN {$query}"; |
|
138 | - try { |
|
139 | - $statement = $this->db->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 $db |
|
167 | - */ |
|
168 | - public function display($db = 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($db)); |
|
178 | - $this->display->setSpeedData($this->gatherSpeedData()); |
|
179 | - |
|
180 | - $display(); |
|
181 | - } |
|
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 $db |
|
102 | + * @returns array |
|
103 | + */ |
|
104 | + public function gatherQueryData($db) |
|
105 | + { |
|
106 | + if ( |
|
107 | + empty($this->profiledQueries) && |
|
108 | + property_exists($db, 'queries') |
|
109 | + ) { |
|
110 | + $this->setProfiledQueries($db->queries); |
|
111 | + } |
|
112 | + |
|
113 | + $data = array(); |
|
114 | + foreach ($this->profiledQueries as $query) { |
|
115 | + if ($query['function'] !== 'perform') { |
|
116 | + continue; |
|
117 | + } |
|
118 | + |
|
119 | + array_push($data, array( |
|
120 | + 'sql' => $query['statement'], |
|
121 | + 'explain' => $this->explainQuery($query['statement'], $query['bind_values']), |
|
122 | + 'time' => $query['duration'] |
|
123 | + )); |
|
124 | + } |
|
125 | + return $data; |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * Attempts to explain a query |
|
130 | + * |
|
131 | + * @param string $query |
|
132 | + * @param array $parameters |
|
133 | + * @return array |
|
134 | + */ |
|
135 | + protected function explainQuery($query, $parameters) |
|
136 | + { |
|
137 | + $query = "EXPLAIN {$query}"; |
|
138 | + try { |
|
139 | + $statement = $this->db->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 $db |
|
167 | + */ |
|
168 | + public function display($db = 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($db)); |
|
178 | + $this->display->setSpeedData($this->gatherSpeedData()); |
|
179 | + |
|
180 | + $display(); |
|
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,41 +7,41 @@ |
||
7 | 7 | class PhpQuickProfilerTest extends PHPUnit_Framework_TestCase |
8 | 8 | { |
9 | 9 | |
10 | - public function testConstruct() |
|
11 | - { |
|
12 | - $startTime = microtime(true); |
|
13 | - $profiler = new PhpQuickProfiler($startTime); |
|
14 | - |
|
15 | - $this->assertAttributeEquals($startTime, 'startTime', $profiler); |
|
16 | - } |
|
17 | - |
|
18 | - public function testSetConsole() |
|
19 | - { |
|
20 | - $console = new Console(); |
|
21 | - $profiler = new PhpQuickProfiler(); |
|
22 | - $profiler->setConsole($console); |
|
23 | - |
|
24 | - $this->assertAttributeSame($console, 'console', $profiler); |
|
25 | - } |
|
26 | - |
|
27 | - public function testSetDisplay() |
|
28 | - { |
|
29 | - $display = new Display(); |
|
30 | - $profiler = new PhpQuickProfiler(); |
|
31 | - $profiler->setDisplay($display); |
|
32 | - |
|
33 | - $this->assertAttributeSame($display, 'display', $profiler); |
|
34 | - } |
|
35 | - |
|
36 | - public function testSetProfiledQueries() |
|
37 | - { |
|
38 | - $profiledQueries = array( |
|
39 | - 'sql' => 'SELECT * FROM example', |
|
40 | - 'time' => 25 |
|
41 | - ); |
|
42 | - $profiler = new PhpQuickProfiler(); |
|
43 | - $profiler->setProfiledQueries($profiledQueries); |
|
44 | - |
|
45 | - $this->assertAttributeEquals($profiledQueries, 'profiledQueries', $profiler); |
|
46 | - } |
|
10 | + public function testConstruct() |
|
11 | + { |
|
12 | + $startTime = microtime(true); |
|
13 | + $profiler = new PhpQuickProfiler($startTime); |
|
14 | + |
|
15 | + $this->assertAttributeEquals($startTime, 'startTime', $profiler); |
|
16 | + } |
|
17 | + |
|
18 | + public function testSetConsole() |
|
19 | + { |
|
20 | + $console = new Console(); |
|
21 | + $profiler = new PhpQuickProfiler(); |
|
22 | + $profiler->setConsole($console); |
|
23 | + |
|
24 | + $this->assertAttributeSame($console, 'console', $profiler); |
|
25 | + } |
|
26 | + |
|
27 | + public function testSetDisplay() |
|
28 | + { |
|
29 | + $display = new Display(); |
|
30 | + $profiler = new PhpQuickProfiler(); |
|
31 | + $profiler->setDisplay($display); |
|
32 | + |
|
33 | + $this->assertAttributeSame($display, 'display', $profiler); |
|
34 | + } |
|
35 | + |
|
36 | + public function testSetProfiledQueries() |
|
37 | + { |
|
38 | + $profiledQueries = array( |
|
39 | + 'sql' => 'SELECT * FROM example', |
|
40 | + 'time' => 25 |
|
41 | + ); |
|
42 | + $profiler = new PhpQuickProfiler(); |
|
43 | + $profiler->setProfiledQueries($profiledQueries); |
|
44 | + |
|
45 | + $this->assertAttributeEquals($profiledQueries, 'profiledQueries', $profiler); |
|
46 | + } |
|
47 | 47 | } |