1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AlecRabbit\Tools\Reports; |
6
|
|
|
|
7
|
|
|
use AlecRabbit\Exception\InvalidStyleException; |
8
|
|
|
use AlecRabbit\Tools\Benchmark; |
9
|
|
|
use AlecRabbit\Tools\Internal\BenchmarkFunction; |
10
|
|
|
use AlecRabbit\Tools\Internal\BenchmarkRelative; |
11
|
|
|
use AlecRabbit\Tools\Reports\Base\Report; |
12
|
|
|
use AlecRabbit\Tools\Timer; |
13
|
|
|
use AlecRabbit\Tools\Traits\BenchmarkFields; |
14
|
|
|
use const AlecRabbit\Traits\Constants\DEFAULT_NAME; |
15
|
|
|
|
16
|
|
|
class BenchmarkReport extends Report |
17
|
|
|
{ |
18
|
|
|
use BenchmarkFields; |
19
|
|
|
|
20
|
|
|
/** @var array */ |
21
|
|
|
protected $relatives; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* BenchmarkReport constructor. |
25
|
|
|
* @param Benchmark $benchmark |
26
|
|
|
* @throws InvalidStyleException |
27
|
|
|
*/ |
28
|
4 |
|
public function __construct(Benchmark $benchmark) |
29
|
|
|
{ |
30
|
4 |
|
$this->profiler = $benchmark->getProfiler(); |
31
|
4 |
|
$this->functions = $benchmark->getFunctions(); |
32
|
4 |
|
$this->totalIterations = $benchmark->getTotalIterations(); |
33
|
4 |
|
$this->withResults = $benchmark->isWithResults(); |
34
|
4 |
|
$this->exceptionMessages = $benchmark->getExceptionMessages(); |
35
|
4 |
|
$this->exceptions = $benchmark->getExceptions(); |
36
|
4 |
|
$this->relatives = $this->computeRelatives(); |
37
|
|
|
|
38
|
4 |
|
parent::__construct(); |
39
|
4 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
4 |
|
private function computeRelatives(): array |
45
|
|
|
{ |
46
|
4 |
|
$averages = $this->computeAverages($this->getTimers()); |
47
|
4 |
|
$relatives = []; |
48
|
4 |
|
if (!empty($averages)) { |
49
|
2 |
|
$min = min($averages); |
50
|
|
|
|
51
|
2 |
|
foreach ($averages as $name => $average) { |
52
|
2 |
|
$relatives[$name] = $average / $min; |
53
|
|
|
} |
54
|
2 |
|
asort($relatives); |
55
|
|
|
|
56
|
|
|
/** @var float|int $relative */ |
57
|
2 |
|
foreach ($relatives as $name => $relative) { |
58
|
2 |
|
$relatives[$name] = new BenchmarkRelative((float)$relative - 1, $averages[$name]); |
59
|
|
|
} |
60
|
|
|
} |
61
|
4 |
|
return $relatives; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param array $timers |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
4 |
|
private function computeAverages(array $timers): array |
69
|
|
|
{ |
70
|
4 |
|
$averages = []; |
71
|
|
|
/** @var Timer $timer */ |
72
|
4 |
|
foreach ($timers as $timer) { |
73
|
2 |
|
if (DEFAULT_NAME !== $name = $timer->getName()) { |
74
|
|
|
try { |
75
|
2 |
|
$averages[$name] = $timer->getAverageValue(); |
76
|
2 |
|
} catch (\Throwable $e) { |
77
|
|
|
// no action |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
4 |
|
return $averages; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $name |
86
|
|
|
* @return BenchmarkFunction |
87
|
|
|
*/ |
88
|
2 |
|
public function getFunctionObject(string $name): BenchmarkFunction |
89
|
|
|
{ |
90
|
2 |
|
return $this->functions[$name]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
public function getFunctionObjects(): array |
97
|
|
|
{ |
98
|
|
|
return $this->functions; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
3 |
|
public function getRelatives(): array |
105
|
|
|
{ |
106
|
3 |
|
return $this->relatives; |
107
|
|
|
} |
108
|
|
|
|
109
|
4 |
|
private function getTimers(): array |
110
|
|
|
{ |
111
|
4 |
|
$timers = []; |
112
|
|
|
/** @var BenchmarkFunction $f */ |
113
|
4 |
|
foreach ($this->functions as $f) { |
114
|
2 |
|
$timers[]=$f->getTimer(); |
115
|
|
|
} |
116
|
4 |
|
return $timers; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|