1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright (c) 2024 TEQneers GmbH & Co. KG |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE.md file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @see https://github.com/teqneers/phpunit-stopwatch |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace TQ\Testing\Extension\Stopwatch; |
15
|
|
|
|
16
|
|
|
use Psr\Clock\ClockInterface; |
17
|
|
|
use Symfony\Component\Clock\Clock; |
18
|
|
|
use TQ\Testing\Extension\Stopwatch\Exception\StopwatchException; |
19
|
|
|
|
20
|
|
|
final class TimingCollector |
21
|
|
|
{ |
22
|
|
|
private array $timing = []; |
23
|
|
|
private array $totalTiming = []; |
24
|
|
|
|
25
|
28 |
|
public function __construct( |
26
|
|
|
private readonly ClockInterface $clock = new Clock(), |
27
|
|
|
) { |
28
|
28 |
|
} |
29
|
|
|
|
30
|
5 |
|
public function reset(?string $name = null): void |
31
|
|
|
{ |
32
|
5 |
|
if (null !== $name) { |
33
|
1 |
|
unset($this->timing[$name]); |
34
|
|
|
} else { |
35
|
4 |
|
$this->timing = []; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
24 |
|
public function start(string $name): void |
40
|
|
|
{ |
41
|
24 |
|
$time = (float)$this->clock->now()->format('U.u'); |
42
|
|
|
|
43
|
|
|
// only start timing if it was not started yet |
44
|
24 |
|
if (!isset($this->timing[$name])) { |
45
|
24 |
|
$this->timing[$name] = [ |
46
|
24 |
|
'start' => $time, |
47
|
24 |
|
'end' => null, |
48
|
24 |
|
'duration' => null, |
49
|
|
|
// time means how many times the stopwatch for $name was used |
50
|
24 |
|
'times' => 0, |
51
|
24 |
|
]; |
52
|
|
|
|
53
|
24 |
|
if (!isset($this->totalTiming[$name])) { |
54
|
24 |
|
$this->totalTiming[$name] = [ |
55
|
24 |
|
'start' => $time, |
56
|
24 |
|
'end' => null, |
57
|
24 |
|
'duration' => null, |
58
|
|
|
// time means how many times the stopwatch for $name was used |
59
|
24 |
|
'times' => 0, |
60
|
24 |
|
]; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
19 |
|
public function stop(string $name, bool $silent = false): void |
66
|
|
|
{ |
67
|
19 |
|
$time = (float)$this->clock->now()->format('U.u'); |
68
|
|
|
|
69
|
19 |
|
if (!isset($this->timing[$name])) { |
70
|
2 |
|
if ($silent) { |
71
|
1 |
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
throw new StopwatchException("Stopwatch {$name} not started"); |
75
|
|
|
} |
76
|
|
|
|
77
|
17 |
|
$duration = $time - $this->timing[$name]['start']; |
78
|
17 |
|
$this->timing[$name]['end'] = $time; |
79
|
17 |
|
$this->timing[$name]['duration'] += $duration; |
80
|
17 |
|
++$this->timing[$name]['times']; |
81
|
|
|
|
82
|
17 |
|
$this->totalTiming[$name]['end'] = $time; |
83
|
17 |
|
$this->totalTiming[$name]['duration'] += $duration; |
84
|
17 |
|
++$this->totalTiming[$name]['times']; |
85
|
|
|
} |
86
|
|
|
|
87
|
23 |
|
public function isStarted(string $name): bool |
88
|
|
|
{ |
89
|
23 |
|
return isset($this->timing[$name]); |
90
|
|
|
} |
91
|
|
|
|
92
|
20 |
|
public function getTiming(?string $name = null): array |
93
|
|
|
{ |
94
|
20 |
|
if (null !== $name) { |
95
|
11 |
|
if (!isset($this->timing[$name])) { |
96
|
1 |
|
throw new StopwatchException("Stopwatch {$name} not started"); |
97
|
|
|
} |
98
|
|
|
|
99
|
10 |
|
return $this->timing[$name]; |
100
|
|
|
} |
101
|
|
|
|
102
|
9 |
|
return $this->timing; |
103
|
|
|
} |
104
|
|
|
|
105
|
16 |
|
public function getTotalTiming(?string $name = null): array |
106
|
|
|
{ |
107
|
16 |
|
if (null !== $name) { |
108
|
3 |
|
if (!isset($this->totalTiming[$name])) { |
109
|
2 |
|
throw new StopwatchException("Stopwatch {$name} not started"); |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
return $this->totalTiming[$name]; |
113
|
|
|
} |
114
|
|
|
|
115
|
13 |
|
return $this->totalTiming; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|