Skip to content

Commit

Permalink
refactor: improved naming about snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
petrknap committed Oct 19, 2024
1 parent 493c1c0 commit 0c966f1
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
32 changes: 16 additions & 16 deletions src/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -56,7 +56,7 @@ public function __construct(

public function __destruct()
{
$this->unregisterTickHandler();
$this->unregisterTickSnapshot();
}

public function getState(): ProfileState
Expand All @@ -77,15 +77,15 @@ 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();
}

/**
* @throws Exception\ProfileCouldNotBeFinished
*/
public function finish(): void
{
$this->unregisterTickHandler();
$this->unregisterTickSnapshot();

if ($this->state !== ProfileState::Started) {
throw new Exception\ProfileCouldNotBeFinished();
Expand All @@ -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);
}
Expand Down
12 changes: 10 additions & 2 deletions src/Profiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed> $profile */
$profile = $profiling->finish();
Expand Down
28 changes: 18 additions & 10 deletions src/Profiling.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,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);
}

/**
Expand All @@ -31,7 +39,7 @@ public function snapshot(): void
{
$this->checkProfileState();

$this->profile->tickHandler();
$this->profile->snapshot();
}

/**
Expand All @@ -51,16 +59,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<mixed> $parentProfile
*/
public function __construct(
private readonly Profile $parentProfile,
bool $listenToTicks,
bool $snapshotOnTick,
) {
parent::__construct(
listenToTicks: $listenToTicks,
snapshotOnTick: $snapshotOnTick,
);
}

Expand All @@ -70,14 +78,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();
}
}
};
Expand Down
10 changes: 5 additions & 5 deletions tests/ProfileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ 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 $shouldItSnapshot): void
{
$profile = $shouldItListen === null ? new Profile() : new Profile(listenToTicks: $shouldItListen);
$profile = $shouldItSnapshot === null ? new Profile() : new Profile(snapshotOnTick: $shouldItSnapshot);
$profile->start();
for ($i = 0; $i < 5; $i++) {
$i = (fn (int $i): int => $i)($i);
}
$profile->finish();

self::assertCount($shouldItListen === true ? 9 : 2, $profile->getMemoryUsages());
self::assertCount($shouldItSnapshot === true ? 9 : 2, $profile->getMemoryUsages());
}

public static function dataListensToTicks(): array
public static function dataSnapshotsOnTick(): array
{
return [
'default' => [null],
Expand Down

0 comments on commit 0c966f1

Please sign in to comment.