Skip to content

Commit 228026d

Browse files
committed
Improve Snapshot comparison
1 parent 14d46c9 commit 228026d

5 files changed

Lines changed: 103 additions & 15 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ All Notable changes to `bakame/stackwatch` will be documented in this file.
2626
- Added `Profiler::dump` and its global function counterpart `pfd` to profile, dump a human-readable result into the output stream and die.
2727
- Added `forHuman()` method to return a human-readable version of `toArray`
2828
- Added `human()` method to return the human-redable version of a single property.
29+
- Added Snapshot relation method `compareTo` and its aliases `isBefore*` `isAfter*` and `isAtSameTime` using the `Snapshot::hrtime` property
2930

3031
### Fixed
3132

src/Exporter/StatsExporter.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Bakame\Stackwatch\Result;
1414
use Bakame\Stackwatch\Snapshot;
1515
use Bakame\Stackwatch\Span;
16+
use Bakame\Stackwatch\Statistics;
1617
use Bakame\Stackwatch\Translator;
1718

1819
use function array_keys;
@@ -52,18 +53,6 @@ public function writeln(string $content): int|false
5253
return fwrite($this->stream, $content."\n");
5354
}
5455

55-
public function exportMetrics(Result|Span|Metrics $metrics): void
56-
{
57-
/** @var Metrics $source */
58-
$source = match ($metrics::class) {
59-
Result::class => $metrics->span->metrics,
60-
Span::class => $metrics->metrics,
61-
Metrics::class => $metrics,
62-
};
63-
64-
$this->writeLeaderPrinter($source->toHuman());
65-
}
66-
6756
/**
6857
* @param array<string, string> $data
6958
*/
@@ -79,11 +68,28 @@ public function exportSnapshots(Snapshot $snapshot): void
7968
$this->writeLeaderPrinter($snapshot->toHuman());
8069
}
8170

82-
public function exportEnvironement(Environment $environment): void
71+
public function exportMetrics(Result|Span|Metrics $metrics): void
72+
{
73+
/** @var Metrics $source */
74+
$source = match ($metrics::class) {
75+
Result::class => $metrics->span->metrics,
76+
Span::class => $metrics->metrics,
77+
Metrics::class => $metrics,
78+
};
79+
80+
$this->writeLeaderPrinter($source->toHuman());
81+
}
82+
83+
public function exportEnvironment(Environment $environment): void
8384
{
8485
$this->writeLeaderPrinter($environment->toHuman());
8586
}
8687

88+
public function exportStatistics(Statistics $statistics): void
89+
{
90+
$this->writeLeaderPrinter($statistics->toHuman());
91+
}
92+
8793
public function exportReport(Report $report): void
8894
{
8995
$reportData = $report->toHuman();

src/Metrics.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ public static function none(): self
6868

6969
public static function fromSnapshots(Snapshot $start, Snapshot $end): self
7070
{
71-
($excutionTime = $end->hrtime - $start->hrtime) >= 0 || throw new UnableToProfile('The ending snapshot was taken before the starting snapshot.');
71+
$start->isBeforeOrAtSameTime($end) || throw new UnableToProfile('The ending snapshot was taken before the starting snapshot.');
7272

7373
return new self(
7474
cpuTime: $end->cpuUserTime + $end->cpuSystemTime - $start->cpuUserTime - $start->cpuSystemTime,
75-
executionTime: $excutionTime,
75+
executionTime: $end->hrtime - $start->hrtime,
7676
memoryUsage: $end->memoryUsage - $start->memoryUsage,
7777
peakMemoryUsage: $end->peakMemoryUsage - $start->peakMemoryUsage,
7878
realMemoryUsage: $end->realMemoryUsage - $start->realMemoryUsage,

src/Snapshot.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,36 @@ public function equals(mixed $other): bool
200200
&& $this->originLine === $other->originLine;
201201
}
202202

203+
public function isBefore(Snapshot $other): bool
204+
{
205+
return -1 === $this->compareTo($other);
206+
}
207+
208+
public function isAfter(Snapshot $other): bool
209+
{
210+
return 1 === $this->compareTo($other);
211+
}
212+
213+
public function isAtSameTime(Snapshot $other): bool
214+
{
215+
return 0 === $this->compareTo($other);
216+
}
217+
218+
public function isAfterOrAtSameTime(Snapshot $other): bool
219+
{
220+
return 0 <= $this->compareTo($other);
221+
}
222+
223+
public function isBeforeOrAtSameTime(Snapshot $other): bool
224+
{
225+
return 0 >= $this->compareTo($other);
226+
}
227+
228+
public function compareTo(Snapshot $other): int
229+
{
230+
return $this->hrtime <=> $other->hrtime;
231+
}
232+
203233
/**
204234
* @return SnapshotMap
205235
*/

src/SnapshotTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Bakame\Stackwatch;
66

7+
use DateTimeImmutable;
78
use PHPUnit\Framework\Attributes\CoversClass;
89
use PHPUnit\Framework\Attributes\Test;
910
use PHPUnit\Framework\TestCase;
@@ -103,4 +104,54 @@ public function it_fails_to_create_a_new_instance_from_an_invalid_array(): void
103104

104105
Snapshot::fromArray([]); /* @phpstan-ignore-line */
105106
}
107+
108+
#[Test]
109+
public function testCompareToAndRelations(): void
110+
{
111+
$a = new Snapshot(
112+
'start',
113+
new DateTimeImmutable(),
114+
100,
115+
1_500_000,
116+
250_000,
117+
1000,
118+
2000,
119+
3000,
120+
4000,
121+
);
122+
123+
$b = new Snapshot(
124+
'start',
125+
new DateTimeImmutable(),
126+
200,
127+
1_500_000,
128+
250_000,
129+
1000,
130+
2000,
131+
3000,
132+
4000,
133+
);
134+
135+
self::assertSame(-1, $a->compareTo($b));
136+
self::assertTrue($a->isBefore($b));
137+
self::assertFalse($a->isAfter($b));
138+
self::assertFalse($a->isAtSameTime($b));
139+
self::assertTrue($a->isBeforeOrAtSameTime($b));
140+
self::assertFalse($a->isAfterOrAtSameTime($b));
141+
142+
self::assertSame(1, $b->compareTo($a));
143+
self::assertTrue($b->isAfter($a));
144+
self::assertFalse($b->isBefore($a));
145+
self::assertFalse($b->isAtSameTime($a));
146+
self::assertFalse($b->isBeforeOrAtSameTime($a));
147+
self::assertTrue($b->isAfterOrAtSameTime($a));
148+
149+
$c = clone $a;
150+
self::assertSame(0, $a->compareTo($c));
151+
self::assertFalse($a->isBefore($c));
152+
self::assertFalse($a->isAfter($c));
153+
self::assertTrue($a->isAtSameTime($c));
154+
self::assertTrue($a->isBeforeOrAtSameTime($c));
155+
self::assertTrue($a->isAfterOrAtSameTime($c));
156+
}
106157
}

0 commit comments

Comments
 (0)