1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Tools\Reports\Formatters; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\Tools\Internal\BenchmarkFunction; |
6
|
|
|
use AlecRabbit\Tools\Reports\BenchmarkReport; |
7
|
|
|
use AlecRabbit\Tools\Reports\Factory; |
8
|
|
|
use function AlecRabbit\array_is_homogeneous; |
9
|
|
|
|
10
|
|
|
class BenchmarkReportFormatter extends ReportFormatter |
11
|
|
|
{ |
12
|
|
|
/** @var BenchmarkReport */ |
13
|
|
|
protected $report; |
14
|
|
|
/** @var mixed */ |
15
|
|
|
protected $lastReturn; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* {@inheritdoc} |
19
|
|
|
*/ |
20
|
|
|
public function process(): string |
21
|
4 |
|
{ |
22
|
|
|
$str = 'Results:' . PHP_EOL; |
23
|
4 |
|
$added = $this->added(); |
24
|
4 |
|
$benchmarked = $this->benchmarked(); |
25
|
|
|
$benchmarkedAny = $this->benchmarkedAny($added, $benchmarked); |
26
|
4 |
|
if ($benchmarkedAny) { |
27
|
|
|
$str .= self::BENCHMARK . PHP_EOL; |
28
|
4 |
|
} |
29
|
4 |
|
$equalReturns = $this->checkReturns(); |
30
|
4 |
|
/** @var BenchmarkFunction $function */ |
31
|
|
|
foreach ($this->report->getFunctions() as $name => $function) { |
32
|
|
|
$str .= |
33
|
4 |
|
Factory::getBenchmarkFunctionFormatter($function) |
34
|
4 |
|
->noResultsIf($equalReturns) |
35
|
4 |
|
->process(); |
36
|
4 |
|
} |
37
|
4 |
|
return |
38
|
4 |
|
sprintf( |
39
|
4 |
|
'%s%s%s%s%s', |
40
|
|
|
$str, |
41
|
|
|
$benchmarkedAny ? $this->allReturnsAreEqual($equalReturns) : '', |
42
|
|
|
$this->countersStatistics($added, $benchmarked), |
43
|
|
|
$this->report->getMemoryUsageReport(), |
44
|
|
|
PHP_EOL |
45
|
|
|
); |
46
|
4 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
4 |
|
* @return int |
50
|
|
|
*/ |
51
|
|
|
private function added(): int |
52
|
|
|
{ |
53
|
|
|
return |
54
|
|
|
$this->report->getProfiler() |
55
|
4 |
|
->counter(static::ADDED)->getValue(); |
56
|
|
|
} |
57
|
4 |
|
|
58
|
|
|
/** |
59
|
4 |
|
* @return int |
60
|
4 |
|
*/ |
61
|
|
|
private function benchmarked(): int |
62
|
4 |
|
{ |
63
|
|
|
return |
64
|
|
|
$this->report->getProfiler() |
65
|
4 |
|
->counter(static::BENCHMARKED)->getValue(); |
66
|
|
|
} |
67
|
4 |
|
|
68
|
3 |
|
/** |
69
|
|
|
* @param int $added |
70
|
|
|
* @param int $benchmarked |
71
|
1 |
|
* @return bool |
72
|
1 |
|
*/ |
73
|
1 |
|
private function benchmarkedAny(int $added, int $benchmarked): bool |
74
|
1 |
|
{ |
75
|
1 |
|
return $added !== $added - $benchmarked; |
76
|
1 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
protected function checkReturns(): bool |
82
|
|
|
{ |
83
|
|
|
return |
84
|
|
|
array_is_homogeneous($this->functionsReturns()); |
85
|
4 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
4 |
|
* @return array |
89
|
4 |
|
*/ |
90
|
|
|
private function functionsReturns(): array |
91
|
4 |
|
{ |
92
|
4 |
|
$returns = []; |
93
|
4 |
|
/** @var BenchmarkFunction $function */ |
94
|
1 |
|
foreach ($this->report->getFunctions() as $name => $function) { |
95
|
|
|
$returns[] = $this->lastReturn = $function->getReturn(); |
96
|
|
|
} |
97
|
|
|
return $returns; |
98
|
3 |
|
} |
99
|
3 |
|
|
100
|
3 |
|
private function allReturnsAreEqual(bool $equalReturns): string |
101
|
3 |
|
{ |
102
|
3 |
|
if ($equalReturns) { |
103
|
3 |
|
return |
104
|
3 |
|
sprintf( |
105
|
3 |
|
'%s %s%s %s', |
106
|
|
|
'All returns are equal:', |
107
|
|
|
PHP_EOL, |
108
|
|
|
BenchmarkFunctionFormatter::returnToString($this->lastReturn), |
109
|
|
|
PHP_EOL |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
return ''; |
113
|
|
|
} |
114
|
3 |
|
|
115
|
|
|
/** |
116
|
3 |
|
* @param int $added |
117
|
|
|
* @param int $benchmarked |
118
|
3 |
|
* @return string |
119
|
3 |
|
*/ |
120
|
3 |
|
private function countersStatistics(int $added, int $benchmarked): string |
121
|
3 |
|
{ |
122
|
|
|
if ($added === $benchmarked) { |
123
|
|
|
return sprintf( |
124
|
|
|
'%s: %s %s', |
125
|
|
|
static::BENCHMARKED, |
126
|
|
|
$benchmarked, |
127
|
|
|
PHP_EOL |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return |
132
|
|
|
sprintf( |
133
|
|
|
'%s: %s %s: %s %s %s', |
134
|
|
|
static::ADDED, |
135
|
|
|
$added, |
136
|
|
|
static::BENCHMARKED, |
137
|
|
|
$benchmarked, |
138
|
|
|
$this->countedExceptions($added, $benchmarked), |
139
|
|
|
PHP_EOL |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param int $added |
145
|
|
|
* @param int $benchmarked |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
private function countedExceptions(int $added, int $benchmarked): string |
149
|
|
|
{ |
150
|
|
|
if (0 !== $exceptions = $added - $benchmarked) { |
151
|
|
|
return |
152
|
|
|
sprintf( |
153
|
|
|
'%s %s', |
154
|
|
|
static::EXCEPTIONS, |
155
|
|
|
$exceptions |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
// @codeCoverageIgnoreStart |
159
|
|
|
return ''; |
160
|
|
|
// @codeCoverageIgnoreEnd |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|