summaryrefslogtreecommitdiff
path: root/vendor/open-telemetry/sdk/Common/Time/StopWatch.php
blob: b2abdabae737b0ee63ade8316db7eaf1f850b9d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\Time;

final class StopWatch implements StopWatchInterface
{
    private const INITIAL_ELAPSED_TIME = 0;

    private ClockInterface $clock;
    private bool $running = false;
    private ?int $initialStartTime;
    private ?int $startTime = null;
    private ?int $stopTime = null;

    public function __construct(ClockInterface $clock, ?int $initialStartTime = null)
    {
        $this->clock = $clock;
        $this->initialStartTime = $initialStartTime;
    }

    public function isRunning(): bool
    {
        return $this->running;
    }

    public function start(): void
    {
        // resolve start time as early as possible
        $startTime = $this->time();

        if ($this->isRunning()) {
            return;
        }

        $this->startTime = $startTime;
        if (!$this->hasBeenStarted()) {
            $this->initialStartTime = $startTime;
        }
        $this->running = true;
    }

    public function stop(): void
    {
        if (!$this->isRunning()) {
            return;
        }

        $this->stopTime = $this->time();
        $this->running = false;
    }

    public function reset(): void
    {
        $this->startTime = $this->initialStartTime = $this->isRunning() ? $this->time() : null;
    }

    public function getElapsedTime(): int
    {
        if (!$this->hasBeenStarted()) {
            return self::INITIAL_ELAPSED_TIME;
        }

        return $this->calculateElapsedTime();
    }

    public function getLastElapsedTime(): int
    {
        if (!$this->hasBeenStarted()) {
            return self::INITIAL_ELAPSED_TIME;
        }

        return $this->calculateLastElapsedTime();
    }

    private function time(): int
    {
        return $this->clock->now();
    }

    private function hasBeenStarted(): bool
    {
        return $this->initialStartTime !== null;
    }

    private function calculateElapsedTime(): int
    {
        $referenceTime = $this->isRunning()
            ? $this->time()
            : $this->getStopTime();

        return $referenceTime - $this->getInitialStartTime();
    }

    private function calculateLastElapsedTime(): int
    {
        $referenceTime = $this->isRunning()
            ? $this->time()
            : $this->getStopTime();

        return $referenceTime - $this->getStartTime();
    }

    private function getInitialStartTime(): ?int
    {
        return $this->initialStartTime;
    }

    private function getStartTime(): ?int
    {
        return $this->startTime;
    }

    private function getStopTime(): ?int
    {
        return $this->stopTime;
    }
}