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\BenchmarkedFunction; |
12
|
|
|
use AlecRabbit\Tools\Reports\Base\Report; |
13
|
|
|
use AlecRabbit\Tools\Timer; |
14
|
|
|
use AlecRabbit\Tools\Traits\BenchmarkFields; |
15
|
|
|
use function AlecRabbit\brackets; |
16
|
|
|
use function AlecRabbit\format_time; |
17
|
|
|
use function AlecRabbit\typeOf; |
18
|
|
|
use const AlecRabbit\Constants\Accessories\DEFAULT_NAME; |
|
|
|
|
19
|
|
|
use const AlecRabbit\Constants\BRACKETS_PARENTHESES; |
|
|
|
|
20
|
|
|
|
21
|
|
|
class BenchmarkReport extends Report |
22
|
|
|
{ |
23
|
|
|
use BenchmarkFields; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* BenchmarkReport constructor. |
27
|
|
|
* @param Benchmark $reportable |
28
|
|
|
*/ |
29
|
2 |
|
public function __construct(Benchmark $reportable) |
30
|
|
|
{ |
31
|
2 |
|
$this->profiler = $reportable->getProfiler(); |
32
|
2 |
|
$this->functions = $reportable->getFunctions(); |
33
|
2 |
|
$this->iteration = $reportable->getIteration(); |
34
|
2 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
1 |
|
public function __toString(): string |
40
|
|
|
{ |
41
|
1 |
|
$r = (string)$this->getProfiler()->report(); |
42
|
1 |
|
$r .= 'Benchmark:' . PHP_EOL; |
43
|
1 |
|
foreach ($this->computeRelatives() as $indexName => $result) { |
44
|
1 |
|
$function = $this->getFunctionObject($indexName); |
45
|
1 |
|
$arguments = $function->getArgs(); |
46
|
1 |
|
$types = []; |
47
|
1 |
|
if (!empty($arguments)) { |
48
|
1 |
|
foreach ($arguments as $argument) { |
49
|
1 |
|
$types[] = typeOf($argument); |
50
|
|
|
} |
51
|
|
|
} |
52
|
1 |
|
$r .= sprintf( |
53
|
1 |
|
'+%s [%s] %s(%s) %s %s %s', |
54
|
1 |
|
$result, |
55
|
1 |
|
$function->getIndex(), |
56
|
1 |
|
$function->getName(), |
57
|
1 |
|
implode(', ', $types), |
58
|
1 |
|
$function->getComment(), |
59
|
1 |
|
$this->getIteration(), |
60
|
1 |
|
PHP_EOL |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
return |
64
|
1 |
|
$r; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
1 |
|
private function computeRelatives(): array |
71
|
|
|
{ |
72
|
1 |
|
$averages = $this->computeAverages( |
73
|
1 |
|
$this->profiler->getTimers() |
74
|
|
|
); |
75
|
|
|
|
76
|
1 |
|
$min = min($averages); |
77
|
|
|
|
78
|
1 |
|
$relatives = []; |
79
|
1 |
|
foreach ($averages as $name => $average) { |
80
|
1 |
|
$relatives[$name] = $average / $min; |
81
|
|
|
} |
82
|
1 |
|
asort($relatives); |
83
|
|
|
|
84
|
1 |
|
foreach ($relatives as $name => $relative) { |
85
|
1 |
|
$relatives[$name] = |
86
|
1 |
|
$this->percent((float)$relative - 1) . ' ' . |
87
|
1 |
|
brackets(format_time($averages[$name]), BRACKETS_PARENTHESES); |
88
|
|
|
} |
89
|
1 |
|
return $relatives; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param array $timers |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
1 |
|
private function computeAverages(array $timers): array |
97
|
|
|
{ |
98
|
1 |
|
$averages = []; |
99
|
|
|
/** @var Timer $timer */ |
100
|
1 |
|
foreach ($timers as $timer) { |
101
|
1 |
|
if (DEFAULT_NAME !== $name = $timer->getName()) { |
102
|
1 |
|
$averages[$name] = $timer->getAverageValue(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
1 |
|
return $averages; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param float $relative |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
1 |
|
private function percent(float $relative): string |
113
|
|
|
{ |
114
|
|
|
return |
115
|
1 |
|
number_format($relative * 100, 1) . '%'; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $name |
120
|
|
|
* @return BenchmarkedFunction |
121
|
|
|
*/ |
122
|
1 |
|
private function getFunctionObject(string $name): BenchmarkedFunction |
123
|
|
|
{ |
124
|
1 |
|
return $this->functions[$name]; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|