-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStats.php
More file actions
81 lines (65 loc) · 1.96 KB
/
Stats.php
File metadata and controls
81 lines (65 loc) · 1.96 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
<?php
declare(strict_types=1);
namespace DragonCode\Benchmark\Transformers;
use DragonCode\Benchmark\Services\Arr;
use DragonCode\Benchmark\Services\MeasurementError;
use function array_sum;
use function call_user_func;
use function count;
use function max;
use function min;
class Stats extends Base
{
protected array $methods = [
'min',
'max',
'avg',
];
public function __construct(
protected MeasurementError $measurementError = new MeasurementError,
protected Arr $arr = new Arr
) {}
public function transform(array $data): array
{
return $this->calculate($data);
}
protected function calculate(array $data): array
{
$items = [];
foreach ($data['each'] as $name => $iterations) {
foreach ($this->methods as $method) {
$this->put($items, $method, $name, fn () => call_user_func([$this, $method], $iterations));
}
}
foreach ($data['total'] as $name => $value) {
$this->put($items, 'total', $name, fn () => ['time' => $value[0]]);
}
return $items;
}
protected function min(array $values): array
{
return [
'time' => min($this->arr->pluck($values, 'time')),
'ram' => min($this->arr->pluck($values, 'ram')),
];
}
protected function max(array $values): array
{
return [
'time' => max($this->arr->pluck($values, 'time')),
'ram' => max($this->arr->pluck($values, 'ram')),
];
}
protected function avg(array $values): array
{
$values = $this->filter($values);
return [
'time' => array_sum($this->arr->pluck($values, 'time')) / count($values),
'ram' => array_sum($this->arr->pluck($values, 'ram')) / count($values),
];
}
protected function filter(array $values): array
{
return $this->measurementError->filter($values);
}
}