|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace AlecRabbit\Tools; |
|
4
|
|
|
|
|
5
|
|
|
use AlecRabbit\Tools\Contracts\TimerInterface; |
|
6
|
|
|
use AlecRabbit\Tools\Reports\Contracts\ReportableInterface; |
|
7
|
|
|
use AlecRabbit\Tools\Reports\Factory; |
|
8
|
|
|
use AlecRabbit\Tools\Reports\TimerReport; |
|
9
|
|
|
use AlecRabbit\Tools\Reports\Traits\HasReport; |
|
10
|
|
|
use AlecRabbit\Tools\Traits\TimerFields; |
|
11
|
|
|
|
|
12
|
|
|
abstract class AbstractTimer implements TimerInterface, ReportableInterface |
|
13
|
|
|
{ |
|
14
|
|
|
use TimerFields, HasReport; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Timer constructor. |
|
18
|
|
|
* @param null|string $name |
|
19
|
|
|
* @param bool $start |
|
20
|
|
|
* @throws \Exception |
|
21
|
|
|
*/ |
|
22
|
63 |
|
public function __construct(?string $name = null, bool $start = true) |
|
23
|
|
|
{ |
|
24
|
63 |
|
$this->checkEnvironment(); |
|
25
|
63 |
|
$this->name = $this->defaultName($name); |
|
26
|
63 |
|
$this->creationTime = new \DateTimeImmutable(); |
|
27
|
63 |
|
$this->computeElapsed(); |
|
28
|
63 |
|
$this->report = (new TimerReport())->buildOn($this); |
|
29
|
63 |
|
if ($start) { |
|
30
|
53 |
|
$this->start(); |
|
31
|
|
|
} |
|
32
|
63 |
|
} |
|
33
|
|
|
|
|
34
|
42 |
|
protected function checkEnvironment(): void |
|
35
|
|
|
{ |
|
36
|
42 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @throws \Exception |
|
40
|
|
|
*/ |
|
41
|
63 |
|
protected function computeElapsed(): void |
|
42
|
|
|
{ |
|
43
|
63 |
|
$this->elapsed = (new \DateTimeImmutable())->diff($this->creationTime); |
|
44
|
63 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Starts the timer. |
|
48
|
|
|
* |
|
49
|
|
|
* @return void |
|
50
|
|
|
*/ |
|
51
|
56 |
|
public function start(): void |
|
52
|
|
|
{ |
|
53
|
56 |
|
if ($this->isNotStarted()) { |
|
54
|
56 |
|
$this->previous = $this->current(); |
|
55
|
|
|
} |
|
56
|
56 |
|
$this->started = true; |
|
57
|
56 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
abstract public function current(); |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Marks the time. |
|
63
|
|
|
* If timer was not started starts the timer. |
|
64
|
|
|
* @param int|null $iterationNumber |
|
65
|
|
|
* @return self |
|
66
|
|
|
*/ |
|
67
|
6 |
|
public function check(?int $iterationNumber = null): self |
|
68
|
|
|
{ |
|
69
|
6 |
|
if ($this->isStopped()) { |
|
70
|
1 |
|
throw new \RuntimeException('Timer[' . $this->name . '] is already stopped.'); |
|
71
|
|
|
} |
|
72
|
6 |
|
if ($this->isNotStarted()) { |
|
73
|
2 |
|
$this->start(); |
|
74
|
|
|
} else { |
|
75
|
6 |
|
$this->mark($iterationNumber); |
|
76
|
|
|
} |
|
77
|
6 |
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param int|null $iterationNumber |
|
82
|
|
|
*/ |
|
83
|
6 |
|
protected function mark(?int $iterationNumber = null): void |
|
84
|
|
|
{ |
|
85
|
6 |
|
$current = $this->current(); |
|
86
|
6 |
|
$this->currentValue = $current - $this->previous; |
|
87
|
6 |
|
$this->previous = $current; |
|
88
|
|
|
|
|
89
|
6 |
|
$this->compute($iterationNumber); |
|
90
|
6 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param null|int $iterationNumber |
|
94
|
|
|
*/ |
|
95
|
24 |
|
protected function compute(?int $iterationNumber): void |
|
96
|
|
|
{ |
|
97
|
24 |
|
if (0 !== $this->count) { |
|
98
|
23 |
|
++$this->count; |
|
99
|
23 |
|
$this->checkMinValue($iterationNumber); |
|
100
|
23 |
|
$this->checkMaxValue($iterationNumber); |
|
101
|
23 |
|
$this->computeAverage(); |
|
102
|
|
|
} else { |
|
103
|
24 |
|
$this->initValues(); |
|
104
|
|
|
} |
|
105
|
24 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param null|int $iterationNumber |
|
109
|
|
|
*/ |
|
110
|
23 |
|
protected function checkMinValue(?int $iterationNumber): void |
|
111
|
|
|
{ |
|
112
|
23 |
|
if ($this->currentValue < $this->minValue) { |
|
113
|
4 |
|
$this->minValue = $this->currentValue; |
|
114
|
4 |
|
$this->minValueIteration = $iterationNumber ?? $this->count; |
|
115
|
|
|
} |
|
116
|
23 |
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param null|int $iterationNumber |
|
120
|
|
|
*/ |
|
121
|
23 |
|
protected function checkMaxValue(?int $iterationNumber): void |
|
122
|
|
|
{ |
|
123
|
23 |
|
if ($this->currentValue > $this->maxValue) { |
|
124
|
7 |
|
$this->maxValue = $this->currentValue; |
|
125
|
7 |
|
$this->maxValueIteration = $iterationNumber ?? $this->count; |
|
126
|
|
|
} |
|
127
|
23 |
|
} |
|
128
|
|
|
|
|
129
|
23 |
|
protected function computeAverage(): void |
|
130
|
|
|
{ |
|
131
|
23 |
|
$this->avgValue = (($this->avgValue * ($this->count - 1)) + $this->currentValue) / $this->count; |
|
132
|
23 |
|
} |
|
133
|
|
|
|
|
134
|
24 |
|
protected function initValues(): void |
|
135
|
|
|
{ |
|
136
|
24 |
|
$this->maxValueIteration = $this->minValueIteration = $this->count = 1; |
|
137
|
24 |
|
$this->maxValue = $this->currentValue; |
|
138
|
24 |
|
$this->minValue = $this->currentValue; |
|
139
|
24 |
|
$this->avgValue = $this->currentValue; |
|
140
|
24 |
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Stops the timer and returns elapsed time string. |
|
144
|
|
|
* @return string |
|
145
|
|
|
* @throws \Exception |
|
146
|
|
|
*/ |
|
147
|
6 |
|
public function elapsed(): string |
|
148
|
|
|
{ |
|
149
|
|
|
// if ($this->isNotStarted()) { // todo investigate if it's needed here |
|
150
|
|
|
// $this->start(); |
|
151
|
|
|
// } |
|
152
|
6 |
|
if ($this->isNotStopped()) { |
|
153
|
6 |
|
$this->stop(); |
|
154
|
|
|
} |
|
155
|
|
|
return |
|
156
|
6 |
|
$this->formattedElapsed(); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Stops the timer. |
|
161
|
|
|
* @throws \Exception |
|
162
|
|
|
*/ |
|
163
|
7 |
|
public function stop(): void |
|
164
|
|
|
{ |
|
165
|
7 |
|
$this->computeElapsed(); |
|
166
|
7 |
|
$this->stopped = true; |
|
167
|
7 |
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @return string |
|
171
|
|
|
* @throws \Exception |
|
172
|
|
|
*/ |
|
173
|
6 |
|
protected function formattedElapsed(): string |
|
174
|
|
|
{ |
|
175
|
|
|
return |
|
176
|
6 |
|
Factory::getTimerReportFormatter()::formatElapsed($this->elapsed); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @return \DateInterval |
|
181
|
|
|
* @throws \Exception |
|
182
|
|
|
*/ |
|
183
|
63 |
|
public function getElapsed(): \DateInterval |
|
184
|
|
|
{ |
|
185
|
63 |
|
if ($this->isNotStopped()) { |
|
186
|
63 |
|
$this->computeElapsed(); |
|
187
|
|
|
} |
|
188
|
63 |
|
return $this->elapsed; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @param int|float $start |
|
193
|
|
|
* @param int|float $stop |
|
194
|
|
|
* @param null|int $iterationNumber |
|
195
|
|
|
* @return self |
|
196
|
|
|
*/ |
|
197
|
22 |
|
public function bounds($start, $stop, ?int $iterationNumber = null): self |
|
198
|
|
|
{ |
|
199
|
22 |
|
$this->assertStartAndStop($start, $stop); |
|
200
|
18 |
|
if ($this->isNotStarted()) { |
|
201
|
1 |
|
$this->start(); |
|
202
|
|
|
} |
|
203
|
18 |
|
$this->updateCurrentAndPrevious($start, $stop); |
|
204
|
|
|
|
|
205
|
18 |
|
$this->compute($iterationNumber); |
|
206
|
18 |
|
return $this; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
abstract protected function assertStartAndStop($start, $stop): void; |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @param float $start |
|
213
|
|
|
* @param float $stop |
|
214
|
|
|
*/ |
|
215
|
18 |
|
protected function updateCurrentAndPrevious($start, $stop): void |
|
216
|
|
|
{ |
|
217
|
18 |
|
$this->currentValue = $stop - $start; |
|
218
|
18 |
|
$this->previous = $stop; |
|
219
|
18 |
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* {@inheritdoc} |
|
223
|
|
|
* @throws \Exception |
|
224
|
|
|
*/ |
|
225
|
14 |
|
protected function beforeReport(): void |
|
226
|
|
|
{ |
|
227
|
|
|
// if ($this->isNotStarted()) { |
|
228
|
|
|
// $this->start(); |
|
229
|
|
|
// $this->mark(); |
|
230
|
|
|
// } |
|
231
|
|
|
// $this->stop(); |
|
232
|
14 |
|
} |
|
233
|
|
|
} |
|
234
|
|
|
|