Skip to content

Commit

Permalink
Merge pull request #3 from Ocramius/fix/#7527-query-parameter-binding…
Browse files Browse the repository at this point in the history
…-benchmarks

#7527 performance benchmark - verifying performance impact of inferred query parameter types
  • Loading branch information
Ocramius authored Dec 19, 2018
2 parents 23af164 + ca436f0 commit 1c95fb7
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
31 changes: 30 additions & 1 deletion tests/Doctrine/Performance/EntityManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Doctrine\Performance;

use Doctrine\Common\EventManager;
use Doctrine\DBAL\Cache\ArrayStatement;
use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\PDOSqlite\Driver;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
Expand All @@ -20,7 +24,7 @@ public static function getEntityManager(array $schemaClassNames) : EntityManager
$config->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_EVAL);
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver([
realpath(__DIR__ . '/Models/Cache'),
realpath(__DIR__ . '/Models/GeoNames')
realpath(__DIR__ . '/Models/GeoNames'),
], true));

$entityManager = EntityManager::create(
Expand All @@ -36,4 +40,29 @@ public static function getEntityManager(array $schemaClassNames) : EntityManager

return $entityManager;
}

public static function makeEntityManagerWithNoResultsConnection() : EntityManagerInterface
{
$config = new Configuration();

$config->setProxyDir(__DIR__ . '/../Tests/Proxies');
$config->setProxyNamespace('Doctrine\Tests\Proxies');
$config->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_EVAL);
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver([
realpath(__DIR__ . '/Models/Cache'),
realpath(__DIR__ . '/Models/Generic'),
realpath(__DIR__ . '/Models/GeoNames'),
], true));

// A connection that doesn't really do anything
$connection = new class ([], new Driver(), null, new EventManager()) extends Connection
{
public function executeQuery($query, array $params = [], $types = [], QueryCacheProfile $qcp = null)
{
return new ArrayStatement([]);
}
};

return EntityManager::create($connection, $config);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace Doctrine\Performance\Query;

use DateTime;
use Doctrine\DBAL\Types\DateTimeType;
use Doctrine\ORM\Query;
use Doctrine\Performance\EntityManagerFactory;
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
use function range;

/**
* @BeforeMethods({"init"})
*/
final class QueryBoundParameterProcessingBench
{
/** @var Query */
private $parsedQueryWithInferredParameterType;

/** @var Query */
private $parsedQueryWithDeclaredParameterType;

public function init() : void
{
$entityManager = EntityManagerFactory::makeEntityManagerWithNoResultsConnection();

// Note: binding a lot of parameters because DQL operations are noisy due to hydrators and other components
// kicking in, so we make the parameter operations more noticeable.
$dql = <<<'DQL'
SELECT e
FROM Doctrine\Tests\Models\Generic\DateTimeModel e
WHERE
e.datetime = :parameter1
OR
e.datetime = :parameter2
OR
e.datetime = :parameter3
OR
e.datetime = :parameter4
OR
e.datetime = :parameter5
OR
e.datetime = :parameter6
OR
e.datetime = :parameter7
OR
e.datetime = :parameter8
OR
e.datetime = :parameter9
OR
e.datetime = :parameter10
DQL;

$this->parsedQueryWithInferredParameterType = $entityManager->createQuery($dql);
$this->parsedQueryWithDeclaredParameterType = $entityManager->createQuery($dql);

foreach (range(1, 10) as $index) {
$this->parsedQueryWithInferredParameterType->setParameter('parameter' . $index, new DateTime());
$this->parsedQueryWithDeclaredParameterType->setParameter('parameter' . $index, new DateTime(), DateTimeType::DATETIME);
}

// Force parsing upfront - we don't benchmark that bit in this scenario
$this->parsedQueryWithInferredParameterType->getSQL();
$this->parsedQueryWithDeclaredParameterType->getSQL();
}

public function benchExecuteParsedQueryWithInferredParameterType() : void
{
$this->parsedQueryWithInferredParameterType->execute();
}

public function benchExecuteParsedQueryWithDeclaredParameterType() : void
{
$this->parsedQueryWithDeclaredParameterType->execute();
}
}

0 comments on commit 1c95fb7

Please sign in to comment.