Skip to content

Commit

Permalink
feat: added ability to snapshot on ProfilerInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
petrknap committed Oct 19, 2024
1 parent 0fdf9d7 commit 256cdbc
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Exception/ParentProfileIsNotStarted.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

/**
* @internal exception should never be thrown out
*
* @todo rename to `ProfilerCouldNotProfileOutsideParentProfile`
*/
final class ParentProfileIsNotStarted extends LogicException implements ProfilerException
{
Expand Down
11 changes: 11 additions & 0 deletions src/Exception/ProfilerCouldNotSnapshotOutsideParentProfile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace PetrKnap\Profiler\Exception;

use LogicException;

final class ProfilerCouldNotSnapshotOutsideParentProfile extends LogicException implements ProfilerException
{
}
4 changes: 4 additions & 0 deletions src/NullProfiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ public function getMemoryUsages(): array
}
};
}

public function snapshot(): void
{
}
}
5 changes: 5 additions & 0 deletions src/Profiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ public function profile(callable $callable): ProcessableProfileInterface & Profi

return $profile; // @phpstan-ignore return.type
}

public function snapshot(): void
{
throw new Exception\ProfilerCouldNotSnapshotOutsideParentProfile();
}
}
5 changes: 5 additions & 0 deletions src/ProfilerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ interface ProfilerInterface
* @return ProcessableProfileInterface<TOutput> & ProfileWithOutputInterface<TOutput>
*/
public function profile(callable $callable): ProcessableProfileInterface & ProfileWithOutputInterface;

/**
* @throws Exception\ProfilerCouldNotSnapshotOutsideParentProfile
*/
public function snapshot(): void;
}
8 changes: 8 additions & 0 deletions src/Profiling.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ public function profile(callable $callable): ProcessableProfileInterface & Profi
$this->parentProfile->registerTickSnapshot();
}
}

public function snapshot(): void
{
if ($this->parentProfile->getState() !== ProfileState::Started) {
throw new Exception\ProfilerCouldNotSnapshotOutsideParentProfile();
}
$this->parentProfile->snapshot();
}
};
}

Expand Down
9 changes: 9 additions & 0 deletions tests/NullProfilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,13 @@ public function testProfileDoesNotRunProcessorAndReturnsCallablesOutput(): void
self::assertFalse($processorWasCalled);
self::assertSame('output', $output);
}

public function testSnapshotDoNotThrow(): void
{
$profiler = new NullProfiler();

$profiler->snapshot();

self::assertTrue(true);
}
}
18 changes: 18 additions & 0 deletions tests/ProfilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,22 @@ public function testReturnsCallablesOutput(): void

self::assertSame('output', $profile->getOutput());
}

public function testSnapshotsOnParentProfile(): void
{
$profiler = new Profiler();

$profile = $profiler->profile(static fn (ProfilerInterface $profiler) => $profiler->snapshot());

self::assertCount(3, $profile->getMemoryUsages());
}

public function testSnapshotsThrowsOutsideProfile(): void
{
$profiler = new Profiler();

self::expectException(Exception\ProfilerCouldNotSnapshotOutsideParentProfile::class);

$profiler->snapshot();
}
}

0 comments on commit 256cdbc

Please sign in to comment.