Skip to content

Commit

Permalink
add split stream benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Dec 13, 2023
1 parent c5fcee7 commit 9979b80
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 2 deletions.
16 changes: 16 additions & 0 deletions tests/Benchmark/BasicImplementation/Aggregate/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Patchlevel\EventSourcing\Attribute\Snapshot;
use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Events\NameChanged;
use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Events\ProfileCreated;
use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Events\Reborn;
use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Normalizer\ProfileIdNormalizer;
use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\ProfileId;

Expand Down Expand Up @@ -39,6 +40,14 @@ public function changeName(string $name): void
$this->recordThat(new NameChanged($name));
}

public function reborn(): void
{
$this->recordThat(new Reborn(
$this->id,
$this->name
));
}

#[Apply]
protected function applyProfileCreated(ProfileCreated $event): void
{
Expand All @@ -52,6 +61,13 @@ protected function applyNameChanged(NameChanged $event): void
$this->name = $event->name;
}

#[Apply]
protected function applyReborn(Reborn $event): void
{
$this->id = $event->profileId;
$this->name = $event->name;
}

public function name(): string
{
return $this->name;
Expand Down
22 changes: 22 additions & 0 deletions tests/Benchmark/BasicImplementation/Events/Reborn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Events;

use Patchlevel\EventSourcing\Attribute\Event;
use Patchlevel\EventSourcing\Attribute\SplitStream;
use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Normalizer\ProfileIdNormalizer;
use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\ProfileId;

#[Event('profile.reborn')]
#[SplitStream]
final class Reborn
{
public function __construct(
#[ProfileIdNormalizer]
public ProfileId $profileId,
public string $name,
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use function unlink;

#[Bench\BeforeMethods('setUp')]
final class LoadEventsWithSnapshotsBench
final class SnapshotsBench
{
private const DB_PATH = __DIR__ . '/BasicImplementation/data/db.sqlite3';

Expand Down
115 changes: 115 additions & 0 deletions tests/Benchmark/SplitStreamBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Benchmark;

use Doctrine\DBAL\Driver\PDO\SQLite\Driver;
use Doctrine\DBAL\DriverManager;
use Patchlevel\EventSourcing\EventBus\Decorator\SplitStreamDecorator;
use Patchlevel\EventSourcing\EventBus\DefaultEventBus;
use Patchlevel\EventSourcing\EventBus\EventBus;
use Patchlevel\EventSourcing\Metadata\AggregateRoot\AttributeAggregateRootRegistryFactory;
use Patchlevel\EventSourcing\Metadata\Event\AttributeEventMetadataFactory;
use Patchlevel\EventSourcing\Repository\DefaultRepository;
use Patchlevel\EventSourcing\Repository\Repository;
use Patchlevel\EventSourcing\Schema\DoctrineSchemaDirector;
use Patchlevel\EventSourcing\Serializer\DefaultEventSerializer;
use Patchlevel\EventSourcing\Snapshot\Adapter\InMemorySnapshotAdapter;
use Patchlevel\EventSourcing\Snapshot\DefaultSnapshotStore;
use Patchlevel\EventSourcing\Snapshot\SnapshotStore;
use Patchlevel\EventSourcing\Store\DoctrineDbalStore;
use Patchlevel\EventSourcing\Store\Store;
use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Aggregate\Profile;
use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\ProfileId;
use PhpBench\Attributes as Bench;

use function file_exists;
use function unlink;

#[Bench\BeforeMethods('setUp')]
final class SplitStreamBench
{
private const DB_PATH = __DIR__ . '/BasicImplementation/data/db.sqlite3';

private Store $store;

Check failure on line 35 in tests/Benchmark/SplitStreamBench.php

View workflow job for this annotation

GitHub Actions / Static Analysis by Psalm (locked, 8.3, ubuntu-latest)

MissingConstructor

tests/Benchmark/SplitStreamBench.php:35:19: MissingConstructor: Patchlevel\EventSourcing\Tests\Benchmark\SplitStreamBench has an uninitialized property Patchlevel\EventSourcing\Tests\Benchmark\SplitStreamBench::$store, but no constructor (see https://psalm.dev/073)
private EventBus $bus;

Check failure on line 36 in tests/Benchmark/SplitStreamBench.php

View workflow job for this annotation

GitHub Actions / Static Analysis by Psalm (locked, 8.3, ubuntu-latest)

MissingConstructor

tests/Benchmark/SplitStreamBench.php:36:22: MissingConstructor: Patchlevel\EventSourcing\Tests\Benchmark\SplitStreamBench has an uninitialized property Patchlevel\EventSourcing\Tests\Benchmark\SplitStreamBench::$bus, but no constructor (see https://psalm.dev/073)
private SnapshotStore $snapshotStore;

Check failure on line 37 in tests/Benchmark/SplitStreamBench.php

View workflow job for this annotation

GitHub Actions / Static Analysis by Psalm (locked, 8.3, ubuntu-latest)

MissingConstructor

tests/Benchmark/SplitStreamBench.php:37:27: MissingConstructor: Patchlevel\EventSourcing\Tests\Benchmark\SplitStreamBench has an uninitialized property Patchlevel\EventSourcing\Tests\Benchmark\SplitStreamBench::$snapshotStore, but no constructor (see https://psalm.dev/073)
private Repository $repository;

Check failure on line 38 in tests/Benchmark/SplitStreamBench.php

View workflow job for this annotation

GitHub Actions / Static Analysis by Psalm (locked, 8.3, ubuntu-latest)

MissingConstructor

tests/Benchmark/SplitStreamBench.php:38:24: MissingConstructor: Patchlevel\EventSourcing\Tests\Benchmark\SplitStreamBench has an uninitialized property Patchlevel\EventSourcing\Tests\Benchmark\SplitStreamBench::$repository, but no constructor (see https://psalm.dev/073)

public function setUp(): void
{
if (file_exists(self::DB_PATH)) {
unlink(self::DB_PATH);
}

$connection = DriverManager::getConnection([
'driverClass' => Driver::class,
'path' => self::DB_PATH,
]);

$this->bus = new DefaultEventBus();

$this->store = new DoctrineDbalStore(
$connection,
DefaultEventSerializer::createFromPaths([__DIR__ . '/BasicImplementation/Events']),
(new AttributeAggregateRootRegistryFactory())->create([__DIR__ . '/BasicImplementation/Aggregate']),
'eventstore',
);

$this->repository = new DefaultRepository(
$this->store,
$this->bus,
Profile::metadata(),
null,
new SplitStreamDecorator(
new AttributeEventMetadataFactory()
)
);

$schemaDirector = new DoctrineSchemaDirector(
$connection,
$this->store,
);

$schemaDirector->create();
}

public function provideData(): void
{
$profile = Profile::create(ProfileId::fromString('1'), 'Peter');

for ($i = 0; $i < 10_000; $i++) {
$profile->changeName(sprintf('Peter %d', $i));

if ($i % 100 === 0) {
$profile->reborn();
}
}

$this->repository->save($profile);
}

#[Bench\Revs(20)]
#[Bench\BeforeMethods("provideData")]
public function benchLoad10000Events(): void
{
$this->repository->load('1');
}

#[Bench\Revs(20)]
public function benchWrite10000Events(): void
{
$profile = Profile::create(ProfileId::fromString('1'), 'Peter');

for ($i = 0; $i < 10_000; $i++) {
$profile->changeName(sprintf('Peter %d', $i));

if ($i % 100 === 0) {
$profile->reborn();
}
}

$this->repository->save($profile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
use function unlink;

#[Bench\BeforeMethods('setUp')]
final class WriteEventsWithSyncProjectionistBench
final class SyncProjectionistBench
{
private const DB_PATH = __DIR__ . '/BasicImplementation/data/db.sqlite3';

Expand Down

0 comments on commit 9979b80

Please sign in to comment.