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\Reports\Base\Report; |
13
|
|
|
use AlecRabbit\Tools\Timer; |
14
|
|
|
use AlecRabbit\Tools\Traits\BenchmarkFields; |
15
|
|
|
use const AlecRabbit\Constants\Accessories\DEFAULT_NAME; |
|
|
|
|
16
|
|
|
|
17
|
|
|
class BenchmarkReport extends Report |
18
|
|
|
{ |
19
|
|
|
use BenchmarkFields; |
20
|
|
|
|
21
|
|
|
protected $relatives; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* BenchmarkReport constructor. |
25
|
|
|
* @param Benchmark $benchmark |
26
|
|
|
*/ |
27
|
|
|
public function __construct(Benchmark $benchmark) |
28
|
|
|
{ |
29
|
2 |
|
$this->profiler = $benchmark->getProfiler(); |
30
|
|
|
$this->functions = $benchmark->getFunctions(); |
31
|
2 |
|
$this->totalIterations = $benchmark->getTotalIterations(); |
32
|
2 |
|
$this->withResults = $benchmark->isWithResults(); |
33
|
2 |
|
$this->exceptionMessages = $benchmark->getExceptionMessages(); |
34
|
2 |
|
$this->relatives = $this->computeRelatives(); |
35
|
2 |
|
|
36
|
2 |
|
parent::__construct($benchmark); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return array |
41
|
1 |
|
*/ |
42
|
|
|
private function computeRelatives(): array |
43
|
1 |
|
{ |
44
|
1 |
|
$averages = $this->computeAverages($this->profiler->getTimers()); |
45
|
1 |
|
$relatives = []; |
46
|
1 |
|
if (!empty($averages)) { |
47
|
1 |
|
$min = min($averages); |
48
|
1 |
|
|
49
|
1 |
|
foreach ($averages as $name => $average) { |
50
|
1 |
|
$relatives[$name] = $average / $min; |
51
|
1 |
|
} |
52
|
|
|
asort($relatives); |
53
|
|
|
|
54
|
1 |
|
foreach ($relatives as $name => $relative) { |
55
|
1 |
|
$relatives[$name] = [(float)$relative - 1, $averages[$name]]; |
56
|
1 |
|
} |
57
|
1 |
|
} |
58
|
1 |
|
return $relatives; |
59
|
1 |
|
} |
60
|
1 |
|
|
61
|
|
|
/** |
62
|
1 |
|
* @param array $timers |
63
|
1 |
|
* @return array |
64
|
|
|
*/ |
65
|
|
|
private function computeAverages(array $timers): array |
66
|
1 |
|
{ |
67
|
|
|
$averages = []; |
68
|
|
|
/** @var Timer $timer */ |
69
|
|
|
foreach ($timers as $timer) { |
70
|
|
|
if (DEFAULT_NAME !== $name = $timer->getName()) { |
71
|
|
|
try { |
72
|
|
|
$averages[$name] = $timer->getAverageValue(); |
73
|
1 |
|
} catch (\Throwable $e) { |
|
|
|
|
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
return $averages; |
78
|
|
|
} |
79
|
1 |
|
|
80
|
|
|
/** |
81
|
1 |
|
* @param string $name |
82
|
1 |
|
* @return BenchmarkFunction |
83
|
|
|
*/ |
84
|
|
|
public function getFunctionObject(string $name): BenchmarkFunction |
85
|
1 |
|
{ |
86
|
|
|
return $this->functions[$name]; |
87
|
1 |
|
} |
88
|
1 |
|
|
89
|
1 |
|
/** |
90
|
|
|
* @return array |
91
|
1 |
|
*/ |
92
|
|
|
public function getRelatives(): array |
93
|
1 |
|
{ |
94
|
1 |
|
return $this->relatives; |
95
|
1 |
|
} |
96
|
|
|
} |
97
|
|
|
|