Added query count statistics to responses

This commit is contained in:
Marcin Kurczewski 2014-10-04 13:28:04 +02:00
parent 69edaa2159
commit 332ca8e4b7
4 changed files with 27 additions and 2 deletions

View file

@ -35,7 +35,7 @@ class DatabaseConnection
$cwd = getcwd(); $cwd = getcwd();
if ($this->config->getDataDirectory()) if ($this->config->getDataDirectory())
chdir($this->config->getDataDirectory()); chdir($this->config->getDataDirectory());
$this->pdo = new \PDO($this->config->database->dsn, $this->config->database->user, $this->pdo = new PDOEx($this->config->database->dsn, $this->config->database->user,
$this->config->database->password); $this->config->database->password);
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
chdir($cwd); chdir($cwd);

View file

@ -4,17 +4,20 @@ namespace Szurubooru;
final class Dispatcher final class Dispatcher
{ {
private $router; private $router;
private $databaseConnection;
private $authService; private $authService;
private $tokenService; private $tokenService;
public function __construct( public function __construct(
\Szurubooru\Router $router, \Szurubooru\Router $router,
\Szurubooru\DatabaseConnection $databaseConnection,
\Szurubooru\Helpers\HttpHelper $httpHelper, \Szurubooru\Helpers\HttpHelper $httpHelper,
\Szurubooru\Services\AuthService $authService, \Szurubooru\Services\AuthService $authService,
\Szurubooru\Services\TokenService $tokenService, \Szurubooru\Services\TokenService $tokenService,
\Szurubooru\ControllerRepository $controllerRepository) \Szurubooru\ControllerRepository $controllerRepository)
{ {
$this->router = $router; $this->router = $router;
$this->databaseConnection = $databaseConnection;
$this->httpHelper = $httpHelper; $this->httpHelper = $httpHelper;
//if script fails prematurely, mark it as fail from advance //if script fails prematurely, mark it as fail from advance
@ -48,6 +51,7 @@ final class Dispatcher
} }
$end = microtime(true); $end = microtime(true);
$json['__time'] = $end - $start; $json['__time'] = $end - $start;
$json['__queries'] = $this->databaseConnection->getPDO()->getQueryCount();
$this->httpHelper->setResponseCode($code); $this->httpHelper->setResponseCode($code);
$this->httpHelper->setHeader('Content-Type', 'application/json'); $this->httpHelper->setHeader('Content-Type', 'application/json');

18
src/PDOEx.php Normal file
View file

@ -0,0 +1,18 @@
<?php
namespace Szurubooru;
class PDOEx extends \PDO
{
private $queryCount = 0;
public function prepare($statement, $driverOptions = [])
{
++ $this->queryCount;
return parent::prepare($statement, $driverOptions);
}
public function getQueryCount()
{
return $this->queryCount;
}
}

View file

@ -1,7 +1,7 @@
<?php <?php
namespace Szurubooru\Tests; namespace Szurubooru\Tests;
final class DispatcherTest extends \Szurubooru\Tests\AbstractTestCase final class DispatcherTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
{ {
private $routerMock; private $routerMock;
private $httpHelperMock; private $httpHelperMock;
@ -34,6 +34,7 @@ final class DispatcherTest extends \Szurubooru\Tests\AbstractTestCase
$actual = $dispatcher->run('GET', '/'); $actual = $dispatcher->run('GET', '/');
unset($actual['__time']); unset($actual['__time']);
unset($actual['__queries']);
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
@ -50,6 +51,7 @@ final class DispatcherTest extends \Szurubooru\Tests\AbstractTestCase
$actual = $dispatcher->run('GET', '/'); $actual = $dispatcher->run('GET', '/');
unset($actual['__time']); unset($actual['__time']);
unset($actual['__queries']);
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
@ -67,6 +69,7 @@ final class DispatcherTest extends \Szurubooru\Tests\AbstractTestCase
{ {
return new \Szurubooru\Dispatcher( return new \Szurubooru\Dispatcher(
$this->routerMock, $this->routerMock,
$this->databaseConnection,
$this->httpHelperMock, $this->httpHelperMock,
$this->authServiceMock, $this->authServiceMock,
$this->tokenServiceMock, $this->tokenServiceMock,