From 5e1b140f8c5150921f9da312b77a3dcd4d96c8f9 Mon Sep 17 00:00:00 2001 From: David Badura Date: Sun, 8 Oct 2023 10:09:47 +0200 Subject: [PATCH] add debug performance possibilities --- src/Debug/ProfileData.php | 70 ++++++++++++++++++++++++ src/Debug/ProfileDataHolder.php | 27 +++++++++ src/Debug/TraceableRepository.php | 60 ++++++++++++++++++++ src/Debug/TraceableRepositoryManager.php | 37 +++++++++++++ 4 files changed, 194 insertions(+) create mode 100644 src/Debug/ProfileData.php create mode 100644 src/Debug/ProfileDataHolder.php create mode 100644 src/Debug/TraceableRepository.php create mode 100644 src/Debug/TraceableRepositoryManager.php diff --git a/src/Debug/ProfileData.php b/src/Debug/ProfileData.php new file mode 100644 index 000000000..ea1fb9fdc --- /dev/null +++ b/src/Debug/ProfileData.php @@ -0,0 +1,70 @@ + */ + private readonly string $aggregateClass, + private readonly string $aggregateId, + ) { + } + + public function start(): void + { + $this->start = microtime(true); + } + + public function stop(): void + { + if ($this->start === null) { + return; + } + + $this->duration = microtime(true) - $this->start; + } + + public function type(): string + { + return $this->type; + } + + /** @return class-string */ + public function aggregateClass(): string + { + return $this->aggregateClass; + } + + public function aggregateId(): string + { + return $this->aggregateId; + } + + public function duration(): float|null + { + return $this->duration; + } + + /** @param class-string $aggregateClass */ + public static function load(string $aggregateClass, string $aggregateId): self + { + return new ProfileData('load', $aggregateClass, $aggregateId); + } + + /** @param class-string $aggregateClass */ + public static function save(string $aggregateClass, string $aggregateId): self + { + return new ProfileData('save', $aggregateClass, $aggregateId); + } +} diff --git a/src/Debug/ProfileDataHolder.php b/src/Debug/ProfileDataHolder.php new file mode 100644 index 000000000..e0144362f --- /dev/null +++ b/src/Debug/ProfileDataHolder.php @@ -0,0 +1,27 @@ +data[] = $data; + } + + /** @return ProfileData[] */ + public function getData(): array + { + return $this->data; + } + + public function reset(): void + { + $this->data = []; + } +} diff --git a/src/Debug/TraceableRepository.php b/src/Debug/TraceableRepository.php new file mode 100644 index 000000000..1c04504b2 --- /dev/null +++ b/src/Debug/TraceableRepository.php @@ -0,0 +1,60 @@ + + */ +final class TraceableRepository implements Repository +{ + public function __construct( + /** @var Repository */ + private readonly Repository $repository, + /** @var class-string */ + private readonly string $aggregateClass, + private readonly ProfileDataHolder $dataHolder, + private readonly Stopwatch|null $stopwatch = null, + ) { + } + + /** @return T */ + public function load(string $id): AggregateRoot + { + $data = ProfileData::load($this->aggregateClass, $id); + + $this->dataHolder->addData($data); + $event = $this->stopwatch?->start('event_sourcing', 'event_sourcing'); + $data->start(); + + try { + $aggregate = $this->repository->load($id); + } finally { + $data->stop(); + $event?->stop(); + } + + return $aggregate; + } + + public function has(string $id): bool + { + return $this->repository->has($id); + } + + /** @param T $aggregate */ + public function save(AggregateRoot $aggregate): void + { + $event = $this->stopwatch?->start('event_sourcing', 'event_sourcing'); + + $this->repository->save($aggregate); + + $event?->stop(); + } +} diff --git a/src/Debug/TraceableRepositoryManager.php b/src/Debug/TraceableRepositoryManager.php new file mode 100644 index 000000000..c468b520c --- /dev/null +++ b/src/Debug/TraceableRepositoryManager.php @@ -0,0 +1,37 @@ + $aggregateClass + * + * @return Repository + * + * @template T of AggregateRoot + */ + public function get(string $aggregateClass): Repository + { + return new TraceableRepository( + $this->repositoryManager->get($aggregateClass), + $aggregateClass, + $this->dataHolder, + $this->stopwatch, + ); + } +}