Skip to content

Commit

Permalink
extract logic in profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Dec 13, 2023
1 parent 271d035 commit 370a1c3
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 76 deletions.
37 changes: 37 additions & 0 deletions src/Debug/DefaultProfiler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Patchlevel\EventSourcing\Debug;

use Symfony\Component\Stopwatch\Stopwatch;

final class DefaultProfiler implements Profiler
{
public function __construct(
private readonly ProfileDataHolder $dataHolder,
private readonly Stopwatch|null $stopwatch = null,
) {
}

/**
* @param \Closure():T $closure
*
* @return T
*
* @template T of mixed
*/
public function profile(string $name, \Closure $closure, $context = []): mixed

Check failure on line 22 in src/Debug/DefaultProfiler.php

View workflow job for this annotation

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

MissingParamType

src/Debug/DefaultProfiler.php:22:62: MissingParamType: Parameter $context has no provided type (see https://psalm.dev/154)

Check failure on line 22 in src/Debug/DefaultProfiler.php

View workflow job for this annotation

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

Method Patchlevel\EventSourcing\Debug\DefaultProfiler::profile() has parameter $context with no type specified.
{
$data = new ProfileData($name, $context);

Check failure on line 24 in src/Debug/DefaultProfiler.php

View workflow job for this annotation

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

MixedArgument

src/Debug/DefaultProfiler.php:24:40: MixedArgument: Argument 2 of Patchlevel\EventSourcing\Debug\ProfileData::__construct cannot be mixed, expecting array<array-key, mixed> (see https://psalm.dev/030)

$this->dataHolder->addData($data);
$event = $this->stopwatch?->start('event_sourcing', $name);
$data->start();

try {
return $closure();
} finally {
$data->stop();
$event?->stop();
}
}
}
44 changes: 7 additions & 37 deletions src/Debug/ProfileData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,16 @@

namespace Patchlevel\EventSourcing\Debug;

use Patchlevel\EventSourcing\Aggregate\AggregateRoot;

use function microtime;

final class ProfileData
{
private float|null $start = null;
private float|null $duration = null;

private function __construct(
/** @var 'load'|'has'|'save' */
private readonly string $type,
/** @var class-string<AggregateRoot> */
private readonly string $aggregateClass,
private readonly string $aggregateId,
public function __construct(

Check failure on line 14 in src/Debug/ProfileData.php

View workflow job for this annotation

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

Method Patchlevel\EventSourcing\Debug\ProfileData::__construct() has parameter $context with no value type specified in iterable type array.
private readonly string $name,
private readonly array $context = [],
) {
}

Expand All @@ -36,43 +31,18 @@ public function stop(): void
$this->duration = microtime(true) - $this->start;
}

/** @return 'load'|'has'|'save' */
public function type(): string
{
return $this->type;
}

/** @return class-string<AggregateRoot> */
public function aggregateClass(): string
public function name(): string
{
return $this->aggregateClass;
return $this->name;
}

public function aggregateId(): string
public function context(): array

Check failure on line 39 in src/Debug/ProfileData.php

View workflow job for this annotation

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

Method Patchlevel\EventSourcing\Debug\ProfileData::context() return type has no value type specified in iterable type array.
{
return $this->aggregateId;
return $this->context;
}

public function duration(): float|null
{
return $this->duration;
}

/** @param class-string<AggregateRoot> $aggregateClass */
public static function loadAggregate(string $aggregateClass, string $aggregateId): self
{
return new ProfileData('load', $aggregateClass, $aggregateId);
}

/** @param class-string<AggregateRoot> $aggregateClass */
public static function hasAggregate(string $aggregateClass, string $aggregateId): self
{
return new ProfileData('has', $aggregateClass, $aggregateId);
}

/** @param class-string<AggregateRoot> $aggregateClass */
public static function saveAggregate(string $aggregateClass, string $aggregateId): self
{
return new ProfileData('save', $aggregateClass, $aggregateId);
}
}
15 changes: 15 additions & 0 deletions src/Debug/Profiler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Patchlevel\EventSourcing\Debug;

interface Profiler
{
/**
* @param \Closure():T $closure
*
* @return T
*
* @template T of mixed
*/
public function profile(string $name, \Closure $closure, $context = []): mixed;

Check failure on line 14 in src/Debug/Profiler.php

View workflow job for this annotation

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

MissingParamType

src/Debug/Profiler.php:14:62: MissingParamType: Parameter $context has no provided type (see https://psalm.dev/154)

Check failure on line 14 in src/Debug/Profiler.php

View workflow job for this annotation

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

Method Patchlevel\EventSourcing\Debug\Profiler::profile() has parameter $context with no type specified.
}
64 changes: 25 additions & 39 deletions src/Debug/TraceableRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Patchlevel\EventSourcing\Aggregate\AggregateRoot;
use Patchlevel\EventSourcing\Repository\Repository;
use Symfony\Component\Stopwatch\Stopwatch;

/**
* @template T of AggregateRoot
Expand All @@ -19,58 +18,45 @@ public function __construct(
private readonly Repository $repository,
/** @var class-string<T> */
private readonly string $aggregateClass,
private readonly ProfileDataHolder $dataHolder,
private readonly Stopwatch|null $stopwatch = null,
private readonly Profiler $profiler,
) {
}

/** @return T */

Check failure on line 25 in src/Debug/TraceableRepository.php

View workflow job for this annotation

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

MixedInferredReturnType

src/Debug/TraceableRepository.php:25:17: MixedInferredReturnType: Could not verify return type 'T' for Patchlevel\EventSourcing\Debug\TraceableRepository::load (see https://psalm.dev/047)
public function load(string $id): AggregateRoot
{
$data = ProfileData::loadAggregate($this->aggregateClass, $id);

$this->dataHolder->addData($data);
$event = $this->stopwatch?->start('event_sourcing', 'event_sourcing');
$data->start();

try {
return $this->repository->load($id);
} finally {
$data->stop();
$event?->stop();
}
return $this->profiler->profile(

Check failure on line 28 in src/Debug/TraceableRepository.php

View workflow job for this annotation

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

MixedReturnStatement

src/Debug/TraceableRepository.php:28:16: MixedReturnStatement: Could not infer a return type (see https://psalm.dev/138)
'aggregate.load',
static fn () => $this->repository->load($id),

Check failure on line 30 in src/Debug/TraceableRepository.php

View workflow job for this annotation

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

MissingClosureReturnType

src/Debug/TraceableRepository.php:30:13: MissingClosureReturnType: Closure does not have a return type, expecting mixed (see https://psalm.dev/068)

Check failure on line 30 in src/Debug/TraceableRepository.php

View workflow job for this annotation

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

InvalidScope

src/Debug/TraceableRepository.php:30:29: InvalidScope: Invalid reference to $this in a static context (see https://psalm.dev/013)

Check failure on line 30 in src/Debug/TraceableRepository.php

View workflow job for this annotation

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

Undefined variable: $this
[
'aggregateClass' => $this->aggregateClass,
'aggregateId' => $id,
]
);
}

public function has(string $id): bool

Check failure on line 38 in src/Debug/TraceableRepository.php

View workflow job for this annotation

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

MixedInferredReturnType

src/Debug/TraceableRepository.php:38:38: MixedInferredReturnType: Could not verify return type 'bool' for Patchlevel\EventSourcing\Debug\TraceableRepository::has (see https://psalm.dev/047)
{
$data = ProfileData::hasAggregate($this->aggregateClass, $id);

$this->dataHolder->addData($data);
$event = $this->stopwatch?->start('event_sourcing', 'event_sourcing');
$data->start();

try {
return $this->repository->has($id);
} finally {
$data->stop();
$event?->stop();
}
return $this->profiler->profile(

Check failure on line 40 in src/Debug/TraceableRepository.php

View workflow job for this annotation

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

MixedReturnStatement

src/Debug/TraceableRepository.php:40:16: MixedReturnStatement: Could not infer a return type (see https://psalm.dev/138)
'aggregate.has',
static fn () => $this->repository->has($id),

Check failure on line 42 in src/Debug/TraceableRepository.php

View workflow job for this annotation

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

MissingClosureReturnType

src/Debug/TraceableRepository.php:42:13: MissingClosureReturnType: Closure does not have a return type, expecting mixed (see https://psalm.dev/068)

Check failure on line 42 in src/Debug/TraceableRepository.php

View workflow job for this annotation

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

Undefined variable: $this
[
'aggregateClass' => $this->aggregateClass,
'aggregateId' => $id,
]
);
}

/** @param T $aggregate */
public function save(AggregateRoot $aggregate): void
{
$data = ProfileData::saveAggregate($this->aggregateClass, $aggregate->aggregateRootId());

$this->dataHolder->addData($data);
$event = $this->stopwatch?->start('event_sourcing', 'event_sourcing');
$data->start();

try {
$this->repository->save($aggregate);
} finally {
$data->stop();
$event?->stop();
}
$this->profiler->profile(
'aggregate.save',
static fn () => $this->repository->save($aggregate),

Check failure on line 55 in src/Debug/TraceableRepository.php

View workflow job for this annotation

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

Undefined variable: $this
[
'aggregateClass' => $this->aggregateClass,
'aggregateId' => $aggregate->aggregateRootId(),
]
);
}
}

0 comments on commit 370a1c3

Please sign in to comment.