diff --git a/README.md b/README.md index 82df504..eb67ad2 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ declare(ticks=2); // this declaration is important (N=2) namespace PetrKnap\Profiler; -$profiling = Profiling::start(listenToTicks: true); +$profiling = Profiling::start(snapshotOnTick: true); (fn () => 'something')(); $profile = $profiling->finish(); diff --git a/src/Profile.php b/src/Profile.php index 4232084..e178c28 100644 --- a/src/Profile.php +++ b/src/Profile.php @@ -18,13 +18,13 @@ */ final class Profile implements ProcessableProfileInterface, ProfileWithOutputInterface { - public const DO_NOT_LISTEN_TO_TICKS = false; + public const DO_NOT_SNAPSHOT_ON_TICK = false; private const MICROTIME_FORMAT = '%.6f'; private const SORTED_BY_TIME = true; private ProfileState $state; - private bool|null $isListeningToTicks; + private bool|null $isSnapshotingOnTick; private OptionalFloat $timeBefore; private OptionalInt $memoryUsageBefore; private OptionalFloat $timeAfter; @@ -43,10 +43,10 @@ final class Profile implements ProcessableProfileInterface, ProfileWithOutputInt private Optional $outputOption; public function __construct( - bool $listenToTicks = self::DO_NOT_LISTEN_TO_TICKS, + bool $snapshotOnTick = self::DO_NOT_SNAPSHOT_ON_TICK, ) { $this->state = ProfileState::Created; - $this->isListeningToTicks = $listenToTicks ? false : null; + $this->isSnapshotingOnTick = $snapshotOnTick ? false : null; $this->timeBefore = OptionalFloat::empty(); $this->memoryUsageBefore = OptionalInt::empty(); $this->timeAfter = OptionalFloat::empty(); @@ -56,7 +56,7 @@ public function __construct( public function __destruct() { - $this->unregisterTickHandler(); + $this->unregisterTickSnapshot(); } public function getState(): ProfileState @@ -77,7 +77,7 @@ public function start(): void $this->timeBefore = OptionalFloat::of(microtime(as_float: true)); $this->memoryUsageBefore = OptionalInt::of(memory_get_usage(real_usage: true)); - $this->registerTickHandler(); + $this->registerTickSnapshot(); } /** @@ -85,7 +85,7 @@ public function start(): void */ public function finish(): void { - $this->unregisterTickHandler(); + $this->unregisterTickSnapshot(); if ($this->state !== ProfileState::Started) { throw new Exception\ProfileCouldNotBeFinished(); @@ -99,23 +99,23 @@ public function finish(): void /** * @throws Exception\ProfileCouldNotRegisterTickHandler */ - public function registerTickHandler(): void + public function registerTickSnapshot(): void { - if ($this->isListeningToTicks === false) { - register_tick_function([$this, 'tickHandler']) or throw new Exception\ProfileCouldNotRegisterTickHandler(); - $this->isListeningToTicks = true; + if ($this->isSnapshotingOnTick === false) { + register_tick_function([$this, 'snapshot']) or throw new Exception\ProfileCouldNotRegisterTickHandler(); + $this->isSnapshotingOnTick = true; } } - public function unregisterTickHandler(): void + public function unregisterTickSnapshot(): void { - if ($this->isListeningToTicks === true) { - unregister_tick_function([$this, 'tickHandler']); - $this->isListeningToTicks = false; + if ($this->isSnapshotingOnTick === true) { + unregister_tick_function([$this, 'snapshot']); + $this->isSnapshotingOnTick = false; } } - public function tickHandler(): void + public function snapshot(): void { $this->memoryUsages[sprintf(self::MICROTIME_FORMAT, microtime(as_float: true))] = memory_get_usage(real_usage: true); } diff --git a/src/Profiler.php b/src/Profiler.php index 815a7af..a1755ff 100644 --- a/src/Profiler.php +++ b/src/Profiler.php @@ -6,14 +6,22 @@ /* final */class Profiler implements ProfilerInterface { + /** + * @param bool $snapshotOnTick if true, it will do snapshot on each tick + */ public function __construct( - private readonly bool $listenToTicks = Profile::DO_NOT_LISTEN_TO_TICKS, + private readonly bool $snapshotOnTick = Profile::DO_NOT_SNAPSHOT_ON_TICK, + /** + * @deprecated + * @todo remove it + */ + private readonly bool $listenToTicks = Profile::DO_NOT_SNAPSHOT_ON_TICK, ) { } public function profile(callable $callable): ProcessableProfileInterface & ProfileWithOutputInterface { - $profiling = Profiling::start($this->listenToTicks); + $profiling = Profiling::start($this->snapshotOnTick, $this->listenToTicks); $output = $callable(Profiling::createNestedProfiler($profiling)); /** @var Profile $profile */ $profile = $profiling->finish(); diff --git a/src/Profiling.php b/src/Profiling.php index f2bf1d9..31aeeae 100644 --- a/src/Profiling.php +++ b/src/Profiling.php @@ -4,6 +4,8 @@ namespace PetrKnap\Profiler; +use JetBrains\PhpStorm\Deprecated; + final class Profiling { /** @@ -11,17 +13,25 @@ final class Profiling */ private function __construct( private readonly Profile $profile, - private readonly bool $listenToTicks, + private readonly bool $snapshotOnTick, ) { } + /** + * @param bool $snapshotOnTick if true, it will do snapshot on each tick + */ public static function start( - bool $listenToTicks = Profile::DO_NOT_LISTEN_TO_TICKS, + bool $snapshotOnTick = Profile::DO_NOT_SNAPSHOT_ON_TICK, + /** + * @deprecated + * @todo remove it + */ + bool $listenToTicks = Profile::DO_NOT_SNAPSHOT_ON_TICK, ): self { - $profile = new Profile($listenToTicks); + $profile = new Profile($listenToTicks || $snapshotOnTick); $profile->start(); - return new self($profile, $listenToTicks); + return new self($profile, $listenToTicks || $snapshotOnTick); } /** @@ -31,7 +41,7 @@ public function snapshot(): void { $this->checkProfileState(); - $this->profile->tickHandler(); + $this->profile->snapshot(); } /** @@ -51,16 +61,16 @@ public function finish(): ProfileInterface */ public static function createNestedProfiler(Profiling $profiling): ProfilerInterface { - return new class ($profiling->profile, $profiling->listenToTicks) extends Profiler { + return new class ($profiling->profile, $profiling->snapshotOnTick) extends Profiler { /** * @param Profile $parentProfile */ public function __construct( private readonly Profile $parentProfile, - bool $listenToTicks, + bool $snapshotOnTick, ) { parent::__construct( - listenToTicks: $listenToTicks, + snapshotOnTick: $snapshotOnTick, ); } @@ -70,14 +80,14 @@ public function profile(callable $callable): ProcessableProfileInterface & Profi throw new Exception\ParentProfileIsNotStarted(); } - $this->parentProfile->unregisterTickHandler(); + $this->parentProfile->unregisterTickSnapshot(); try { $profile = parent::profile($callable); $this->parentProfile->addChild($profile); return $profile; } finally { - $this->parentProfile->registerTickHandler(); + $this->parentProfile->registerTickSnapshot(); } } }; diff --git a/tests/ProfileTest.php b/tests/ProfileTest.php index e1c7584..f6324ae 100644 --- a/tests/ProfileTest.php +++ b/tests/ProfileTest.php @@ -26,10 +26,10 @@ public function testAddsChildren(): void self::assertSame([$child1, $child2], $parent->getChildren()); } - #[DataProvider('dataListensToTicks')] - public function testListensToTicks(bool|null $shouldItListen): void + #[DataProvider('dataSnapshotsOnTick')] + public function testSnapshotsOnTick(bool|null $shouldItListen): void { - $profile = $shouldItListen === null ? new Profile() : new Profile(listenToTicks: $shouldItListen); + $profile = $shouldItListen === null ? new Profile() : new Profile(snapshotOnTick: $shouldItListen); $profile->start(); for ($i = 0; $i < 5; $i++) { $i = (fn (int $i): int => $i)($i); @@ -39,7 +39,7 @@ public function testListensToTicks(bool|null $shouldItListen): void self::assertCount($shouldItListen === true ? 9 : 2, $profile->getMemoryUsages()); } - public static function dataListensToTicks(): array + public static function dataSnapshotsOnTick(): array { return [ 'default' => [null],