Skip to content

Commit

Permalink
Merge pull request #6575 from lcobucci/improvement/move-performance-t…
Browse files Browse the repository at this point in the history
…ests-to-phpbench

Move performance tests to phpbench
  • Loading branch information
lcobucci authored Jul 23, 2017
2 parents 9826d9c + e07c90d commit 8407957
Show file tree
Hide file tree
Showing 30 changed files with 1,595 additions and 951 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ lib/Doctrine/DBAL
.idea
vendor/
composer.lock
/tests/Doctrine/Performance/history.db
18 changes: 15 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ before_script:
- if [[ $PHPSTAN = 1 ]]; then composer require --dev --prefer-stable phpstan/phpstan:^0.7 symfony/console:^3.0; fi
- if [ "$MYSQL_VERSION" == "5.7" ]; then bash ./tests/travis/install-mysql-5.7.sh; fi;
- if [[ $DB == "mysql" || $DB == "mariadb" ]]; then mysql -e "CREATE SCHEMA doctrine_tests; GRANT ALL PRIVILEGES ON doctrine_tests.* to travis@'%'"; fi;
- if [[ $PHPBENCH = 1 ]]; then wget https://phpbench.github.io/phpbench/phpbench.phar https://phpbench.github.io/phpbench/phpbench.phar.pubkey; fi

script:
- if [[ $PHPSTAN = 1 ]]; then vendor/bin/phpstan analyse -l 1 -c phpstan.neon lib; fi
- ENABLE_SECOND_LEVEL_CACHE=0 ./vendor/bin/phpunit -v -c tests/travis/$DB.travis.xml $PHPUNIT_FLAGS
- ENABLE_SECOND_LEVEL_CACHE=1 ./vendor/bin/phpunit -v -c tests/travis/$DB.travis.xml --exclude-group performance,non-cacheable,locking_functional
- if [[ $PHPBENCH = 1 ]]; then php phpbench.phar run -l dots --report=default; fi
- if [[ $DB != "none" ]]; then ENABLE_SECOND_LEVEL_CACHE=0 ./vendor/bin/phpunit -v -c tests/travis/$DB.travis.xml $PHPUNIT_FLAGS; fi
- if [[ $DB != "none" ]]; then ENABLE_SECOND_LEVEL_CACHE=1 ./vendor/bin/phpunit -v -c tests/travis/$DB.travis.xml --exclude-group performance,non-cacheable,locking_functional; fi

after_script:
- if [[ "$PHPUNIT_FLAGS" != "" ]]; then wget https://scrutinizer-ci.com/ocular.phar; fi
Expand All @@ -39,14 +41,15 @@ matrix:
env: DB=mariadb
addons:
mariadb: 10.1

- php: 7.1
env:
- DB=sqlite
- DEPENDENCIES='low'

- php: 7.1
env:
- DB=pgsql
- PHPSTAN=1

- php: 7.1
env: DB=mysql MYSQL_VERSION=5.7
Expand All @@ -55,6 +58,15 @@ matrix:
env: DB=mysql MYSQL_VERSION=5.7
sudo: required

- php: 7.1
env:
- DB=none
- PHPSTAN=1
- php: 7.1
env:
- DB=none
- PHPBENCH=1

allow_failures:
- php: nightly

Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@
"psr-4": { "Doctrine\\ORM\\": "lib/Doctrine/ORM" }
},
"autoload-dev": {
"psr-4": { "Doctrine\\Tests\\": "tests/Doctrine/Tests" }
"psr-4": {
"Doctrine\\Tests\\": "tests/Doctrine/Tests",
"Doctrine\\Performance\\": "tests/Doctrine/Performance"
}
},
"bin": ["bin/doctrine"],
"extra": {
Expand Down
15 changes: 15 additions & 0 deletions phpbench.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"bootstrap": "vendor/autoload.php",
"path": "tests/Doctrine/Performance",

"extensions": [
"PhpBench\\Extensions\\Dbal\\DbalExtension",
"PhpBench\\Extensions\\XDebug\\XDebugExtension"
],

"storage": "dbal",
"storage.dbal.connection": {
"driver": "pdo_sqlite",
"path": "tests/Doctrine/Performance/history.db"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Doctrine\Performance\ChangeSet;

use Doctrine\ORM\Query;
use Doctrine\ORM\UnitOfWork;
use Doctrine\Performance\EntityManagerFactory;
use Doctrine\Tests\Mocks\HydratorMockStatement;
use Doctrine\Tests\Models\CMS\CmsUser;
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;

/**
* @BeforeMethods({"init"})
*/
final class UnitOfWorkComputeChangesBench
{
/**
* @var CmsUser[]
*/
private $users;

/**
* @var UnitOfWork
*/
private $unitOfWork;

public function init()
{
$this->unitOfWork = EntityManagerFactory::getEntityManager([])->getUnitOfWork();

for ($i = 1; $i <= 100; ++$i) {
$user = new CmsUser;
$user->id = $i;
$user->status = 'user';
$user->username = 'user' . $i;
$user->name = 'Mr.Smith-' . $i;
$this->users[] = $user;

$this->unitOfWork->registerManaged(
$user,
[
'id' => $i,
],
[
'id' => $user->id,
'status' => $user->status,
'username' => $user->username,
'name' => $user->name,
'address' => $user->address,
'email' => $user->email,
]
);
}

$this->unitOfWork->computeChangeSets();

if ($this->unitOfWork->getScheduledEntityUpdates()) {
throw new \LogicException('Unit of work should be clean at this stage');
}

foreach ($this->users AS $user) {
$user->status = 'other';
$user->username .= '++';
$user->name = str_replace('Mr.', 'Mrs.', $user->name);
}
}

public function benchChangeSetComputation()
{
$this->unitOfWork->computeChangeSets();
}
}

39 changes: 39 additions & 0 deletions tests/Doctrine/Performance/EntityManagerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Doctrine\Performance;

use Doctrine\DBAL\Driver\PDOSqlite\Driver;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Proxy\ProxyFactory;
use Doctrine\ORM\Tools\SchemaTool;

final class EntityManagerFactory
{
public static function getEntityManager(array $schemaClassNames) : 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/GeoNames')
], true));

$entityManager = EntityManager::create(
[
'driverClass' => Driver::class,
'memory' => true,
],
$config
);

(new SchemaTool($entityManager))
->createSchema(array_map([$entityManager, 'getClassMetadata'], $schemaClassNames));

return $entityManager;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Doctrine\Performance\Hydration;

use Doctrine\ORM\Internal\Hydration\ArrayHydrator;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\Performance\EntityManagerFactory;
use Doctrine\Tests\Mocks\HydratorMockStatement;
use Doctrine\Tests\Models\CMS\CmsPhonenumber;
use Doctrine\Tests\Models\CMS\CmsUser;
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;

/**
* @BeforeMethods({"init"})
*/
final class MixedQueryFetchJoinArrayHydrationPerformanceBench
{
/**
* @var ArrayHydrator
*/
private $hydrator;

/**
* @var ResultSetMapping
*/
private $rsm;

/**
* @var HydratorMockStatement
*/
private $stmt;

public function init()
{
$resultSet = [
[
'u__id' => '1',
'u__status' => 'developer',
'u__username' => 'romanb',
'u__name' => 'Roman',
'sclr0' => 'ROMANB',
'p__phonenumber' => '42',
],
[
'u__id' => '1',
'u__status' => 'developer',
'u__username' => 'romanb',
'u__name' => 'Roman',
'sclr0' => 'ROMANB',
'p__phonenumber' => '43',
],
[
'u__id' => '2',
'u__status' => 'developer',
'u__username' => 'romanb',
'u__name' => 'Roman',
'sclr0' => 'JWAGE',
'p__phonenumber' => '91'
]
];

for ($i = 4; $i < 10000; ++$i) {
$resultSet[] = [
'u__id' => $i,
'u__status' => 'developer',
'u__username' => 'jwage',
'u__name' => 'Jonathan',
'sclr0' => 'JWAGE' . $i,
'p__phonenumber' => '91'
];
}

$this->stmt = new HydratorMockStatement($resultSet);
$this->hydrator = new ArrayHydrator(EntityManagerFactory::getEntityManager([]));
$this->rsm = new ResultSetMapping;

$this->rsm->addEntityResult(CmsUser::class, 'u');
$this->rsm->addJoinedEntityResult(CmsPhonenumber::class, 'p', 'u', 'phonenumbers');
$this->rsm->addFieldResult('u', 'u__id', 'id');
$this->rsm->addFieldResult('u', 'u__status', 'status');
$this->rsm->addFieldResult('u', 'u__username', 'username');
$this->rsm->addFieldResult('u', 'u__name', 'name');
$this->rsm->addScalarResult('sclr0', 'nameUpper');
$this->rsm->addFieldResult('p', 'p__phonenumber', 'phonenumber');
}

public function benchHydration()
{
$this->hydrator->hydrateAll($this->stmt, $this->rsm);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Doctrine\Performance\Hydration;

use Doctrine\ORM\Internal\Hydration\ObjectHydrator;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\Performance\EntityManagerFactory;
use Doctrine\Tests\Mocks\HydratorMockStatement;
use Doctrine\Tests\Models\CMS\CmsAddress;
use Doctrine\Tests\Models\CMS\CmsPhonenumber;
use Doctrine\Tests\Models\CMS\CmsUser;
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;

/**
* @BeforeMethods({"init"})
*/
final class MixedQueryFetchJoinFullObjectHydrationPerformanceBench
{
/**
* @var ObjectHydrator
*/
private $hydrator;

/**
* @var ResultSetMapping
*/
private $rsm;

/**
* @var HydratorMockStatement
*/
private $stmt;

public function init()
{
$resultSet = [
[
'u__id' => '1',
'u__status' => 'developer',
'u__username' => 'romanb',
'u__name' => 'Roman',
'sclr0' => 'ROMANB',
'p__phonenumber' => '42',
'a__id' => '1'
]
];

for ($i = 2; $i < 2000; ++$i) {
$resultSet[] = [
'u__id' => $i,
'u__status' => 'developer',
'u__username' => 'jwage',
'u__name' => 'Jonathan',
'sclr0' => 'JWAGE' . $i,
'p__phonenumber' => '91',
'a__id' => $i
];
}

$this->stmt = new HydratorMockStatement($resultSet);
$this->hydrator = new ObjectHydrator(EntityManagerFactory::getEntityManager([]));
$this->rsm = new ResultSetMapping;

$this->rsm->addEntityResult(CmsUser::class, 'u');
$this->rsm->addJoinedEntityResult(CmsPhonenumber::class, 'p', 'u', 'phonenumbers');
$this->rsm->addFieldResult('u', 'u__id', 'id');
$this->rsm->addFieldResult('u', 'u__status', 'status');
$this->rsm->addFieldResult('u', 'u__username', 'username');
$this->rsm->addFieldResult('u', 'u__name', 'name');
$this->rsm->addScalarResult('sclr0', 'nameUpper');
$this->rsm->addFieldResult('p', 'p__phonenumber', 'phonenumber');
$this->rsm->addJoinedEntityResult(CmsAddress::class, 'a', 'u', 'address');
$this->rsm->addFieldResult('a', 'a__id', 'id');
}

public function benchHydration()
{
$this->hydrator->hydrateAll($this->stmt, $this->rsm);
}
}

Loading

0 comments on commit 8407957

Please sign in to comment.