|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Patchlevel\EventSourcing\Tests\Benchmark; |
| 6 | + |
| 7 | +use Patchlevel\EventSourcing\Aggregate\AggregateRootId; |
| 8 | +use Patchlevel\EventSourcing\Message\Message; |
| 9 | +use Patchlevel\EventSourcing\Repository\DefaultRepository; |
| 10 | +use Patchlevel\EventSourcing\Repository\Repository; |
| 11 | +use Patchlevel\EventSourcing\Schema\DoctrineSchemaDirector; |
| 12 | +use Patchlevel\EventSourcing\Serializer\DefaultEventSerializer; |
| 13 | +use Patchlevel\EventSourcing\Store\TaggableDoctrineDbalStore; |
| 14 | +use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Events\ProfileCreated; |
| 15 | +use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Profile; |
| 16 | +use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\ProfileId; |
| 17 | +use Patchlevel\EventSourcing\Tests\DbalManager; |
| 18 | +use PhpBench\Attributes as Bench; |
| 19 | + |
| 20 | +#[Bench\BeforeMethods('setUp')] |
| 21 | +final class SimpleSetupTaggableStoreBench |
| 22 | +{ |
| 23 | + private TaggableDoctrineDbalStore $store; |
| 24 | + private Repository $repository; |
| 25 | + |
| 26 | + private AggregateRootId $singleEventId; |
| 27 | + private AggregateRootId $multipleEventsId; |
| 28 | + |
| 29 | + public function setUp(): void |
| 30 | + { |
| 31 | + $connection = DbalManager::createConnection(); |
| 32 | + |
| 33 | + $this->store = new TaggableDoctrineDbalStore( |
| 34 | + $connection, |
| 35 | + DefaultEventSerializer::createFromPaths([__DIR__ . '/BasicImplementation/Events']), |
| 36 | + ); |
| 37 | + |
| 38 | + $this->repository = new DefaultRepository($this->store, Profile::metadata()); |
| 39 | + |
| 40 | + $schemaDirector = new DoctrineSchemaDirector( |
| 41 | + $connection, |
| 42 | + $this->store, |
| 43 | + ); |
| 44 | + |
| 45 | + $schemaDirector->create(); |
| 46 | + |
| 47 | + $this->singleEventId = ProfileId::generate(); |
| 48 | + $profile = Profile::create($this->singleEventId, 'Peter'); |
| 49 | + $this->repository->save($profile); |
| 50 | + |
| 51 | + $this->multipleEventsId = ProfileId::generate(); |
| 52 | + $profile = Profile::create($this->multipleEventsId, 'Peter'); |
| 53 | + |
| 54 | + for ($i = 0; $i < 10_000; $i++) { |
| 55 | + $profile->changeName('Peter'); |
| 56 | + } |
| 57 | + |
| 58 | + $this->repository->save($profile); |
| 59 | + } |
| 60 | + |
| 61 | + #[Bench\Revs(10)] |
| 62 | + public function benchLoad1Event(): void |
| 63 | + { |
| 64 | + $this->repository->load($this->singleEventId); |
| 65 | + } |
| 66 | + |
| 67 | + #[Bench\Revs(10)] |
| 68 | + public function benchLoad10000Events(): void |
| 69 | + { |
| 70 | + $this->repository->load($this->multipleEventsId); |
| 71 | + } |
| 72 | + |
| 73 | + #[Bench\Revs(10)] |
| 74 | + public function benchSave1Event(): void |
| 75 | + { |
| 76 | + $profile = Profile::create(ProfileId::generate(), 'Peter'); |
| 77 | + $this->repository->save($profile); |
| 78 | + } |
| 79 | + |
| 80 | + #[Bench\Revs(10)] |
| 81 | + public function benchSave10000Events(): void |
| 82 | + { |
| 83 | + $profile = Profile::create(ProfileId::generate(), 'Peter'); |
| 84 | + |
| 85 | + for ($i = 1; $i < 10_000; $i++) { |
| 86 | + $profile->changeName('Peter'); |
| 87 | + } |
| 88 | + |
| 89 | + $this->repository->save($profile); |
| 90 | + } |
| 91 | + |
| 92 | + #[Bench\Revs(1)] |
| 93 | + public function benchSave10000Aggregates(): void |
| 94 | + { |
| 95 | + for ($i = 1; $i < 10_000; $i++) { |
| 96 | + $profile = Profile::create(ProfileId::generate(), 'Peter'); |
| 97 | + $this->repository->save($profile); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + #[Bench\Revs(10)] |
| 102 | + public function benchSave10000AggregatesTransaction(): void |
| 103 | + { |
| 104 | + $this->store->transactional(function (): void { |
| 105 | + for ($i = 1; $i < 10_000; $i++) { |
| 106 | + $profile = Profile::create(ProfileId::generate(), 'Peter'); |
| 107 | + $this->repository->save($profile); |
| 108 | + } |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + #[Bench\Revs(10)] |
| 113 | + public function benchAppend1Event(): void |
| 114 | + { |
| 115 | + $messages = [ |
| 116 | + Message::create( |
| 117 | + new ProfileCreated( |
| 118 | + ProfileId::generate(), |
| 119 | + 'Peter', |
| 120 | + null, |
| 121 | + ) |
| 122 | + ) |
| 123 | + ]; |
| 124 | + |
| 125 | + $this->store->append($messages); |
| 126 | + } |
| 127 | + |
| 128 | + #[Bench\Revs(10)] |
| 129 | + public function benchAppend10000Events(): void |
| 130 | + { |
| 131 | + $messages = []; |
| 132 | + |
| 133 | + for ($i = 1; $i < 10_000; $i++) { |
| 134 | + $messages[] = Message::create( |
| 135 | + new ProfileCreated( |
| 136 | + ProfileId::generate(), |
| 137 | + 'Peter', |
| 138 | + null, |
| 139 | + ) |
| 140 | + ); |
| 141 | + } |
| 142 | + |
| 143 | + $this->store->append($messages); |
| 144 | + } |
| 145 | +} |
0 commit comments