1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Tools; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\Accessories\MemoryUsage; |
6
|
|
|
use AlecRabbit\Accessories\Rewindable; |
7
|
|
|
use AlecRabbit\Tools\Contracts\BenchmarkInterface; |
8
|
|
|
use AlecRabbit\Tools\Contracts\Strings; |
9
|
|
|
use AlecRabbit\Tools\Internal\BenchmarkFunction; |
10
|
|
|
use AlecRabbit\Tools\Reports\BenchmarkReport; |
11
|
|
|
use AlecRabbit\Tools\Traits\BenchmarkFields; |
12
|
|
|
use function AlecRabbit\typeOf; |
13
|
|
|
|
14
|
|
|
class Benchmark extends Reportable implements BenchmarkInterface, Strings |
15
|
|
|
{ |
16
|
|
|
use BenchmarkFields; |
17
|
|
|
|
18
|
|
|
public const MIN_ITERATIONS = 100; |
19
|
|
|
public const DEFAULT_STEPS = 100; |
20
|
|
|
public const CLOSURE_NAME = 'λ'; |
21
|
|
|
|
22
|
|
|
/** @var int */ |
23
|
|
|
protected $advanceSteps = self::DEFAULT_STEPS; |
24
|
|
|
/** @var Rewindable */ |
25
|
|
|
protected $rewindable; |
26
|
|
|
/** @var int */ |
27
|
|
|
protected $iterations; |
28
|
|
|
/** @var null|string */ |
29
|
|
|
protected $comment; |
30
|
|
|
/** @var string|null */ |
31
|
|
|
protected $humanReadableName; |
32
|
|
|
/** @var int */ |
33
|
|
|
protected $totalIterations = 0; |
34
|
|
|
/** @var null|callable */ |
35
|
|
|
protected $onStart; |
36
|
|
|
/** @var null|callable */ |
37
|
|
|
protected $onAdvance; |
38
|
|
|
/** @var null|callable */ |
39
|
|
|
protected $onFinish; |
40
|
|
|
/** @var int */ |
41
|
|
|
protected $advanceStep = 0; |
42
|
|
|
/** @var \Closure */ |
43
|
|
|
protected $iterationNumberGenerator; |
44
|
|
|
/** @var bool */ |
45
|
|
|
protected $launched = false; |
46
|
|
|
/** @var int */ |
47
|
|
|
protected $functionIndex = 1; |
48
|
|
|
/** @var bool */ |
49
|
|
|
protected $silent = false; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Benchmark constructor. |
53
|
|
|
* @param null|int $iterations |
54
|
|
|
* @param null|bool $silent |
55
|
|
|
* @throws \Exception |
56
|
|
|
*/ |
57
|
20 |
|
public function __construct(?int $iterations = null, ?bool $silent = null) |
58
|
|
|
{ |
59
|
20 |
|
$this->iterations = $this->refineIterations($iterations); |
60
|
20 |
|
$this->silent = $silent ?? $this->silent; |
61
|
|
|
|
62
|
20 |
|
$this->iterationNumberGenerator = |
63
|
|
|
function (int $iterations, int $i = 1): \Generator { |
64
|
16 |
|
while ($i <= $iterations) { |
65
|
16 |
|
yield $i++; |
66
|
|
|
} |
67
|
16 |
|
}; |
68
|
|
|
|
69
|
20 |
|
$this->timer = new Timer(); // Timer to count benchmark process total time |
70
|
20 |
|
$this->initialize(); |
71
|
20 |
|
} |
72
|
|
|
|
73
|
20 |
|
protected function refineIterations(?int $iterations): int |
74
|
|
|
{ |
75
|
20 |
|
$iterations = $iterations ?? self::MIN_ITERATIONS; |
76
|
20 |
|
$this->assertIterations($iterations); |
77
|
20 |
|
return $iterations; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param int $iterations |
82
|
|
|
*/ |
83
|
20 |
|
protected function assertIterations(int $iterations): void |
84
|
|
|
{ |
85
|
20 |
|
if ($iterations < self::MIN_ITERATIONS) { |
86
|
1 |
|
throw new \RuntimeException( |
87
|
1 |
|
'[' . __CLASS__ . '] Number of Iterations should be greater than ' . self::MIN_ITERATIONS . '.' |
88
|
|
|
); |
89
|
|
|
} |
90
|
20 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Resets Benchmark object clear |
94
|
|
|
* @throws \Exception |
95
|
|
|
*/ |
96
|
20 |
|
protected function initialize(): void |
97
|
|
|
{ |
98
|
20 |
|
unset($this->functions, $this->humanReadableName, $this->rewindable, $this->memoryUsageReport); |
99
|
|
|
|
100
|
20 |
|
$this->rewindable = |
101
|
20 |
|
new Rewindable( |
102
|
20 |
|
$this->iterationNumberGenerator, |
103
|
20 |
|
$this->iterations |
104
|
|
|
); |
105
|
20 |
|
$this->humanReadableName = null; |
106
|
20 |
|
$this->functions = []; |
107
|
20 |
|
$this->functionIndex = 1; |
108
|
20 |
|
$this->launched = false; |
109
|
20 |
|
$this->resetComment(); |
110
|
20 |
|
$this->added = new SimpleCounter('added'); |
111
|
20 |
|
$this->benchmarked = new SimpleCounter('benchmarked'); |
112
|
20 |
|
$this->memoryUsageReport = MemoryUsage::report(); |
113
|
20 |
|
$this->doneIterations = 0; |
114
|
20 |
|
$this->totalIterations = 0; |
115
|
20 |
|
$this->report = (new BenchmarkReport())->buildOn($this); |
116
|
20 |
|
} |
117
|
|
|
|
118
|
20 |
|
protected function resetComment(): void |
119
|
|
|
{ |
120
|
20 |
|
$this->comment = null; |
121
|
20 |
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Resets Benchmark object clear |
125
|
|
|
* @throws \Exception |
126
|
|
|
*/ |
127
|
2 |
|
public function reset(): void |
128
|
|
|
{ |
129
|
2 |
|
$this->initialize(); |
130
|
2 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param callable|null $onStart |
134
|
|
|
* @param callable|null $onAdvance |
135
|
|
|
* @param callable|null $onFinish |
136
|
|
|
* @return Benchmark |
137
|
|
|
*/ |
138
|
4 |
|
public function showProgressBy( |
139
|
|
|
callable $onStart = null, |
140
|
|
|
callable $onAdvance = null, |
141
|
|
|
callable $onFinish = null |
142
|
|
|
): Benchmark { |
143
|
4 |
|
$this->onStart = $onStart; |
144
|
4 |
|
$this->onAdvance = $onAdvance; |
145
|
4 |
|
$this->onFinish = $onFinish; |
146
|
4 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param mixed $func |
151
|
|
|
* @param mixed ...$args |
152
|
|
|
* @throws \Exception |
153
|
|
|
*/ |
154
|
17 |
|
public function addFunction($func, ...$args): void |
155
|
|
|
{ |
156
|
17 |
|
if (!\is_callable($func, false, $name)) { |
157
|
1 |
|
throw new \InvalidArgumentException( |
158
|
1 |
|
sprintf( |
159
|
1 |
|
'\'%s\' is NOT callable. Function must be callable. Type of "%s" provided instead.', |
160
|
|
|
$name, |
161
|
1 |
|
typeOf($func) |
162
|
|
|
) |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
$function = |
166
|
16 |
|
new BenchmarkFunction( |
167
|
16 |
|
$func, |
168
|
16 |
|
$this->refineName($func, $name), |
169
|
16 |
|
$this->functionIndex++, |
170
|
|
|
$args, |
171
|
16 |
|
$this->comment, |
172
|
16 |
|
$this->humanReadableName |
173
|
|
|
); |
174
|
16 |
|
$function->setShowReturns($this->isShowReturns()); |
175
|
16 |
|
$this->functions[$function->enumeratedName()] = $function; |
176
|
16 |
|
$this->humanReadableName = null; |
177
|
16 |
|
$this->resetComment(); |
178
|
16 |
|
$this->added->bump(); |
179
|
16 |
|
$this->totalIterations += $this->iterations; |
180
|
16 |
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param callable $func |
184
|
|
|
* @param string $name |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
16 |
|
protected function refineName($func, $name): string |
188
|
|
|
{ |
189
|
16 |
|
if ($func instanceof \Closure) { |
190
|
16 |
|
$name = self::CLOSURE_NAME; |
191
|
|
|
} |
192
|
16 |
|
return $name; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param string $comment |
197
|
|
|
* @return Benchmark |
198
|
|
|
*/ |
199
|
6 |
|
public function withComment(string $comment): self |
200
|
|
|
{ |
201
|
6 |
|
$this->comment = $comment; |
202
|
6 |
|
return $this; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param string $name |
207
|
|
|
* @return Benchmark |
208
|
|
|
*/ |
209
|
7 |
|
public function useName(string $name): self |
210
|
|
|
{ |
211
|
7 |
|
$this->humanReadableName = $name; |
212
|
7 |
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @return string |
217
|
|
|
* @throws \Exception |
218
|
|
|
*/ |
219
|
4 |
|
public function stat(): string |
220
|
|
|
{ |
221
|
|
|
return |
222
|
4 |
|
sprintf( |
223
|
4 |
|
'Done in: %s%s%s', |
224
|
4 |
|
$this->getTimer()->elapsed(), |
225
|
4 |
|
PHP_EOL, |
226
|
4 |
|
(string)$this->memoryUsageReport |
227
|
|
|
); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @return Benchmark |
232
|
|
|
*/ |
233
|
1 |
|
public function showReturns(): Benchmark |
234
|
|
|
{ |
235
|
1 |
|
$this->setShowReturns(true); |
236
|
1 |
|
foreach ($this->functions as $function) { |
237
|
1 |
|
$function->setShowReturns($this->isShowReturns()); |
238
|
|
|
} |
239
|
1 |
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param bool $showReturns |
244
|
|
|
*/ |
245
|
1 |
|
public function setShowReturns(bool $showReturns): void |
246
|
|
|
{ |
247
|
1 |
|
$this->showReturns = $showReturns; |
248
|
1 |
|
} |
249
|
|
|
|
250
|
|
|
/** {@inheritdoc} */ |
251
|
15 |
|
protected function meetConditions(): void |
252
|
|
|
{ |
253
|
15 |
|
if ($this->isNotLaunched()) { |
254
|
10 |
|
$this->run(); |
255
|
|
|
} |
256
|
15 |
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @return bool |
260
|
|
|
*/ |
261
|
15 |
|
public function isNotLaunched(): bool |
262
|
|
|
{ |
263
|
15 |
|
return !$this->isLaunched(); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @return bool |
268
|
|
|
*/ |
269
|
15 |
|
public function isLaunched(): bool |
270
|
|
|
{ |
271
|
15 |
|
return $this->launched; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Launch benchmarking |
276
|
|
|
*/ |
277
|
17 |
|
public function run(): self |
278
|
|
|
{ |
279
|
17 |
|
$this->displayComment(); |
280
|
17 |
|
$this->launched = true; |
281
|
17 |
|
if ($this->onStart) { |
282
|
3 |
|
($this->onStart)(); |
283
|
|
|
} |
284
|
17 |
|
$this->execute(); |
285
|
17 |
|
if ($this->onFinish) { |
286
|
4 |
|
($this->onFinish)(); |
287
|
|
|
} |
288
|
17 |
|
$this->doneIterationsCombined += $this->doneIterations; |
289
|
17 |
|
return $this; |
290
|
|
|
} |
291
|
|
|
|
292
|
17 |
|
protected function displayComment(): void |
293
|
|
|
{ |
294
|
17 |
|
if (!$this->silent && null !== $this->comment) { |
295
|
|
|
$this->showComment($this->comment); |
296
|
|
|
$this->resetComment(); |
297
|
|
|
} |
298
|
17 |
|
} |
299
|
|
|
|
300
|
|
|
protected function showComment(string $comment = ''): void |
301
|
|
|
{ |
302
|
|
|
echo $comment . PHP_EOL; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Benchmarking |
307
|
|
|
*/ |
308
|
17 |
|
protected function execute(): void |
309
|
|
|
{ |
310
|
|
|
/** @var BenchmarkFunction $f */ |
311
|
17 |
|
foreach ($this->functions as $f) { |
312
|
16 |
|
if (!$f->execute()) { |
313
|
7 |
|
$this->totalIterations -= $this->iterations; |
314
|
7 |
|
continue; |
315
|
|
|
} |
316
|
16 |
|
$this->advanceStep = (int)($this->totalIterations / $this->advanceSteps); |
317
|
16 |
|
$this->bench($f); |
318
|
16 |
|
$this->benchmarked->bump(); |
319
|
|
|
} |
320
|
17 |
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @param BenchmarkFunction $f |
324
|
|
|
*/ |
325
|
16 |
|
protected function bench(BenchmarkFunction $f): void |
326
|
|
|
{ |
327
|
16 |
|
$timer = $f->getTimer(); |
328
|
16 |
|
$function = $f->getCallable(); |
329
|
16 |
|
$args = $f->getArgs(); |
330
|
16 |
|
foreach ($this->rewindable as $iteration) { |
331
|
16 |
|
$start = $timer->current(); |
332
|
|
|
/** @noinspection DisconnectedForeachInstructionInspection */ |
333
|
16 |
|
$function(...$args); |
334
|
16 |
|
$stop = $timer->current(); |
335
|
16 |
|
$timer->bounds($start, $stop, $iteration); |
336
|
|
|
/** @noinspection DisconnectedForeachInstructionInspection */ |
337
|
16 |
|
$this->progress(); |
338
|
|
|
} |
339
|
16 |
|
} |
340
|
|
|
|
341
|
16 |
|
protected function progress(): void |
342
|
|
|
{ |
343
|
16 |
|
$this->doneIterations++; |
344
|
16 |
|
if ($this->onAdvance && 0 === $this->doneIterations % $this->advanceStep) { |
345
|
4 |
|
($this->onAdvance)(); |
346
|
|
|
} |
347
|
16 |
|
} |
348
|
|
|
} |
349
|
|
|
|