@@ -17,167 +17,167 @@ |
||
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 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 $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 | - $elapsedTime = round($elapsedTime, 3); |
|
157 | - $allowedTime = ini_get('max_execution_time'); |
|
158 | - return array( |
|
159 | - 'elapsed' => $elapsedTime, |
|
160 | - 'allowed' => $allowedTime |
|
161 | - ); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * Triggers end display of the profiling data |
|
166 | - * |
|
167 | - * @param object $dbConnection |
|
168 | - */ |
|
169 | - public function display($dbConnection = null) |
|
170 | - { |
|
171 | - if (!isset($this->display)) { |
|
172 | - throw new Exception('Display object has not been injected into Profiler'); |
|
173 | - } |
|
174 | - |
|
175 | - $this->display->setConsole($this->console); |
|
176 | - $this->display->setFileData($this->gatherFileData()); |
|
177 | - $this->display->setMemoryData($this->gatherMemoryData()); |
|
178 | - $this->display->setQueryData($this->gatherQueryData($dbConnection)); |
|
179 | - $this->display->setSpeedData($this->gatherSpeedData()); |
|
180 | - |
|
181 | - $this->display->__invoke(); |
|
182 | - } |
|
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 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 $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 | + $elapsedTime = round($elapsedTime, 3); |
|
157 | + $allowedTime = ini_get('max_execution_time'); |
|
158 | + return array( |
|
159 | + 'elapsed' => $elapsedTime, |
|
160 | + 'allowed' => $allowedTime |
|
161 | + ); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * Triggers end display of the profiling data |
|
166 | + * |
|
167 | + * @param object $dbConnection |
|
168 | + */ |
|
169 | + public function display($dbConnection = null) |
|
170 | + { |
|
171 | + if (!isset($this->display)) { |
|
172 | + throw new Exception('Display object has not been injected into Profiler'); |
|
173 | + } |
|
174 | + |
|
175 | + $this->display->setConsole($this->console); |
|
176 | + $this->display->setFileData($this->gatherFileData()); |
|
177 | + $this->display->setMemoryData($this->gatherMemoryData()); |
|
178 | + $this->display->setQueryData($this->gatherQueryData($dbConnection)); |
|
179 | + $this->display->setSpeedData($this->gatherSpeedData()); |
|
180 | + |
|
181 | + $this->display->__invoke(); |
|
182 | + } |
|
183 | 183 | } |
@@ -7,130 +7,130 @@ |
||
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 | - } elseif ($setting == 'max_execution_time') { |
|
40 | - return '30'; |
|
41 | - } |
|
42 | - return \ini_get($setting); |
|
37 | + if ($setting == 'memory_limit') { |
|
38 | + return '128M'; |
|
39 | + } elseif ($setting == 'max_execution_time') { |
|
40 | + return '30'; |
|
41 | + } |
|
42 | + return \ini_get($setting); |
|
43 | 43 | } |
44 | 44 | |
45 | 45 | class PhpQuickProfilerTest extends PHPUnit_Framework_TestCase |
46 | 46 | { |
47 | 47 | |
48 | - public function testConstruct() |
|
49 | - { |
|
50 | - $startTime = microtime(true); |
|
51 | - |
|
52 | - $profiler = new PhpQuickProfiler(); |
|
53 | - $this->assertAttributeEquals($startTime, 'startTime', $profiler); |
|
54 | - |
|
55 | - $profiler = new PhpQuickProfiler($startTime); |
|
56 | - $this->assertAttributeEquals($startTime, 'startTime', $profiler); |
|
57 | - } |
|
58 | - |
|
59 | - public function testSetConsole() |
|
60 | - { |
|
61 | - $console = new Console(); |
|
62 | - $profiler = new PhpQuickProfiler(); |
|
63 | - $profiler->setConsole($console); |
|
64 | - |
|
65 | - $this->assertAttributeSame($console, 'console', $profiler); |
|
66 | - } |
|
67 | - |
|
68 | - public function testSetDisplay() |
|
69 | - { |
|
70 | - $display = new Display(); |
|
71 | - $profiler = new PhpQuickProfiler(); |
|
72 | - $profiler->setDisplay($display); |
|
73 | - |
|
74 | - $this->assertAttributeSame($display, 'display', $profiler); |
|
75 | - } |
|
76 | - |
|
77 | - public function testGatherFileData() |
|
78 | - { |
|
79 | - $files = get_included_files(); |
|
80 | - $profiler = new PhpQuickProfiler(); |
|
81 | - $gatheredFileData = $profiler->gatherFileData(); |
|
82 | - |
|
83 | - $this->assertInternalType('array', $gatheredFileData); |
|
84 | - $this->assertEquals(count($files), count($gatheredFileData)); |
|
85 | - foreach ($gatheredFileData as $fileData) { |
|
86 | - $this->assertInternalType('array', $fileData); |
|
87 | - $this->assertArrayHasKey('name', $fileData); |
|
88 | - $this->assertContains($fileData['name'], $files); |
|
89 | - $this->assertArrayHasKey('size', $fileData); |
|
90 | - $this->assertEquals($fileData['size'], filesize($fileData['name'])); |
|
91 | - } |
|
92 | - } |
|
93 | - |
|
94 | - public function testGatherMemoryData() |
|
95 | - { |
|
96 | - $memoryUsage = memory_get_peak_usage(); |
|
97 | - $allowedLimit = ini_get('memory_limit'); |
|
98 | - $profiler = new PhpQuickProfiler(); |
|
99 | - $gatheredMemoryData = $profiler->gatherMemoryData(); |
|
100 | - |
|
101 | - $this->assertInternalType('array', $gatheredMemoryData); |
|
102 | - $this->assertEquals(2, count($gatheredMemoryData)); |
|
103 | - $this->assertArrayHasKey('used', $gatheredMemoryData); |
|
104 | - $this->assertEquals($memoryUsage, $gatheredMemoryData['used']); |
|
105 | - $this->assertArrayHasKey('allowed', $gatheredMemoryData); |
|
106 | - $this->assertEquals($allowedLimit, $gatheredMemoryData['allowed']); |
|
107 | - } |
|
108 | - |
|
109 | - public function testSetProfiledQueries() |
|
110 | - { |
|
111 | - $profiledQueries = array( |
|
112 | - 'sql' => 'SELECT * FROM example', |
|
113 | - 'time' => 25 |
|
114 | - ); |
|
115 | - $profiler = new PhpQuickProfiler(); |
|
116 | - $profiler->setProfiledQueries($profiledQueries); |
|
117 | - |
|
118 | - $this->assertAttributeEquals($profiledQueries, 'profiledQueries', $profiler); |
|
119 | - } |
|
120 | - |
|
121 | - public function testGatherSpeedData() |
|
122 | - { |
|
123 | - $elapsedTime = 1.234; |
|
124 | - $startTime = microtime(true) - $elapsedTime; |
|
125 | - $allowedTime = ini_get('max_execution_time'); |
|
126 | - $profiler = new PhpQuickProfiler($startTime); |
|
127 | - $gatheredSpeedData = $profiler->gatherSpeedData(); |
|
128 | - |
|
129 | - $this->assertInternalType('array', $gatheredSpeedData); |
|
130 | - $this->assertEquals(2, count($gatheredSpeedData)); |
|
131 | - $this->assertArrayHasKey('elapsed', $gatheredSpeedData); |
|
132 | - $this->assertEquals($elapsedTime, $gatheredSpeedData['elapsed']); |
|
133 | - $this->assertArrayHasKey('allowed', $gatheredSpeedData); |
|
134 | - $this->assertEquals($allowedTime, $gatheredSpeedData['allowed']); |
|
135 | - } |
|
48 | + public function testConstruct() |
|
49 | + { |
|
50 | + $startTime = microtime(true); |
|
51 | + |
|
52 | + $profiler = new PhpQuickProfiler(); |
|
53 | + $this->assertAttributeEquals($startTime, 'startTime', $profiler); |
|
54 | + |
|
55 | + $profiler = new PhpQuickProfiler($startTime); |
|
56 | + $this->assertAttributeEquals($startTime, 'startTime', $profiler); |
|
57 | + } |
|
58 | + |
|
59 | + public function testSetConsole() |
|
60 | + { |
|
61 | + $console = new Console(); |
|
62 | + $profiler = new PhpQuickProfiler(); |
|
63 | + $profiler->setConsole($console); |
|
64 | + |
|
65 | + $this->assertAttributeSame($console, 'console', $profiler); |
|
66 | + } |
|
67 | + |
|
68 | + public function testSetDisplay() |
|
69 | + { |
|
70 | + $display = new Display(); |
|
71 | + $profiler = new PhpQuickProfiler(); |
|
72 | + $profiler->setDisplay($display); |
|
73 | + |
|
74 | + $this->assertAttributeSame($display, 'display', $profiler); |
|
75 | + } |
|
76 | + |
|
77 | + public function testGatherFileData() |
|
78 | + { |
|
79 | + $files = get_included_files(); |
|
80 | + $profiler = new PhpQuickProfiler(); |
|
81 | + $gatheredFileData = $profiler->gatherFileData(); |
|
82 | + |
|
83 | + $this->assertInternalType('array', $gatheredFileData); |
|
84 | + $this->assertEquals(count($files), count($gatheredFileData)); |
|
85 | + foreach ($gatheredFileData as $fileData) { |
|
86 | + $this->assertInternalType('array', $fileData); |
|
87 | + $this->assertArrayHasKey('name', $fileData); |
|
88 | + $this->assertContains($fileData['name'], $files); |
|
89 | + $this->assertArrayHasKey('size', $fileData); |
|
90 | + $this->assertEquals($fileData['size'], filesize($fileData['name'])); |
|
91 | + } |
|
92 | + } |
|
93 | + |
|
94 | + public function testGatherMemoryData() |
|
95 | + { |
|
96 | + $memoryUsage = memory_get_peak_usage(); |
|
97 | + $allowedLimit = ini_get('memory_limit'); |
|
98 | + $profiler = new PhpQuickProfiler(); |
|
99 | + $gatheredMemoryData = $profiler->gatherMemoryData(); |
|
100 | + |
|
101 | + $this->assertInternalType('array', $gatheredMemoryData); |
|
102 | + $this->assertEquals(2, count($gatheredMemoryData)); |
|
103 | + $this->assertArrayHasKey('used', $gatheredMemoryData); |
|
104 | + $this->assertEquals($memoryUsage, $gatheredMemoryData['used']); |
|
105 | + $this->assertArrayHasKey('allowed', $gatheredMemoryData); |
|
106 | + $this->assertEquals($allowedLimit, $gatheredMemoryData['allowed']); |
|
107 | + } |
|
108 | + |
|
109 | + public function testSetProfiledQueries() |
|
110 | + { |
|
111 | + $profiledQueries = array( |
|
112 | + 'sql' => 'SELECT * FROM example', |
|
113 | + 'time' => 25 |
|
114 | + ); |
|
115 | + $profiler = new PhpQuickProfiler(); |
|
116 | + $profiler->setProfiledQueries($profiledQueries); |
|
117 | + |
|
118 | + $this->assertAttributeEquals($profiledQueries, 'profiledQueries', $profiler); |
|
119 | + } |
|
120 | + |
|
121 | + public function testGatherSpeedData() |
|
122 | + { |
|
123 | + $elapsedTime = 1.234; |
|
124 | + $startTime = microtime(true) - $elapsedTime; |
|
125 | + $allowedTime = ini_get('max_execution_time'); |
|
126 | + $profiler = new PhpQuickProfiler($startTime); |
|
127 | + $gatheredSpeedData = $profiler->gatherSpeedData(); |
|
128 | + |
|
129 | + $this->assertInternalType('array', $gatheredSpeedData); |
|
130 | + $this->assertEquals(2, count($gatheredSpeedData)); |
|
131 | + $this->assertArrayHasKey('elapsed', $gatheredSpeedData); |
|
132 | + $this->assertEquals($elapsedTime, $gatheredSpeedData['elapsed']); |
|
133 | + $this->assertArrayHasKey('allowed', $gatheredSpeedData); |
|
134 | + $this->assertEquals($allowedTime, $gatheredSpeedData['allowed']); |
|
135 | + } |
|
136 | 136 | } |