Skip to content

Commit 0450a85

Browse files
Use garbage collector information introduced in php/php-src#11523
1 parent 45b0cf5 commit 0450a85

9 files changed

+115
-15
lines changed

.psalm/baseline.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@
7575
</file>
7676
<file src="src/Event/Value/Telemetry/Php83GarbageCollectorStatusProvider.php">
7777
<InvalidArrayOffset>
78+
<code><![CDATA[$status['application_time']]]></code>
7879
<code><![CDATA[$status['buffer_size']]]></code>
80+
<code><![CDATA[$status['collector_time']]]></code>
81+
<code><![CDATA[$status['destructor_time']]]></code>
82+
<code><![CDATA[$status['free_time']]]></code>
7983
<code><![CDATA[$status['full']]]></code>
8084
<code><![CDATA[$status['protected']]]></code>
8185
<code><![CDATA[$status['running']]]></code>

src/Event/Value/Telemetry/GarbageCollectorStatus.php

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,29 @@ final class GarbageCollectorStatus
2222
private readonly int $collected;
2323
private readonly int $threshold;
2424
private readonly int $roots;
25+
private readonly ?float $applicationTime;
26+
private readonly ?float $collectorTime;
27+
private readonly ?float $destructorTime;
28+
private readonly ?float $freeTime;
2529
private readonly ?bool $running;
2630
private readonly ?bool $protected;
2731
private readonly ?bool $full;
2832
private readonly ?int $bufferSize;
2933

30-
public function __construct(int $runs, int $collected, int $threshold, int $roots, ?bool $running, ?bool $protected, ?bool $full, ?int $bufferSize)
34+
public function __construct(int $runs, int $collected, int $threshold, int $roots, ?float $applicationTime, ?float $collectorTime, ?float $destructorTime, ?float $freeTime, ?bool $running, ?bool $protected, ?bool $full, ?int $bufferSize)
3135
{
32-
$this->runs = $runs;
33-
$this->collected = $collected;
34-
$this->threshold = $threshold;
35-
$this->roots = $roots;
36-
$this->running = $running;
37-
$this->protected = $protected;
38-
$this->full = $full;
39-
$this->bufferSize = $bufferSize;
36+
$this->runs = $runs;
37+
$this->collected = $collected;
38+
$this->threshold = $threshold;
39+
$this->roots = $roots;
40+
$this->applicationTime = $applicationTime;
41+
$this->collectorTime = $collectorTime;
42+
$this->destructorTime = $destructorTime;
43+
$this->freeTime = $freeTime;
44+
$this->running = $running;
45+
$this->protected = $protected;
46+
$this->full = $full;
47+
$this->bufferSize = $bufferSize;
4048
}
4149

4250
public function runs(): int
@@ -60,6 +68,10 @@ public function roots(): int
6068
}
6169

6270
/**
71+
* @psalm-assert-if-true !null $this->applicationTime
72+
* @psalm-assert-if-true !null $this->collectorTime
73+
* @psalm-assert-if-true !null $this->destructorTime
74+
* @psalm-assert-if-true !null $this->freeTime
6375
* @psalm-assert-if-true !null $this->running
6476
* @psalm-assert-if-true !null $this->protected
6577
* @psalm-assert-if-true !null $this->full
@@ -70,6 +82,54 @@ public function hasExtendedInformation(): bool
7082
return $this->running !== null;
7183
}
7284

85+
/**
86+
* @throws RuntimeException on PHP < 8.3
87+
*/
88+
public function applicationTime(): float
89+
{
90+
if ($this->applicationTime === null) {
91+
throw new RuntimeException('Information not available');
92+
}
93+
94+
return $this->applicationTime;
95+
}
96+
97+
/**
98+
* @throws RuntimeException on PHP < 8.3
99+
*/
100+
public function collectorTime(): float
101+
{
102+
if ($this->collectorTime === null) {
103+
throw new RuntimeException('Information not available');
104+
}
105+
106+
return $this->collectorTime;
107+
}
108+
109+
/**
110+
* @throws RuntimeException on PHP < 8.3
111+
*/
112+
public function destructorTime(): float
113+
{
114+
if ($this->destructorTime === null) {
115+
throw new RuntimeException('Information not available');
116+
}
117+
118+
return $this->destructorTime;
119+
}
120+
121+
/**
122+
* @throws RuntimeException on PHP < 8.3
123+
*/
124+
public function freeTime(): float
125+
{
126+
if ($this->freeTime === null) {
127+
throw new RuntimeException('Information not available');
128+
}
129+
130+
return $this->freeTime;
131+
}
132+
73133
/**
74134
* @throws RuntimeException on PHP < 8.3
75135
*/

src/Event/Value/Telemetry/Php81GarbageCollectorStatusProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public function status(): GarbageCollectorStatus
2929
null,
3030
null,
3131
null,
32+
null,
33+
null,
34+
null,
35+
null,
3236
);
3337
}
3438
}

src/Event/Value/Telemetry/Php83GarbageCollectorStatusProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public function status(): GarbageCollectorStatus
2525
$status['collected'],
2626
$status['threshold'],
2727
$status['roots'],
28+
$status['application_time'],
29+
$status['collector_time'],
30+
$status['destructor_time'],
31+
$status['free_time'],
2832
$status['running'],
2933
$status['protected'],
3034
$status['full'],

tests/unit/Event/AbstractEventTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ final protected function telemetryInfo(): Telemetry\Info
2828
HRTime::fromSecondsAndNanoseconds(...hrtime(false)),
2929
Telemetry\MemoryUsage::fromBytes(1000),
3030
Telemetry\MemoryUsage::fromBytes(2000),
31-
new Telemetry\GarbageCollectorStatus(0, 0, 0, 0, false, false, false, 0),
31+
new Telemetry\GarbageCollectorStatus(0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, false, false, false, 0),
3232
),
3333
Duration::fromSecondsAndNanoseconds(123, 456),
3434
Telemetry\MemoryUsage::fromBytes(2000),

tests/unit/Event/Value/Telemetry/GarbageCollectorStatusTest.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ public function testMayHaveRunning(): void
4848
$this->assertTrue($this->withDetails()->isRunning());
4949
}
5050

51+
public function testMayHaveApplicationTime(): void
52+
{
53+
$this->assertSame(5.0, $this->withDetails()->applicationTime());
54+
}
55+
56+
public function testMayHaveCollectorTime(): void
57+
{
58+
$this->assertSame(6.0, $this->withDetails()->collectorTime());
59+
}
60+
61+
public function testMayHaveDestructorTime(): void
62+
{
63+
$this->assertSame(7.0, $this->withDetails()->destructorTime());
64+
}
65+
66+
public function testMayHaveFreeTime(): void
67+
{
68+
$this->assertSame(8.0, $this->withDetails()->freeTime());
69+
}
70+
5171
public function testMayHaveProtected(): void
5272
{
5373
$this->assertTrue($this->withDetails()->isProtected());
@@ -60,7 +80,7 @@ public function testMayHaveFull(): void
6080

6181
public function testMayHaveBufferSize(): void
6282
{
63-
$this->assertSame(5, $this->withDetails()->bufferSize());
83+
$this->assertSame(9, $this->withDetails()->bufferSize());
6484
}
6585

6686
public function testMayNotHaveExtendedInformation(): void
@@ -107,6 +127,10 @@ private function withoutDetails(): GarbageCollectorStatus
107127
null,
108128
null,
109129
null,
130+
null,
131+
null,
132+
null,
133+
null,
110134
);
111135
}
112136

@@ -117,10 +141,14 @@ private function withDetails(): GarbageCollectorStatus
117141
2,
118142
3,
119143
4,
144+
5.0,
145+
6.0,
146+
7.0,
147+
8.0,
120148
true,
121149
true,
122150
true,
123-
5,
151+
9,
124152
);
125153
}
126154
}

tests/unit/Event/Value/Telemetry/SnapshotTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function testConstructorSetsValues(): void
2323
$time = HRTime::fromSecondsAndNanoseconds(...hrtime(false));
2424
$memoryUsage = MemoryUsage::fromBytes(2000);
2525
$peakMemoryUsage = MemoryUsage::fromBytes(3000);
26-
$garbageCollectorStatus = new GarbageCollectorStatus(0, 0, 0, 0, false, false, false, 0);
26+
$garbageCollectorStatus = new GarbageCollectorStatus(0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, false, false, false, 0);
2727

2828
$snapshot = new Snapshot(
2929
$time,

tests/unit/Event/Value/Telemetry/SystemTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function peakMemoryUsage(): MemoryUsage
6262
}
6363
};
6464

65-
$garbageCollectorStatus = new GarbageCollectorStatus(0, 0, 0, 0, false, false, false, 0);
65+
$garbageCollectorStatus = new GarbageCollectorStatus(0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, false, false, false, 0);
6666

6767
$garbageCollectorProvider = new class($garbageCollectorStatus) implements GarbageCollectorStatusProvider
6868
{

tests/unit/TextUI/Output/Default/ResultPrinterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ private static function telemetryInfo(): Info
532532
HRTime::fromSecondsAndNanoseconds(...hrtime(false)),
533533
MemoryUsage::fromBytes(1000),
534534
MemoryUsage::fromBytes(2000),
535-
new GarbageCollectorStatus(0, 0, 0, 0, false, false, false, 0),
535+
new GarbageCollectorStatus(0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0, false, false, false, 0),
536536
),
537537
Duration::fromSecondsAndNanoseconds(123, 456),
538538
MemoryUsage::fromBytes(2000),

0 commit comments

Comments
 (0)