-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathCommandToQueryBench.php
More file actions
130 lines (107 loc) · 5.08 KB
/
Copy pathCommandToQueryBench.php
File metadata and controls
130 lines (107 loc) · 5.08 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
declare(strict_types=1);
namespace Benchmark;
use Patchlevel\EventSourcing\Clock\SystemClock;
use Patchlevel\EventSourcing\CommandBus\CommandBus;
use Patchlevel\EventSourcing\CommandBus\ServiceLocator;
use Patchlevel\EventSourcing\CommandBus\SyncCommandBus;
use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootRegistry;
use Patchlevel\EventSourcing\QueryBus\QueryBus;
use Patchlevel\EventSourcing\QueryBus\ServiceHandlerProvider;
use Patchlevel\EventSourcing\QueryBus\SyncQueryBus;
use Patchlevel\EventSourcing\Repository\DefaultRepositoryManager;
use Patchlevel\EventSourcing\Repository\StoreAdapter\StreamDoctrineDbalStoreAdapter;
use Patchlevel\EventSourcing\Schema\DoctrineSchemaDirector;
use Patchlevel\EventSourcing\Serializer\DefaultEventSerializer;
use Patchlevel\EventSourcing\Snapshot\Adapter\InMemorySnapshotAdapter;
use Patchlevel\EventSourcing\Snapshot\DefaultSnapshotStore;
use Patchlevel\EventSourcing\Store\StreamDoctrineDbalStore;
use Patchlevel\EventSourcing\Subscription\Engine\DefaultSubscriptionEngine;
use Patchlevel\EventSourcing\Subscription\Engine\StoreMessageLoader;
use Patchlevel\EventSourcing\Subscription\Repository\RunSubscriptionEngineRepositoryManager;
use Patchlevel\EventSourcing\Subscription\Store\InMemorySubscriptionStore;
use Patchlevel\EventSourcing\Subscription\Subscriber\MetadataSubscriberAccessorRepository;
use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Command\ChangeProfileName;
use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Command\CreateProfile;
use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Processor\SendEmailProcessor;
use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\ProfileId;
use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\ProfileWithCommands;
use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Projection\ProfileProjector;
use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Query\QueryProfileName;
use Patchlevel\EventSourcing\Tests\DbalManager;
use PhpBench\Attributes as Bench;
use Psr\Clock\ClockInterface;
use function assert;
#[Bench\BeforeMethods('setUp')]
final class CommandToQueryBench
{
private CommandBus $commandBus;
private QueryBus $queryBus;
private ProfileId $updateId;
public function setUp(): void
{
$connection = DbalManager::createConnection();
$store = new StreamDoctrineDbalStore(
$connection,
DefaultEventSerializer::createFromPaths([__DIR__ . '/BasicImplementation/Events']),
);
$aggregateRootRegistry = new AggregateRootRegistry(['profile_with_commands' => ProfileWithCommands::class]);
$manager = new DefaultRepositoryManager(
$aggregateRootRegistry,
new StreamDoctrineDbalStoreAdapter($store),
null,
new DefaultSnapshotStore(['default' => new InMemorySnapshotAdapter()]),
);
$projectionConnection = DbalManager::createConnection();
$profileProjection = new ProfileProjector($projectionConnection);
$engine = new DefaultSubscriptionEngine(
new StoreMessageLoader($store),
new InMemorySubscriptionStore(),
new MetadataSubscriberAccessorRepository([
$profileProjection,
new SendEmailProcessor(),
]),
);
$manager = new RunSubscriptionEngineRepositoryManager($manager, $engine);
$this->commandBus = SyncCommandBus::createForAggregateHandlers(
$aggregateRootRegistry,
$manager,
new ServiceLocator([
ClockInterface::class => new SystemClock(),
'env' => 'test',
]),
);
$this->queryBus = new SyncQueryBus(new ServiceHandlerProvider([$profileProjection]));
$schemaDirector = new DoctrineSchemaDirector($connection, $store);
$schemaDirector->create();
$engine->setup(skipBooting: true);
$this->updateId = ProfileId::generate();
$this->commandBus->dispatch(new CreateProfile($this->updateId, 'Peter'));
}
#[Bench\Revs(10)]
public function benchCreate(): void
{
$id = ProfileId::generate();
$this->commandBus->dispatch(new CreateProfile($id, 'James'));
$result = $this->queryBus->dispatch(new QueryProfileName($id));
assert($result === 'James');
}
#[Bench\Revs(10)]
public function benchUpdate(): void
{
$this->commandBus->dispatch(new ChangeProfileName($this->updateId, 'James Doe'));
$result = $this->queryBus->dispatch(new QueryProfileName($this->updateId));
assert($result === 'James Doe');
}
#[Bench\Revs(10)]
public function benchBoth(): void
{
$id = ProfileId::generate();
$this->commandBus->dispatch(new CreateProfile($id, 'James'));
$result = $this->queryBus->dispatch(new QueryProfileName($id));
assert($result === 'James');
$this->commandBus->dispatch(new ChangeProfileName($id, 'James Doe'));
$result = $this->queryBus->dispatch(new QueryProfileName($id));
assert($result === 'James Doe');
}
}