Profile::samplePeakMemoryUsage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace RemotelyLiving\PHPDNS\Observability\Performance;
4
5
use RemotelyLiving\PHPDNS\Observability\Performance\Interfaces\ProfileInterface;
6
use RemotelyLiving\PHPDNS\Observability\Performance\Interfaces\Time;
7
8
use function memory_get_peak_usage;
9
10
final class Profile implements ProfileInterface
11
{
12
    private float $startTime = 0.0;
13
14
    private float $stopTime = 0.0;
15
16
    private int $peakMemoryUsage = 0;
17
18
    private Time $time;
19
20
    public function __construct(private string $transactionName, Time $time = null)
21
    {
22
        $this->time = $time ?? new Timer();
23
    }
24
25
    public function startTransaction(): void
26
    {
27
        $this->startTime = $this->time->getMicroTime();
28
    }
29
30
    public function endTransaction(): void
31
    {
32
        $this->stopTime = $this->time->getMicroTime();
33
    }
34
35
    public function getTransactionName(): string
36
    {
37
        return $this->transactionName;
38
    }
39
40
    public function getElapsedSeconds(): float
41
    {
42
        return $this->stopTime - $this->startTime;
43
    }
44
45
    public function samplePeakMemoryUsage(): void
46
    {
47
        $this->peakMemoryUsage = memory_get_peak_usage();
48
    }
49
50
    public function getPeakMemoryUsage(): int
51
    {
52
        return $this->peakMemoryUsage;
53
    }
54
}
55