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