1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AlecRabbit\Tools\Reports; |
6
|
|
|
|
7
|
|
|
use AlecRabbit\Tools\Benchmark; |
8
|
|
|
use AlecRabbit\Tools\Internal\BenchmarkFunction; |
9
|
|
|
use AlecRabbit\Tools\Internal\BenchmarkRelative; |
10
|
|
|
use AlecRabbit\Tools\Reports\Base\Report; |
11
|
|
|
use AlecRabbit\Tools\Traits\BenchmarkFields; |
12
|
|
|
use const AlecRabbit\Traits\Constants\DEFAULT_NAME; |
13
|
|
|
|
14
|
|
|
class BenchmarkReport extends Report |
15
|
|
|
{ |
16
|
|
|
use BenchmarkFields; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* BenchmarkReport constructor. |
20
|
|
|
* @param Benchmark $benchmark |
21
|
|
|
*/ |
22
|
3 |
|
public function __construct(Benchmark $benchmark) |
23
|
|
|
{ |
24
|
3 |
|
$this->profiler = $benchmark->getProfiler(); |
25
|
3 |
|
$this->doneIterations = $benchmark->getDoneIterations(); |
26
|
3 |
|
$this->functions = $this->updateFunctions($benchmark->getFunctions()); |
27
|
3 |
|
$this->timer = $benchmark->getTimer(); |
28
|
|
|
|
29
|
3 |
|
parent::__construct(); |
30
|
3 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param array $functions |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
3 |
|
private function updateFunctions(array $functions): array |
37
|
|
|
{ |
38
|
3 |
|
$averages = $this->computeAverages($functions); |
39
|
3 |
|
$relatives = $this->computeRelatives($averages); |
40
|
3 |
|
$updatedFunctions = []; |
41
|
3 |
|
if (!empty($relatives)) { |
42
|
2 |
|
$rank = 0; |
43
|
|
|
/** @var BenchmarkFunction $function */ |
44
|
2 |
|
foreach ($functions as $name => $function) { |
45
|
2 |
|
$relative = $relatives[$name] ?? null; |
46
|
2 |
|
$average = $averages[$name] ?? null; |
47
|
2 |
|
if (null !== $relative && null !== $average) { |
48
|
2 |
|
$function->setBenchmarkRelative( |
49
|
2 |
|
new BenchmarkRelative(++$rank, (float)$relative - 1, (float)$average) |
50
|
|
|
); |
51
|
|
|
} |
52
|
2 |
|
$updatedFunctions[$name] = $function; |
53
|
|
|
} |
54
|
|
|
} |
55
|
3 |
|
return $updatedFunctions; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param array $functions |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
3 |
|
private function computeAverages(array $functions): array |
63
|
|
|
{ |
64
|
3 |
|
$averages = []; |
65
|
|
|
/** @var BenchmarkFunction $f */ |
66
|
3 |
|
foreach ($functions as $f) { |
67
|
2 |
|
$timer = $f->getTimer(); |
68
|
2 |
|
if ((DEFAULT_NAME !== $name = $timer->getName()) |
69
|
2 |
|
&& 0.0 !== $avg = $timer->getAverageValue()) { |
70
|
2 |
|
$averages[$name] = $avg; |
71
|
|
|
} |
72
|
|
|
} |
73
|
3 |
|
return $averages; |
74
|
|
|
} |
75
|
|
|
|
76
|
3 |
|
private function computeRelatives(array $averages): array |
77
|
|
|
{ |
78
|
3 |
|
$rel = []; |
79
|
3 |
|
if (!empty($averages)) { |
80
|
2 |
|
$min = min($averages); |
81
|
|
|
|
82
|
2 |
|
foreach ($averages as $name => $average) { |
83
|
2 |
|
$rel[$name] = $average / $min; |
84
|
|
|
} |
85
|
2 |
|
asort($rel); |
86
|
|
|
} |
87
|
3 |
|
return $rel; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return BenchmarkFunction[] |
92
|
|
|
*/ |
93
|
2 |
|
public function getFunctions(): array |
94
|
|
|
{ |
95
|
2 |
|
return $this->functions; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|