1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
<?php
declare(strict_types=1);
namespace Doctrine\Performance\Query;
use DateTime;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Query;
use Doctrine\Performance\EntityManagerFactory;
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
use function range;
/** @BeforeMethods({"init"}) */
final class QueryBoundParameterProcessingBench
{
private Query|null $parsedQueryWithInferredParameterType = null;
private Query|null $parsedQueryWithDeclaredParameterType = null;
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(), Types::DATETIME_MUTABLE);
}
// 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();
}
}
|