|
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 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->totalIterations = $reportable->getTotalIterations(); |
|
34
|
2 |
|
$this->withResults = $reportable->isWithResults(); |
|
35
|
2 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
1 |
|
public function __toString(): string |
|
41
|
|
|
{ |
|
42
|
1 |
|
$profilerReport = (string)$this->getProfiler()->getReport(); |
|
43
|
1 |
|
$r = 'Benchmark:' . PHP_EOL; |
|
44
|
1 |
|
foreach ($this->computeRelatives() as $indexName => $result) { |
|
45
|
1 |
|
$function = $this->getFunctionObject($indexName); |
|
46
|
1 |
|
$arguments = $function->getArgs(); |
|
47
|
1 |
|
$types = []; |
|
48
|
1 |
|
if (!empty($arguments)) { |
|
49
|
1 |
|
foreach ($arguments as $argument) { |
|
50
|
1 |
|
$types[] = typeOf($argument); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
1 |
|
$r .= sprintf( |
|
54
|
1 |
|
'+%s [%s] %s(%s) %s %s', |
|
55
|
1 |
|
$result, |
|
56
|
1 |
|
$function->getIndex(), |
|
57
|
1 |
|
$function->getName(), |
|
58
|
1 |
|
implode(', ', $types), |
|
59
|
1 |
|
$function->getComment(), |
|
60
|
1 |
|
PHP_EOL |
|
61
|
|
|
); |
|
62
|
1 |
|
if ($this->withResults) { |
|
63
|
1 |
|
$r .= var_export($function->getResult(), true) . PHP_EOL; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
return |
|
67
|
1 |
|
$r . PHP_EOL . $profilerReport; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
1 |
|
private function computeRelatives(): array |
|
74
|
|
|
{ |
|
75
|
1 |
|
$averages = $this->computeAverages( |
|
76
|
1 |
|
$this->profiler->getTimers() |
|
77
|
|
|
); |
|
78
|
|
|
|
|
79
|
1 |
|
$min = min($averages); |
|
80
|
|
|
|
|
81
|
1 |
|
$relatives = []; |
|
82
|
1 |
|
foreach ($averages as $name => $average) { |
|
83
|
1 |
|
$relatives[$name] = $average / $min; |
|
84
|
|
|
} |
|
85
|
1 |
|
asort($relatives); |
|
86
|
|
|
|
|
87
|
1 |
|
foreach ($relatives as $name => $relative) { |
|
88
|
1 |
|
$relatives[$name] = |
|
89
|
1 |
|
$this->percent((float)$relative - 1) . ' ' . |
|
90
|
1 |
|
brackets(format_time($averages[$name]), BRACKETS_PARENTHESES); |
|
91
|
|
|
} |
|
92
|
1 |
|
return $relatives; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param array $timers |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
1 |
|
private function computeAverages(array $timers): array |
|
100
|
|
|
{ |
|
101
|
1 |
|
$averages = []; |
|
102
|
|
|
/** @var Timer $timer */ |
|
103
|
1 |
|
foreach ($timers as $timer) { |
|
104
|
1 |
|
if (DEFAULT_NAME !== $name = $timer->getName()) { |
|
105
|
1 |
|
$averages[$name] = $timer->getAverageValue(); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
1 |
|
return $averages; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param float $relative |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
1 |
|
private function percent(float $relative): string |
|
116
|
|
|
{ |
|
117
|
|
|
return |
|
118
|
1 |
|
number_format($relative * 100, 1) . '%'; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param string $name |
|
123
|
|
|
* @return BenchmarkFunction |
|
124
|
|
|
*/ |
|
125
|
1 |
|
private function getFunctionObject(string $name): BenchmarkFunction |
|
126
|
|
|
{ |
|
127
|
1 |
|
return $this->functions[$name]; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|