-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBenchmark.php
More file actions
172 lines (132 loc) · 4.31 KB
/
Benchmark.php
File metadata and controls
172 lines (132 loc) · 4.31 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
declare(strict_types=1);
namespace DragonCode\Benchmark;
use Closure;
use DragonCode\Benchmark\Exceptions\ValueIsNotCallableException;
use DragonCode\Benchmark\Services\Runner;
use DragonCode\Benchmark\Services\View;
use DragonCode\Benchmark\Transformers\Transformer;
use Symfony\Component\Console\Helper\ProgressBar as ProgressBarService;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Style\SymfonyStyle;
use function count;
use function func_get_args;
use function gettype;
use function is_array;
use function is_callable;
use function max;
class Benchmark
{
protected View $view;
protected int $iterations = 100;
protected ?Closure $beforeEach = null;
protected ?Closure $afterEach = null;
protected array $result = [
'each' => [],
'total' => [],
];
public function __construct(
protected Runner $runner = new Runner,
protected Transformer $transformer = new Transformer
) {
$this->view = new View(
new SymfonyStyle(
new ArgvInput,
new ConsoleOutput
)
);
}
public static function start(): static
{
return new static;
}
public function beforeEach(Closure $callback): self
{
$this->beforeEach = $callback;
return $this;
}
public function afterEach(Closure $callback): self
{
$this->afterEach = $callback;
return $this;
}
public function iterations(int $count): self
{
$this->iterations = max(1, $count);
return $this;
}
public function round(?int $precision): self
{
$this->view->setRound($precision);
return $this;
}
public function compare(array|Closure ...$callbacks): void
{
$values = is_array($callbacks[0]) ? $callbacks[0] : func_get_args();
$this->withProgress($values, $this->stepsCount($values));
$this->show();
}
protected function withProgress(array $callbacks, int $count): void
{
$bar = $this->view->progressBar()->create($count);
$this->chunks($callbacks, $bar);
$bar->finish();
$this->view->emptyLine(2);
}
protected function stepsCount(array $callbacks): int
{
return count($callbacks) * $this->iterations;
}
protected function chunks(array $callbacks, ProgressBarService $progressBar): void
{
foreach ($callbacks as $name => $callback) {
$this->validate($callback);
$this->each($name, $callback, $progressBar);
}
}
protected function each(mixed $name, Closure $callback, ProgressBarService $progressBar): void
{
$this->result['total'][$name] = $this->call(
fn () => $this->run($name, $callback, $progressBar)
);
}
protected function run(mixed $name, Closure $callback, ProgressBarService $progressBar): void
{
for ($i = 1; $i <= $this->iterations; ++$i) {
$result = $this->runCallback($this->beforeEach, $name, $i);
[$time, $ram] = $this->call($callback, [$i, $result]);
$this->runCallback($this->afterEach, $name, $i, $time, $ram);
$this->push($name, $i, $time, $ram);
$progressBar->advance();
}
}
protected function runCallback(?Closure $callback, mixed ...$arguments): mixed
{
if (! $callback) {
return null;
}
return $callback(...$arguments);
}
protected function call(Closure $callback, array $parameters = []): array
{
return $this->runner->call($callback, $parameters);
}
protected function push(mixed $name, int $iteration, float $time, float $ram): void
{
$this->result['each'][$name][$iteration]['time'] = $time;
$this->result['each'][$name][$iteration]['ram'] = $ram;
}
protected function show(): void
{
$stats = $this->transformer->forStats($this->result);
$winner = $this->transformer->forWinners($stats);
$this->view->table($this->transformer->merge($stats, $winner));
}
protected function validate(mixed $callback): void
{
if (! is_callable($callback)) {
throw new ValueIsNotCallableException(gettype($callback));
}
}
}