-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBenchmark.php
More file actions
159 lines (121 loc) · 4 KB
/
Benchmark.php
File metadata and controls
159 lines (121 loc) · 4 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
<?php
declare(strict_types=1);
namespace DragonCode\Benchmark;
use Closure;
use DragonCode\Benchmark\Exceptions\ValueIsNotCallableException;
use DragonCode\Benchmark\Services\Callbacks;
use DragonCode\Benchmark\Services\Runner;
use DragonCode\Benchmark\Services\View;
use DragonCode\Benchmark\Transformers\Transformer;
use DragonCode\Benchmark\View\ProgressBarView;
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 int $iterations = 100;
protected array $result = [
'each' => [],
'total' => [],
];
public function __construct(
protected Runner $runner = new Runner,
protected Transformer $transformer = new Transformer,
protected View $view = new View,
protected Callbacks $callbacks = new Callbacks,
) {}
public static function make(): static
{
return new static;
}
public function beforeEach(Closure $callback): self
{
$this->callbacks->beforeEach = $callback;
return $this;
}
public function afterEach(Closure $callback): self
{
$this->callbacks->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): static
{
$values = is_array($callbacks[0]) ? $callbacks[0] : func_get_args();
$this->withProgress($values, $this->stepsCount($values));
$this->show();
return $this;
}
/**
* @return \DragonCode\Benchmark\Data\ResultData[]
*/
public function toData(): array {}
public function toConsole(): void {}
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, ProgressBarView $progressBar): void
{
foreach ($callbacks as $name => $callback) {
$this->validate($callback);
$this->each($name, $callback, $progressBar);
}
}
protected function each(mixed $name, Closure $callback, ProgressBarView $progressBar): void
{
$this->result['total'][$name] = $this->call(
fn () => $this->run($name, $callback, $progressBar)
);
}
protected function run(mixed $name, Closure $callback, ProgressBarView $progressBar): void
{
for ($i = 1; $i <= $this->iterations; ++$i) {
$result = $this->callbacks->performBeforeEach($name, $i);
[$time, $ram] = $this->call($callback, [$i, $result]);
$this->callbacks->performAfterEach($name, $i, $time, $ram);
$this->push($name, $i, $time, $ram);
$progressBar->advance();
}
}
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));
}
}
}