Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

command bus #644

Draft
wants to merge 19 commits into
base: 3.7.x
Choose a base branch
from
Draft

command bus #644

wants to merge 19 commits into from

Conversation

DavidBadura
Copy link
Member

@DavidBadura DavidBadura commented Nov 8, 2024

use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot;
use Patchlevel\EventSourcing\Attribute\Aggregate;
use Patchlevel\EventSourcing\Attribute\Apply;
use Patchlevel\EventSourcing\Attribute\Handle;
use Patchlevel\EventSourcing\Attribute\Id;

#[Aggregate('profile')]
final class Profile extends BasicAggregateRoot
{
    #[Id]
    private ProfileId $id;
    private string $name;

    #[Handle]
    public static function create(CreateProfile $command, NameValidator $nameValidator): self
    {
        $self = new self();
 
        if (!$nameValidator($command->name) {
            throw new InvalidArgument();
        }

        $self->recordThat(new ProfileCreated($command->id, $command->name));

        return $self;
    }

    #[Handle]
    public function changeName(ChangeProfileName $command, NameValidator $nameValidator): void
    {
        if (!$nameValidator($command->name) {
            throw new InvalidArgument();
        }

        $this->recordThat(new NameChanged($command->name));
    }

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

    #[Apply]
    protected function applyNameChanged(NameChanged $event): void
    {
        $this->name = $event->name;
    }

    public function name(): string
    {
        return $this->name;
    }
}
final class CreateProfile
{
    public function __construct(
        public readonly ProfileId $id,
        public readonly string $name,
    ) {
    }
}
use Patchlevel\EventSourcing\Attribute\Id;

final class ChangeProfileName
{
    public function __construct(
        #[Id]
        public readonly ProfileId $id,
        public readonly string $name,
    ) {
    }
}
use Patchlevel\EventSourcing\CommandBus\DefaultCommandBus;
use Patchlevel\EventSourcing\CommandBus\ServiceLocator;

$commandBus = DefaultCommandBus::createForAggregateHandlers(
    $aggregateRootRegistry,
    $repositoryManager,
    new ServiceLocator([
        NameValidator::class => new NameValidator(),
    ]), // or other psr-11 compatible container
);

$commandBus->dispatch(new CreateProfile($profileId, 'John'));
$commandBus->dispatch(new ChangeProfileName($profileId, 'John Doe'));

@DavidBadura DavidBadura added the enhancement New feature or request label Nov 8, 2024
Copy link

github-actions bot commented Nov 8, 2024

Hello 👋

here is the most recent benchmark result:

SimpleSetupBench
================

+----------------------------------------+--------------------+--------------------+-----------+-----------------+------------+-------------+
|                                        | time (kde mode)                                     | memory                                     |
+----------------------------------------+--------------------+--------------------+-----------+-----------------+------------+-------------+
| subject                                | Tag: <current>     | Tag: base          | time-diff | Tag: <current>  | Tag: base  | memory-diff |
+----------------------------------------+--------------------+--------------------+-----------+-----------------+------------+-------------+
| benchLoad1Event ()                     | 963.200μs (±0.00%) | 987.100μs (±0.00%) | -2.42%    | 34.266mb        | 34.274mb   | -0.02%      |
| benchLoad10000Events ()                | 49.571ms (±0.00%)  | 48.211ms (±0.00%)  | +2.82%    | 34.266mb        | 33.765mb   | +1.48%      |
| benchSave1Event ()                     | 1.076ms (±0.00%)   | 1.034ms (±0.00%)   | +4.02%    | 33.765mb        | 33.765mb   | 0.00%       |
| benchSave10000Events ()                | 229.757ms (±0.00%) | 234.363ms (±0.00%) | -1.97%    | 33.765mb        | 33.765mb   | 0.00%       |
| benchSave10000Aggregates ()            | 8.842s (±0.00%)    | 9.061s (±0.00%)    | -2.42%    | 33.765mb        | 33.765mb   | 0.00%       |
| benchSave10000AggregatesTransaction () | 4.859s (±0.00%)    | 4.863s (±0.00%)    | -0.08%    | 33.765mb        | 33.765mb   | 0.00%       |
+----------------------------------------+--------------------+--------------------+-----------+-----------------+------------+-------------+

SnapshotsBench
==============

+----------------------------------------+--------------------+--------------------+-----------+-----------------+------------+-------------+
|                                        | time (kde mode)                                     | memory                                     |
+----------------------------------------+--------------------+--------------------+-----------+-----------------+------------+-------------+
| subject                                | Tag: <current>     | Tag: base          | time-diff | Tag: <current>  | Tag: base  | memory-diff |
+----------------------------------------+--------------------+--------------------+-----------+-----------------+------------+-------------+
| benchLoad10000EventsMissingSnapshot () | 50.485ms (±0.00%)  | 49.563ms (±0.00%)  | +1.86%    | 33.836mb        | 33.836mb   | 0.00%       |
| benchLoad10000Events ()                | 886.000μs (±0.00%) | 898.000μs (±0.00%) | -1.34%    | 33.836mb        | 33.836mb   | 0.00%       |
+----------------------------------------+--------------------+--------------------+-----------+-----------------+------------+-------------+

SubscriptionEngineBatchBench
============================

+---------------------------+-------------------+-------------------+-----------+-----------------+------------+-------------+
|                           | time (kde mode)                                   | memory                                     |
+---------------------------+-------------------+-------------------+-----------+-----------------+------------+-------------+
| subject                   | Tag: <current>    | Tag: base         | time-diff | Tag: <current>  | Tag: base  | memory-diff |
+---------------------------+-------------------+-------------------+-----------+-----------------+------------+-------------+
| benchHandle10000Events () | 72.349ms (±0.00%) | 79.388ms (±0.00%) | -8.87%    | 34.234mb        | 34.234mb   | 0.00%       |
+---------------------------+-------------------+-------------------+-----------+-----------------+------------+-------------+

SubscriptionEngineBench
=======================

+---------------------------+-----------------+-----------------+-----------+-----------------+------------+-------------+
|                           | time (kde mode)                               | memory                                     |
+---------------------------+-----------------+-----------------+-----------+-----------------+------------+-------------+
| subject                   | Tag: <current>  | Tag: base       | time-diff | Tag: <current>  | Tag: base  | memory-diff |
+---------------------------+-----------------+-----------------+-----------+-----------------+------------+-------------+
| benchHandle10000Events () | 3.132s (±0.00%) | 3.172s (±0.00%) | -1.25%    | 46.382mb        | 46.382mb   | 0.00%       |
+---------------------------+-----------------+-----------------+-----------+-----------------+------------+-------------+

PersonalDataBench
=================

+----------------------------------------+--------------------+--------------------+-----------+-----------------+------------+-------------+
|                                        | time (kde mode)                                     | memory                                     |
+----------------------------------------+--------------------+--------------------+-----------+-----------------+------------+-------------+
| subject                                | Tag: <current>     | Tag: base          | time-diff | Tag: <current>  | Tag: base  | memory-diff |
+----------------------------------------+--------------------+--------------------+-----------+-----------------+------------+-------------+
| benchLoad1Event ()                     | 912.200μs (±0.00%) | 920.400μs (±0.00%) | -0.89%    | 34.895mb        | 34.895mb   | 0.00%       |
| benchLoad10000Events ()                | 87.406ms (±0.00%)  | 86.726ms (±0.00%)  | +0.79%    | 34.895mb        | 34.895mb   | 0.00%       |
| benchSave1Event ()                     | 1.607ms (±0.00%)   | 1.847ms (±0.00%)   | -12.99%   | 34.895mb        | 34.895mb   | 0.00%       |
| benchSave10000Events ()                | 251.336ms (±0.00%) | 259.579ms (±0.00%) | -3.18%    | 34.897mb        | 34.897mb   | 0.00%       |
| benchSave10000Aggregates ()            | 13.091s (±0.00%)   | 12.872s (±0.00%)   | +1.70%    | 34.895mb        | 34.895mb   | 0.00%       |
| benchSave10000AggregatesTransaction () | 8.907s (±0.00%)    | 9.056s (±0.00%)    | -1.65%    | 35.396mb        | 35.396mb   | 0.00%       |
+----------------------------------------+--------------------+--------------------+-----------+-----------------+------------+-------------+

SimpleSetupStreamStoreBench
===========================

+----------------------------------------+--------------------+--------------------+-----------+-----------------+------------+-------------+
|                                        | time (kde mode)                                     | memory                                     |
+----------------------------------------+--------------------+--------------------+-----------+-----------------+------------+-------------+
| subject                                | Tag: <current>     | Tag: base          | time-diff | Tag: <current>  | Tag: base  | memory-diff |
+----------------------------------------+--------------------+--------------------+-----------+-----------------+------------+-------------+
| benchLoad1Event ()                     | 937.000μs (±0.00%) | 969.700μs (±0.00%) | -3.37%    | 34.862mb        | 34.862mb   | 0.00%       |
| benchLoad10000Events ()                | 56.785ms (±0.00%)  | 55.827ms (±0.00%)  | +1.72%    | 34.862mb        | 34.862mb   | 0.00%       |
| benchSave1Event ()                     | 1.316ms (±0.00%)   | 1.142ms (±0.00%)   | +15.26%   | 34.862mb        | 34.862mb   | 0.00%       |
| benchSave10000Events ()                | 296.319ms (±0.00%) | 305.288ms (±0.00%) | -2.94%    | 34.862mb        | 34.862mb   | 0.00%       |
| benchSave10000Aggregates ()            | 8.929s (±0.00%)    | 9.052s (±0.00%)    | -1.37%    | 34.862mb        | 34.862mb   | 0.00%       |
| benchSave10000AggregatesTransaction () | 5.048s (±0.00%)    | 5.108s (±0.00%)    | -1.18%    | 34.862mb        | 34.862mb   | 0.00%       |
+----------------------------------------+--------------------+--------------------+-----------+-----------------+------------+-------------+

SplitStreamBench
================

+-------------------------+--------------------+--------------------+-----------+-----------------+------------+-------------+
|                         | time (kde mode)                                     | memory                                     |
+-------------------------+--------------------+--------------------+-----------+-----------------+------------+-------------+
| subject                 | Tag: <current>     | Tag: base          | time-diff | Tag: <current>  | Tag: base  | memory-diff |
+-------------------------+--------------------+--------------------+-----------+-----------------+------------+-------------+
| benchLoad10000Events () | 4.191ms (±0.00%)   | 4.305ms (±0.00%)   | -2.66%    | 37.072mb        | 37.072mb   | 0.00%       |
| benchSave10000Events () | 344.363ms (±0.00%) | 354.985ms (±0.00%) | -2.99%    | 37.144mb        | 37.145mb   | -0.00%      |
+-------------------------+--------------------+--------------------+-----------+-----------------+------------+-------------+

This comment gets update everytime a new commit comes in!

@DavidBadura DavidBadura changed the title POC: command bus command bus Dec 10, 2024
@DavidBadura DavidBadura changed the base branch from 3.6.x to 3.7.x December 20, 2024 11:49
@DavidBadura DavidBadura added this to the 3.7.0 milestone Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant