Skip to content

Commit 884b646

Browse files
committed
add benchmark
1 parent 9b46c9d commit 884b646

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed

tests/Benchmark/BasicImplementation/Events/ProfileCreated.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Events;
66

77
use Patchlevel\EventSourcing\Attribute\Event;
8+
use Patchlevel\EventSourcing\Attribute\EventTag;
89
use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\ProfileId;
910
use Patchlevel\Hydrator\Attribute\DataSubjectId;
1011
use Patchlevel\Hydrator\Attribute\PersonalData;
@@ -14,6 +15,7 @@ final class ProfileCreated
1415
{
1516
public function __construct(
1617
#[DataSubjectId]
18+
#[EventTag(prefix: 'profile')]
1719
public ProfileId $profileId,
1820
public string $name,
1921
#[PersonalData]
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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 benchAppend100Events(): void
130+
{
131+
$messages = [];
132+
133+
for ($i = 1; $i < 100; $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+
}

tests/DbalManager.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Doctrine\DBAL\Connection;
88
use Doctrine\DBAL\Driver\AbstractSQLiteDriver;
99
use Doctrine\DBAL\DriverManager;
10+
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
1011
use Doctrine\DBAL\Tools\DsnParser;
1112
use Patchlevel\EventSourcing\Console\DoctrineHelper;
1213
use RuntimeException;
@@ -45,6 +46,14 @@ public static function createConnection(string $dbName = self::DEFAULT_DB_NAME):
4546
$databases = $schemaManager->listDatabases();
4647

4748
if (in_array($dbName, $databases, true)) {
49+
if ($tempConnection->getDatabasePlatform() instanceof PostgreSQLPlatform) {
50+
$tempConnection->executeStatement("
51+
SELECT pg_terminate_backend(pid)
52+
FROM pg_stat_activity
53+
WHERE datname = '{$dbName}';
54+
");
55+
}
56+
4857
$schemaManager->dropDatabase($dbName);
4958
}
5059

0 commit comments

Comments
 (0)