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
|
|
|
/** @var array */ |
19
|
|
|
protected $relatives; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* BenchmarkReport constructor. |
23
|
|
|
* @param Benchmark $benchmark |
24
|
|
|
*/ |
25
|
3 |
|
public function __construct(Benchmark $benchmark) |
26
|
|
|
{ |
27
|
3 |
|
$this->profiler = $benchmark->getProfiler(); |
28
|
3 |
|
$this->doneIterations = $benchmark->getDoneIterations(); |
29
|
3 |
|
$this->functions = $this->updateFunctions($benchmark->getFunctions()); |
30
|
|
|
|
31
|
3 |
|
parent::__construct(); |
32
|
3 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param array $functions |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
3 |
|
private function updateFunctions(array $functions): array |
39
|
|
|
{ |
40
|
3 |
|
$averages = $this->computeAverages($functions); |
41
|
3 |
|
$relatives = $this->computeRelatives($averages); |
42
|
3 |
|
$updatedFunctions = []; |
43
|
3 |
|
if (!empty($relatives)) { |
44
|
2 |
|
$rank = 0; |
45
|
|
|
/** @var BenchmarkFunction $function */ |
46
|
2 |
|
foreach ($functions as $name => $function) { |
47
|
2 |
|
$relative = $relatives[$name] ?? null; |
48
|
2 |
|
$average = $averages[$name] ?? null; |
49
|
2 |
|
if (null !== $relative && null !== $average) { |
50
|
2 |
|
$function->setBenchmarkRelative( |
51
|
2 |
|
new BenchmarkRelative(++$rank, (float)$relative - 1, (float)$average) |
52
|
|
|
); |
53
|
|
|
} |
54
|
2 |
|
$updatedFunctions[$name] = $function; |
55
|
|
|
} |
56
|
|
|
} |
57
|
3 |
|
return $updatedFunctions; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array $functions |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
3 |
|
private function computeAverages(array $functions): array |
65
|
|
|
{ |
66
|
3 |
|
$averages = []; |
67
|
|
|
/** @var BenchmarkFunction $f */ |
68
|
3 |
|
foreach ($functions as $f) { |
69
|
2 |
|
$timer = $f->getTimer(); |
70
|
2 |
|
if ((DEFAULT_NAME !== $name = $timer->getName()) |
71
|
2 |
|
&& 0.0 !== $avg = $timer->getAverageValue()) { |
72
|
2 |
|
$averages[$name] = $avg; |
73
|
|
|
} |
74
|
|
|
} |
75
|
3 |
|
return $averages; |
76
|
|
|
} |
77
|
|
|
|
78
|
3 |
|
private function computeRelatives(array $averages): array |
79
|
|
|
{ |
80
|
3 |
|
$rel = []; |
81
|
3 |
|
if (!empty($averages)) { |
82
|
2 |
|
$min = min($averages); |
83
|
|
|
|
84
|
2 |
|
foreach ($averages as $name => $average) { |
85
|
2 |
|
$rel[$name] = $average / $min; |
86
|
|
|
} |
87
|
2 |
|
asort($rel); |
88
|
|
|
} |
89
|
3 |
|
return $rel; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return array[BenchmarkFunction] |
|
|
|
|
94
|
|
|
*/ |
95
|
2 |
|
public function getFunctions(): array |
96
|
|
|
{ |
97
|
2 |
|
return $this->functions; |
98
|
|
|
} |
99
|
|
|
// /** |
100
|
|
|
// * @param array $functions |
101
|
|
|
// * @return array |
102
|
|
|
// */ |
103
|
|
|
// private function updateFunctions(array $functions): array |
104
|
|
|
// { |
105
|
|
|
// $averages = $this->computeAverages($functions); |
106
|
|
|
// $relatives = []; |
107
|
|
|
// if (!empty($averages)) { |
108
|
|
|
// $min = min($averages); |
109
|
|
|
// |
110
|
|
|
// foreach ($averages as $name => $average) { |
111
|
|
|
// $rel[$name] = $average / $min; |
112
|
|
|
// } |
113
|
|
|
// asort($rel); |
114
|
|
|
// $rank = 0; |
115
|
|
|
// /** @var BenchmarkFunction $f */ |
116
|
|
|
// foreach ($rel as $name => $r) { |
117
|
|
|
// $f = $functions[$name]; |
118
|
|
|
// $relatives[$name] = |
119
|
|
|
// $f->setBenchmarkRelative( |
120
|
|
|
// new BenchmarkRelative(++$rank, (float)$r - 1, $averages[$name]) |
121
|
|
|
// ); |
122
|
|
|
// } |
123
|
|
|
// } |
124
|
|
|
// return $functions; |
125
|
|
|
// } |
126
|
|
|
} |
127
|
|
|
|