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 191c4e0
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Exception/ProfilerCouldNotSnapshotOutsideProfile.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 ProfilerCouldNotSnapshotOutsideProfile 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\ProfilerCouldNotSnapshotOutsideProfile();
}
}
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\ProfilerCouldNotSnapshotOutsideProfile
*/
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\ProfilerCouldNotSnapshotOutsideProfile();
}
$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\ProfilerCouldNotSnapshotOutsideProfile::class);

$profiler->snapshot();
}
}

0 comments on commit 191c4e0

Please sign in to comment.