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
|
<?php
declare(strict_types=1);
namespace Doctrine\Performance\ChangeSet;
use Doctrine\ORM\UnitOfWork;
use Doctrine\Performance\EntityManagerFactory;
use Doctrine\Tests\Models\CMS\CmsUser;
use LogicException;
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
use function str_replace;
/** @BeforeMethods({"init"}) */
final class UnitOfWorkComputeChangesBench
{
/** @var CmsUser[] */
private array|null $users = null;
private UnitOfWork|null $unitOfWork = null;
public function init(): void
{
$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(): void
{
$this->unitOfWork->computeChangeSets();
}
}
|