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