@@ -6,5 +6,5 @@ |
||
| 6 | 6 | |
| 7 | 7 | class LoggingPdo extends PDO |
| 8 | 8 | { |
| 9 | - public $queries = array(); |
|
| 9 | + public $queries = array(); |
|
| 10 | 10 | } |
@@ -1,5 +1,5 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -require_once __DIR__ . '/../vendor/autoload.php'; |
|
| 4 | -require_once __DIR__ . '/helpers/function-overrides.php'; |
|
| 5 | -require_once __DIR__ . '/helpers/LoggingPdo.php'; |
|
| 3 | +require_once __DIR__.'/../vendor/autoload.php'; |
|
| 4 | +require_once __DIR__.'/helpers/function-overrides.php'; |
|
| 5 | +require_once __DIR__.'/helpers/LoggingPdo.php'; |
|
@@ -9,19 +9,19 @@ discard block |
||
| 9 | 9 | class PhpQuickProfilerTest extends PHPUnit_Framework_TestCase |
| 10 | 10 | { |
| 11 | 11 | |
| 12 | - protected static $dbConnection; |
|
| 12 | + protected static $dbConnection; |
|
| 13 | 13 | |
| 14 | - public static function setUpBeforeClass() |
|
| 15 | - { |
|
| 16 | - self::$dbConnection = new LoggingPdo('sqlite::memory:'); |
|
| 17 | - $createTable = " |
|
| 14 | + public static function setUpBeforeClass() |
|
| 15 | + { |
|
| 16 | + self::$dbConnection = new LoggingPdo('sqlite::memory:'); |
|
| 17 | + $createTable = " |
|
| 18 | 18 | CREATE TABLE IF NOT EXISTS `testing` ( |
| 19 | 19 | `id` integer PRIMARY KEY AUTOINCREMENT, |
| 20 | 20 | `title` varchar(60) NOT NULL |
| 21 | 21 | );"; |
| 22 | - self::$dbConnection->exec($createTable); |
|
| 22 | + self::$dbConnection->exec($createTable); |
|
| 23 | 23 | |
| 24 | - $hydrateTable = " |
|
| 24 | + $hydrateTable = " |
|
| 25 | 25 | INSERT INTO `testing` |
| 26 | 26 | (`title`) |
| 27 | 27 | VALUES |
@@ -29,256 +29,256 @@ discard block |
||
| 29 | 29 | ('beta'), |
| 30 | 30 | ('charlie'), |
| 31 | 31 | ('delta');"; |
| 32 | - self::$dbConnection->exec($hydrateTable); |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - public function testConstruct() |
|
| 36 | - { |
|
| 37 | - $startTime = microtime(true); |
|
| 38 | - |
|
| 39 | - $profiler = new PhpQuickProfiler(); |
|
| 40 | - $this->assertAttributeEquals($startTime, 'startTime', $profiler); |
|
| 41 | - |
|
| 42 | - $profiler = new PhpQuickProfiler($startTime); |
|
| 43 | - $this->assertAttributeEquals($startTime, 'startTime', $profiler); |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - public function testSetConsole() |
|
| 47 | - { |
|
| 48 | - $console = new Console(); |
|
| 49 | - $profiler = new PhpQuickProfiler(); |
|
| 50 | - $profiler->setConsole($console); |
|
| 51 | - |
|
| 52 | - $this->assertAttributeSame($console, 'console', $profiler); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - public function testSetDisplay() |
|
| 56 | - { |
|
| 57 | - $display = new Display(); |
|
| 58 | - $profiler = new PhpQuickProfiler(); |
|
| 59 | - $profiler->setDisplay($display); |
|
| 60 | - |
|
| 61 | - $this->assertAttributeSame($display, 'display', $profiler); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - public function testGatherFileData() |
|
| 65 | - { |
|
| 66 | - $files = get_included_files(); |
|
| 67 | - $profiler = new PhpQuickProfiler(); |
|
| 68 | - $gatheredFileData = $profiler->gatherFileData(); |
|
| 69 | - |
|
| 70 | - $this->assertInternalType('array', $gatheredFileData); |
|
| 71 | - $this->assertEquals(count($files), count($gatheredFileData)); |
|
| 72 | - foreach ($gatheredFileData as $fileData) { |
|
| 73 | - $this->assertInternalType('array', $fileData); |
|
| 74 | - $this->assertArrayHasKey('name', $fileData); |
|
| 75 | - $this->assertContains($fileData['name'], $files); |
|
| 76 | - $this->assertArrayHasKey('size', $fileData); |
|
| 77 | - $this->assertEquals($fileData['size'], filesize($fileData['name'])); |
|
| 78 | - } |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - public function testGatherMemoryData() |
|
| 82 | - { |
|
| 83 | - $memoryUsage = memory_get_peak_usage(); |
|
| 84 | - $allowedLimit = ini_get('memory_limit'); |
|
| 85 | - $profiler = new PhpQuickProfiler(); |
|
| 86 | - $gatheredMemoryData = $profiler->gatherMemoryData(); |
|
| 87 | - |
|
| 88 | - $this->assertInternalType('array', $gatheredMemoryData); |
|
| 89 | - $this->assertEquals(2, count($gatheredMemoryData)); |
|
| 90 | - $this->assertArrayHasKey('used', $gatheredMemoryData); |
|
| 91 | - $this->assertEquals($memoryUsage, $gatheredMemoryData['used']); |
|
| 92 | - $this->assertArrayHasKey('allowed', $gatheredMemoryData); |
|
| 93 | - $this->assertEquals($allowedLimit, $gatheredMemoryData['allowed']); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - public function testSetProfiledQueries() |
|
| 97 | - { |
|
| 98 | - $profiledQueries = $this->dataProfiledQueries(); |
|
| 99 | - $profiler = new PhpQuickProfiler(); |
|
| 100 | - $profiler->setProfiledQueries($profiledQueries); |
|
| 101 | - |
|
| 102 | - $this->assertAttributeEquals($profiledQueries, 'profiledQueries', $profiler); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - public function testGatherQueryData() |
|
| 106 | - { |
|
| 107 | - $profiledQueries = $this->dataProfiledQueries(); |
|
| 108 | - $profiledQueriesSql = array(); |
|
| 109 | - $profiledQueriesTime = array(); |
|
| 110 | - foreach ($profiledQueries as $queryData) { |
|
| 111 | - array_push($profiledQueriesSql, $queryData['sql']); |
|
| 112 | - array_push($profiledQueriesTime, $queryData['time']); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - $profiler = new PhpQuickProfiler(); |
|
| 116 | - $profiler->setProfiledQueries($profiledQueries); |
|
| 117 | - $gatheredQueryData = $profiler->gatherQueryData(self::$dbConnection); |
|
| 118 | - |
|
| 119 | - $this->assertInternalType('array', $gatheredQueryData); |
|
| 120 | - $this->assertEquals(count($profiledQueries), count($gatheredQueryData)); |
|
| 121 | - foreach ($gatheredQueryData as $queryData) { |
|
| 122 | - $this->assertInternalType('array', $queryData); |
|
| 123 | - $this->assertArrayHasKey('sql', $queryData); |
|
| 124 | - $this->assertContains($queryData['sql'], $profiledQueriesSql); |
|
| 125 | - $this->assertArrayHasKey('explain', $queryData); |
|
| 126 | - $this->assertInternaltype('array', $queryData['explain']); |
|
| 127 | - $this->assertGreaterThan(0, count($queryData['explain'])); |
|
| 128 | - $this->assertArrayHasKey('time', $queryData); |
|
| 129 | - $this->assertContains($queryData['time'], $profiledQueriesTime); |
|
| 130 | - } |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - public function testGatherQueryDataInternalProfiler() |
|
| 134 | - { |
|
| 135 | - $profiledQueries = $this->dataProfiledQueries(); |
|
| 136 | - $dbConnection = self::$dbConnection; |
|
| 137 | - $dbConnection->queries = $profiledQueries; |
|
| 138 | - $profiler = new PhpQuickProfiler(); |
|
| 139 | - $profiler->gatherQueryData($dbConnection); |
|
| 140 | - |
|
| 141 | - $this->assertAttributeSame($profiledQueries, 'profiledQueries', $profiler); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * @dataProvider dataProfiledQueries |
|
| 146 | - */ |
|
| 147 | - public function testExplainQuery($sql, $parameters) |
|
| 148 | - { |
|
| 149 | - $profiler = new PhpQuickProfiler(); |
|
| 150 | - $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery'); |
|
| 151 | - |
|
| 152 | - $explainedQuery = $reflectedMethod->invokeArgs( |
|
| 153 | - $profiler, |
|
| 154 | - array(self::$dbConnection, $sql, $parameters) |
|
| 155 | - ); |
|
| 156 | - $this->assertInternalType('array', $explainedQuery); |
|
| 157 | - $this->assertGreaterThan(0, count($explainedQuery)); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * @expectedException Exception |
|
| 162 | - */ |
|
| 163 | - public function testExplainQueryBadQueryException() |
|
| 164 | - { |
|
| 165 | - $invalidQuery = 'SELECT * FROM `fake_table`'; |
|
| 166 | - $profiler = new PhpQuickProfiler(); |
|
| 167 | - $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery'); |
|
| 168 | - |
|
| 169 | - $reflectedMethod->invokeArgs( |
|
| 170 | - $profiler, |
|
| 171 | - array(self::$dbConnection, $invalidQuery) |
|
| 172 | - ); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * @expectedException Exception |
|
| 177 | - */ |
|
| 178 | - public function testExplainQueryBadParametersException() |
|
| 179 | - { |
|
| 180 | - $query = 'SELECT * FROM `testing` WHERE `title` = :title'; |
|
| 181 | - $invalidParams = array('id' => 1); |
|
| 182 | - $profiler = new PhpQuickProfiler(); |
|
| 183 | - $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery'); |
|
| 184 | - |
|
| 185 | - $reflectedMethod->invokeArgs( |
|
| 186 | - $profiler, |
|
| 187 | - array(self::$dbConnection, $query, $invalidParams) |
|
| 188 | - ); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * @dataProvider dataConnectionDrivers |
|
| 193 | - */ |
|
| 194 | - public function testGetExplainQuery($driver, $prefix) |
|
| 195 | - { |
|
| 196 | - $query = 'SELECT * FROM `testing`'; |
|
| 197 | - $profiler = new PhpQuickProfiler(); |
|
| 198 | - $reflectedMethod = $this->getAccessibleMethod($profiler, 'getExplainQuery'); |
|
| 199 | - |
|
| 200 | - $explainQuery = $reflectedMethod->invokeArgs( |
|
| 201 | - $profiler, |
|
| 202 | - array($query, $driver) |
|
| 203 | - ); |
|
| 204 | - |
|
| 205 | - $explainPrefix = str_replace($query, '', $explainQuery); |
|
| 206 | - $explainPrefix = trim($explainPrefix); |
|
| 207 | - $this->assertEquals($prefix, $explainPrefix); |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * @expectedException Exception |
|
| 212 | - */ |
|
| 213 | - public function testGetExplainQueryUnsupportedDriver() |
|
| 214 | - { |
|
| 215 | - $query = 'SELECT * FROM `testing`'; |
|
| 216 | - $unsupportedDriver = 'zz'; |
|
| 217 | - $profiler = new PhpQuickProfiler(); |
|
| 218 | - $reflectedMethod = $this->getAccessibleMethod($profiler, 'getExplainQuery'); |
|
| 219 | - |
|
| 220 | - $reflectedMethod->invokeArgs( |
|
| 221 | - $profiler, |
|
| 222 | - array($query, $unsupportedDriver) |
|
| 223 | - ); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - public function testGatherSpeedData() |
|
| 227 | - { |
|
| 228 | - $elapsedTime = 1.234; |
|
| 229 | - $startTime = microtime(true) - $elapsedTime; |
|
| 230 | - $allowedTime = ini_get('max_execution_time'); |
|
| 231 | - $profiler = new PhpQuickProfiler($startTime); |
|
| 232 | - $gatheredSpeedData = $profiler->gatherSpeedData(); |
|
| 233 | - |
|
| 234 | - $this->assertInternalType('array', $gatheredSpeedData); |
|
| 235 | - $this->assertEquals(2, count($gatheredSpeedData)); |
|
| 236 | - $this->assertArrayHasKey('elapsed', $gatheredSpeedData); |
|
| 237 | - $this->assertEquals($elapsedTime, $gatheredSpeedData['elapsed']); |
|
| 238 | - $this->assertArrayHasKey('allowed', $gatheredSpeedData); |
|
| 239 | - $this->assertEquals($allowedTime, $gatheredSpeedData['allowed']); |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - public function dataProfiledQueries() |
|
| 243 | - { |
|
| 244 | - return array( |
|
| 245 | - array( |
|
| 246 | - 'sql' => "SELECT * FROM testing", |
|
| 247 | - 'parameters' => array(), |
|
| 248 | - 'time' => 25 |
|
| 249 | - ), |
|
| 250 | - array( |
|
| 251 | - 'sql' => "SELECT id FROM testing WHERE title = :title", |
|
| 252 | - 'parameters' => array('title' => 'beta'), |
|
| 253 | - 'time' => 5 |
|
| 254 | - ) |
|
| 255 | - ); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - public function dataConnectionDrivers() |
|
| 259 | - { |
|
| 260 | - return array( |
|
| 261 | - array( |
|
| 262 | - 'driver' => 'mysql', |
|
| 263 | - 'prefix' => 'EXPLAIN' |
|
| 264 | - ), |
|
| 265 | - array( |
|
| 266 | - 'driver' => 'sqlite', |
|
| 267 | - 'prefix' => 'EXPLAIN QUERY PLAN' |
|
| 268 | - ) |
|
| 269 | - ); |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - protected function getAccessibleMethod(PhpQuickProfiler $profiler, $methodName) |
|
| 273 | - { |
|
| 274 | - $reflectedConsole = new ReflectionClass(get_class($profiler)); |
|
| 275 | - $reflectedMethod = $reflectedConsole->getMethod($methodName); |
|
| 276 | - $reflectedMethod->setAccessible(true); |
|
| 277 | - return $reflectedMethod; |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - public static function tearDownAfterClass() |
|
| 281 | - { |
|
| 282 | - self::$dbConnection = null; |
|
| 283 | - } |
|
| 32 | + self::$dbConnection->exec($hydrateTable); |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + public function testConstruct() |
|
| 36 | + { |
|
| 37 | + $startTime = microtime(true); |
|
| 38 | + |
|
| 39 | + $profiler = new PhpQuickProfiler(); |
|
| 40 | + $this->assertAttributeEquals($startTime, 'startTime', $profiler); |
|
| 41 | + |
|
| 42 | + $profiler = new PhpQuickProfiler($startTime); |
|
| 43 | + $this->assertAttributeEquals($startTime, 'startTime', $profiler); |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + public function testSetConsole() |
|
| 47 | + { |
|
| 48 | + $console = new Console(); |
|
| 49 | + $profiler = new PhpQuickProfiler(); |
|
| 50 | + $profiler->setConsole($console); |
|
| 51 | + |
|
| 52 | + $this->assertAttributeSame($console, 'console', $profiler); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + public function testSetDisplay() |
|
| 56 | + { |
|
| 57 | + $display = new Display(); |
|
| 58 | + $profiler = new PhpQuickProfiler(); |
|
| 59 | + $profiler->setDisplay($display); |
|
| 60 | + |
|
| 61 | + $this->assertAttributeSame($display, 'display', $profiler); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + public function testGatherFileData() |
|
| 65 | + { |
|
| 66 | + $files = get_included_files(); |
|
| 67 | + $profiler = new PhpQuickProfiler(); |
|
| 68 | + $gatheredFileData = $profiler->gatherFileData(); |
|
| 69 | + |
|
| 70 | + $this->assertInternalType('array', $gatheredFileData); |
|
| 71 | + $this->assertEquals(count($files), count($gatheredFileData)); |
|
| 72 | + foreach ($gatheredFileData as $fileData) { |
|
| 73 | + $this->assertInternalType('array', $fileData); |
|
| 74 | + $this->assertArrayHasKey('name', $fileData); |
|
| 75 | + $this->assertContains($fileData['name'], $files); |
|
| 76 | + $this->assertArrayHasKey('size', $fileData); |
|
| 77 | + $this->assertEquals($fileData['size'], filesize($fileData['name'])); |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + public function testGatherMemoryData() |
|
| 82 | + { |
|
| 83 | + $memoryUsage = memory_get_peak_usage(); |
|
| 84 | + $allowedLimit = ini_get('memory_limit'); |
|
| 85 | + $profiler = new PhpQuickProfiler(); |
|
| 86 | + $gatheredMemoryData = $profiler->gatherMemoryData(); |
|
| 87 | + |
|
| 88 | + $this->assertInternalType('array', $gatheredMemoryData); |
|
| 89 | + $this->assertEquals(2, count($gatheredMemoryData)); |
|
| 90 | + $this->assertArrayHasKey('used', $gatheredMemoryData); |
|
| 91 | + $this->assertEquals($memoryUsage, $gatheredMemoryData['used']); |
|
| 92 | + $this->assertArrayHasKey('allowed', $gatheredMemoryData); |
|
| 93 | + $this->assertEquals($allowedLimit, $gatheredMemoryData['allowed']); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + public function testSetProfiledQueries() |
|
| 97 | + { |
|
| 98 | + $profiledQueries = $this->dataProfiledQueries(); |
|
| 99 | + $profiler = new PhpQuickProfiler(); |
|
| 100 | + $profiler->setProfiledQueries($profiledQueries); |
|
| 101 | + |
|
| 102 | + $this->assertAttributeEquals($profiledQueries, 'profiledQueries', $profiler); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + public function testGatherQueryData() |
|
| 106 | + { |
|
| 107 | + $profiledQueries = $this->dataProfiledQueries(); |
|
| 108 | + $profiledQueriesSql = array(); |
|
| 109 | + $profiledQueriesTime = array(); |
|
| 110 | + foreach ($profiledQueries as $queryData) { |
|
| 111 | + array_push($profiledQueriesSql, $queryData['sql']); |
|
| 112 | + array_push($profiledQueriesTime, $queryData['time']); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + $profiler = new PhpQuickProfiler(); |
|
| 116 | + $profiler->setProfiledQueries($profiledQueries); |
|
| 117 | + $gatheredQueryData = $profiler->gatherQueryData(self::$dbConnection); |
|
| 118 | + |
|
| 119 | + $this->assertInternalType('array', $gatheredQueryData); |
|
| 120 | + $this->assertEquals(count($profiledQueries), count($gatheredQueryData)); |
|
| 121 | + foreach ($gatheredQueryData as $queryData) { |
|
| 122 | + $this->assertInternalType('array', $queryData); |
|
| 123 | + $this->assertArrayHasKey('sql', $queryData); |
|
| 124 | + $this->assertContains($queryData['sql'], $profiledQueriesSql); |
|
| 125 | + $this->assertArrayHasKey('explain', $queryData); |
|
| 126 | + $this->assertInternaltype('array', $queryData['explain']); |
|
| 127 | + $this->assertGreaterThan(0, count($queryData['explain'])); |
|
| 128 | + $this->assertArrayHasKey('time', $queryData); |
|
| 129 | + $this->assertContains($queryData['time'], $profiledQueriesTime); |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + public function testGatherQueryDataInternalProfiler() |
|
| 134 | + { |
|
| 135 | + $profiledQueries = $this->dataProfiledQueries(); |
|
| 136 | + $dbConnection = self::$dbConnection; |
|
| 137 | + $dbConnection->queries = $profiledQueries; |
|
| 138 | + $profiler = new PhpQuickProfiler(); |
|
| 139 | + $profiler->gatherQueryData($dbConnection); |
|
| 140 | + |
|
| 141 | + $this->assertAttributeSame($profiledQueries, 'profiledQueries', $profiler); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * @dataProvider dataProfiledQueries |
|
| 146 | + */ |
|
| 147 | + public function testExplainQuery($sql, $parameters) |
|
| 148 | + { |
|
| 149 | + $profiler = new PhpQuickProfiler(); |
|
| 150 | + $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery'); |
|
| 151 | + |
|
| 152 | + $explainedQuery = $reflectedMethod->invokeArgs( |
|
| 153 | + $profiler, |
|
| 154 | + array(self::$dbConnection, $sql, $parameters) |
|
| 155 | + ); |
|
| 156 | + $this->assertInternalType('array', $explainedQuery); |
|
| 157 | + $this->assertGreaterThan(0, count($explainedQuery)); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * @expectedException Exception |
|
| 162 | + */ |
|
| 163 | + public function testExplainQueryBadQueryException() |
|
| 164 | + { |
|
| 165 | + $invalidQuery = 'SELECT * FROM `fake_table`'; |
|
| 166 | + $profiler = new PhpQuickProfiler(); |
|
| 167 | + $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery'); |
|
| 168 | + |
|
| 169 | + $reflectedMethod->invokeArgs( |
|
| 170 | + $profiler, |
|
| 171 | + array(self::$dbConnection, $invalidQuery) |
|
| 172 | + ); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * @expectedException Exception |
|
| 177 | + */ |
|
| 178 | + public function testExplainQueryBadParametersException() |
|
| 179 | + { |
|
| 180 | + $query = 'SELECT * FROM `testing` WHERE `title` = :title'; |
|
| 181 | + $invalidParams = array('id' => 1); |
|
| 182 | + $profiler = new PhpQuickProfiler(); |
|
| 183 | + $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery'); |
|
| 184 | + |
|
| 185 | + $reflectedMethod->invokeArgs( |
|
| 186 | + $profiler, |
|
| 187 | + array(self::$dbConnection, $query, $invalidParams) |
|
| 188 | + ); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * @dataProvider dataConnectionDrivers |
|
| 193 | + */ |
|
| 194 | + public function testGetExplainQuery($driver, $prefix) |
|
| 195 | + { |
|
| 196 | + $query = 'SELECT * FROM `testing`'; |
|
| 197 | + $profiler = new PhpQuickProfiler(); |
|
| 198 | + $reflectedMethod = $this->getAccessibleMethod($profiler, 'getExplainQuery'); |
|
| 199 | + |
|
| 200 | + $explainQuery = $reflectedMethod->invokeArgs( |
|
| 201 | + $profiler, |
|
| 202 | + array($query, $driver) |
|
| 203 | + ); |
|
| 204 | + |
|
| 205 | + $explainPrefix = str_replace($query, '', $explainQuery); |
|
| 206 | + $explainPrefix = trim($explainPrefix); |
|
| 207 | + $this->assertEquals($prefix, $explainPrefix); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * @expectedException Exception |
|
| 212 | + */ |
|
| 213 | + public function testGetExplainQueryUnsupportedDriver() |
|
| 214 | + { |
|
| 215 | + $query = 'SELECT * FROM `testing`'; |
|
| 216 | + $unsupportedDriver = 'zz'; |
|
| 217 | + $profiler = new PhpQuickProfiler(); |
|
| 218 | + $reflectedMethod = $this->getAccessibleMethod($profiler, 'getExplainQuery'); |
|
| 219 | + |
|
| 220 | + $reflectedMethod->invokeArgs( |
|
| 221 | + $profiler, |
|
| 222 | + array($query, $unsupportedDriver) |
|
| 223 | + ); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + public function testGatherSpeedData() |
|
| 227 | + { |
|
| 228 | + $elapsedTime = 1.234; |
|
| 229 | + $startTime = microtime(true) - $elapsedTime; |
|
| 230 | + $allowedTime = ini_get('max_execution_time'); |
|
| 231 | + $profiler = new PhpQuickProfiler($startTime); |
|
| 232 | + $gatheredSpeedData = $profiler->gatherSpeedData(); |
|
| 233 | + |
|
| 234 | + $this->assertInternalType('array', $gatheredSpeedData); |
|
| 235 | + $this->assertEquals(2, count($gatheredSpeedData)); |
|
| 236 | + $this->assertArrayHasKey('elapsed', $gatheredSpeedData); |
|
| 237 | + $this->assertEquals($elapsedTime, $gatheredSpeedData['elapsed']); |
|
| 238 | + $this->assertArrayHasKey('allowed', $gatheredSpeedData); |
|
| 239 | + $this->assertEquals($allowedTime, $gatheredSpeedData['allowed']); |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + public function dataProfiledQueries() |
|
| 243 | + { |
|
| 244 | + return array( |
|
| 245 | + array( |
|
| 246 | + 'sql' => "SELECT * FROM testing", |
|
| 247 | + 'parameters' => array(), |
|
| 248 | + 'time' => 25 |
|
| 249 | + ), |
|
| 250 | + array( |
|
| 251 | + 'sql' => "SELECT id FROM testing WHERE title = :title", |
|
| 252 | + 'parameters' => array('title' => 'beta'), |
|
| 253 | + 'time' => 5 |
|
| 254 | + ) |
|
| 255 | + ); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + public function dataConnectionDrivers() |
|
| 259 | + { |
|
| 260 | + return array( |
|
| 261 | + array( |
|
| 262 | + 'driver' => 'mysql', |
|
| 263 | + 'prefix' => 'EXPLAIN' |
|
| 264 | + ), |
|
| 265 | + array( |
|
| 266 | + 'driver' => 'sqlite', |
|
| 267 | + 'prefix' => 'EXPLAIN QUERY PLAN' |
|
| 268 | + ) |
|
| 269 | + ); |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + protected function getAccessibleMethod(PhpQuickProfiler $profiler, $methodName) |
|
| 273 | + { |
|
| 274 | + $reflectedConsole = new ReflectionClass(get_class($profiler)); |
|
| 275 | + $reflectedMethod = $reflectedConsole->getMethod($methodName); |
|
| 276 | + $reflectedMethod->setAccessible(true); |
|
| 277 | + return $reflectedMethod; |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + public static function tearDownAfterClass() |
|
| 281 | + { |
|
| 282 | + self::$dbConnection = null; |
|
| 283 | + } |
|
| 284 | 284 | } |
@@ -17,186 +17,186 @@ |
||
| 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 $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 | - array_push($data, array( |
|
| 115 | - 'sql' => $query['sql'], |
|
| 116 | - 'explain' => $this->explainQuery($dbConnection, $query['sql'], $query['parameters']), |
|
| 117 | - 'time' => $query['time'] |
|
| 118 | - )); |
|
| 119 | - } |
|
| 120 | - return $data; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Attempts to explain a query |
|
| 125 | - * |
|
| 126 | - * @param object $dbConnection |
|
| 127 | - * @param string $query |
|
| 128 | - * @param array $parameters |
|
| 129 | - * @throws Exception |
|
| 130 | - * @return array |
|
| 131 | - */ |
|
| 132 | - protected function explainQuery($dbConnection, $query, $parameters = array()) |
|
| 133 | - { |
|
| 134 | - $driver = $dbConnection->getAttribute(\PDO::ATTR_DRIVER_NAME); |
|
| 135 | - $query = $this->getExplainQuery($query, $driver); |
|
| 136 | - $statement = $dbConnection->prepare($query); |
|
| 137 | - if ($statement === false) { |
|
| 138 | - throw new Exception('Invalid query passed to explainQuery method'); |
|
| 139 | - } |
|
| 140 | - $statement->execute($parameters); |
|
| 141 | - $result = $statement->fetch(\PDO::FETCH_ASSOC); |
|
| 142 | - if ($result === false) { |
|
| 143 | - throw new Exception('Query could not be explained with given parameters'); |
|
| 144 | - } |
|
| 145 | - return $result; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * Attempts to figure out what kind of explain query format the db wants |
|
| 150 | - * |
|
| 151 | - * @param string $query |
|
| 152 | - * @param string $driver |
|
| 153 | - * @throws Exception |
|
| 154 | - * @return string |
|
| 155 | - */ |
|
| 156 | - protected function getExplainQuery($query, $driver) |
|
| 157 | - { |
|
| 158 | - if ($driver == 'mysql') { |
|
| 159 | - return "EXPLAIN {$query}"; |
|
| 160 | - } elseif ($driver == 'sqlite') { |
|
| 161 | - return "EXPLAIN QUERY PLAN {$query}"; |
|
| 162 | - } |
|
| 163 | - throw new Exception('Could not process db driver'); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Get data about speed of the application |
|
| 168 | - * |
|
| 169 | - * @returns array |
|
| 170 | - */ |
|
| 171 | - public function gatherSpeedData() |
|
| 172 | - { |
|
| 173 | - $elapsedTime = microtime(true) - $this->startTime; |
|
| 174 | - $elapsedTime = round($elapsedTime, 3); |
|
| 175 | - $allowedTime = ini_get('max_execution_time'); |
|
| 176 | - return array( |
|
| 177 | - 'elapsed' => $elapsedTime, |
|
| 178 | - 'allowed' => $allowedTime |
|
| 179 | - ); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Triggers end display of the profiling data |
|
| 184 | - * |
|
| 185 | - * @param object $dbConnection |
|
| 186 | - * @throws Exception |
|
| 187 | - */ |
|
| 188 | - public function display($dbConnection = null) |
|
| 189 | - { |
|
| 190 | - if (!isset($this->display)) { |
|
| 191 | - throw new Exception('Display object has not been injected into Profiler'); |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - $this->display->setConsole($this->console); |
|
| 195 | - $this->display->setFileData($this->gatherFileData()); |
|
| 196 | - $this->display->setMemoryData($this->gatherMemoryData()); |
|
| 197 | - $this->display->setQueryData($this->gatherQueryData($dbConnection)); |
|
| 198 | - $this->display->setSpeedData($this->gatherSpeedData()); |
|
| 199 | - |
|
| 200 | - $this->display->__invoke(); |
|
| 201 | - } |
|
| 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 $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 | + array_push($data, array( |
|
| 115 | + 'sql' => $query['sql'], |
|
| 116 | + 'explain' => $this->explainQuery($dbConnection, $query['sql'], $query['parameters']), |
|
| 117 | + 'time' => $query['time'] |
|
| 118 | + )); |
|
| 119 | + } |
|
| 120 | + return $data; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Attempts to explain a query |
|
| 125 | + * |
|
| 126 | + * @param object $dbConnection |
|
| 127 | + * @param string $query |
|
| 128 | + * @param array $parameters |
|
| 129 | + * @throws Exception |
|
| 130 | + * @return array |
|
| 131 | + */ |
|
| 132 | + protected function explainQuery($dbConnection, $query, $parameters = array()) |
|
| 133 | + { |
|
| 134 | + $driver = $dbConnection->getAttribute(\PDO::ATTR_DRIVER_NAME); |
|
| 135 | + $query = $this->getExplainQuery($query, $driver); |
|
| 136 | + $statement = $dbConnection->prepare($query); |
|
| 137 | + if ($statement === false) { |
|
| 138 | + throw new Exception('Invalid query passed to explainQuery method'); |
|
| 139 | + } |
|
| 140 | + $statement->execute($parameters); |
|
| 141 | + $result = $statement->fetch(\PDO::FETCH_ASSOC); |
|
| 142 | + if ($result === false) { |
|
| 143 | + throw new Exception('Query could not be explained with given parameters'); |
|
| 144 | + } |
|
| 145 | + return $result; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * Attempts to figure out what kind of explain query format the db wants |
|
| 150 | + * |
|
| 151 | + * @param string $query |
|
| 152 | + * @param string $driver |
|
| 153 | + * @throws Exception |
|
| 154 | + * @return string |
|
| 155 | + */ |
|
| 156 | + protected function getExplainQuery($query, $driver) |
|
| 157 | + { |
|
| 158 | + if ($driver == 'mysql') { |
|
| 159 | + return "EXPLAIN {$query}"; |
|
| 160 | + } elseif ($driver == 'sqlite') { |
|
| 161 | + return "EXPLAIN QUERY PLAN {$query}"; |
|
| 162 | + } |
|
| 163 | + throw new Exception('Could not process db driver'); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Get data about speed of the application |
|
| 168 | + * |
|
| 169 | + * @returns array |
|
| 170 | + */ |
|
| 171 | + public function gatherSpeedData() |
|
| 172 | + { |
|
| 173 | + $elapsedTime = microtime(true) - $this->startTime; |
|
| 174 | + $elapsedTime = round($elapsedTime, 3); |
|
| 175 | + $allowedTime = ini_get('max_execution_time'); |
|
| 176 | + return array( |
|
| 177 | + 'elapsed' => $elapsedTime, |
|
| 178 | + 'allowed' => $allowedTime |
|
| 179 | + ); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Triggers end display of the profiling data |
|
| 184 | + * |
|
| 185 | + * @param object $dbConnection |
|
| 186 | + * @throws Exception |
|
| 187 | + */ |
|
| 188 | + public function display($dbConnection = null) |
|
| 189 | + { |
|
| 190 | + if (!isset($this->display)) { |
|
| 191 | + throw new Exception('Display object has not been injected into Profiler'); |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + $this->display->setConsole($this->console); |
|
| 195 | + $this->display->setFileData($this->gatherFileData()); |
|
| 196 | + $this->display->setMemoryData($this->gatherMemoryData()); |
|
| 197 | + $this->display->setQueryData($this->gatherQueryData($dbConnection)); |
|
| 198 | + $this->display->setSpeedData($this->gatherSpeedData()); |
|
| 199 | + |
|
| 200 | + $this->display->__invoke(); |
|
| 201 | + } |
|
| 202 | 202 | } |