|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AlecRabbit\Tools\Reports\Formatters; |
|
6
|
|
|
|
|
7
|
|
|
use AlecRabbit\Accessories\Pretty; |
|
8
|
|
|
use AlecRabbit\Tools\Contracts\StringConstants; |
|
9
|
|
|
use AlecRabbit\Tools\Internal\BenchmarkFunction; |
|
10
|
|
|
use AlecRabbit\Tools\Reports\Formatters\Contracts\Formatter; |
|
11
|
|
|
use function AlecRabbit\typeOf; |
|
12
|
|
|
|
|
13
|
|
|
class BenchmarkFunctionFormatter implements Formatter, StringConstants |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var BenchmarkFunction */ |
|
16
|
|
|
protected $function; |
|
17
|
|
|
|
|
18
|
|
|
/** @var bool */ |
|
19
|
|
|
protected $withResults = true; |
|
20
|
|
|
|
|
21
|
4 |
|
public function __construct(BenchmarkFunction $function) |
|
22
|
|
|
{ |
|
23
|
4 |
|
$this->function = $function; |
|
24
|
4 |
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param bool $equalReturns |
|
28
|
|
|
* @return BenchmarkFunctionFormatter |
|
29
|
|
|
*/ |
|
30
|
4 |
|
public function noResultsIf(bool $equalReturns = false): BenchmarkFunctionFormatter |
|
31
|
|
|
{ |
|
32
|
4 |
|
$this->withResults = !$equalReturns; |
|
33
|
4 |
|
return $this; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
4 |
|
public function getString(): string |
|
40
|
|
|
{ |
|
41
|
|
|
return |
|
42
|
4 |
|
$this->formatBenchmarkRelative() . |
|
43
|
4 |
|
(empty($exception = $this->formatException()) ? |
|
44
|
4 |
|
PHP_EOL : |
|
45
|
4 |
|
static::EXCEPTIONS . PHP_EOL . $exception); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
4 |
|
protected function formatBenchmarkRelative(): string |
|
52
|
|
|
{ |
|
53
|
4 |
|
$function = $this->function; |
|
54
|
4 |
|
if ($br = $function->getBenchmarkRelative()) { |
|
55
|
4 |
|
$argumentsTypes = $this->extractArgumentsTypes($function->getArgs()); |
|
56
|
4 |
|
$executionReturn = $function->getReturn(); |
|
57
|
|
|
|
|
58
|
4 |
|
if ($this->withResults) { |
|
59
|
|
|
return |
|
60
|
3 |
|
sprintf( |
|
61
|
3 |
|
'%s. %s (%s) %s(%s) %s %s %s %s', |
|
62
|
3 |
|
(string)$br->getRank(), |
|
63
|
3 |
|
$this->average($br->getAverage()), |
|
64
|
3 |
|
$this->relativePercent($br->getRelative()), |
|
65
|
3 |
|
$function->humanReadableName(), |
|
66
|
3 |
|
implode(', ', $argumentsTypes), |
|
67
|
3 |
|
$function->comment(), |
|
68
|
3 |
|
PHP_EOL, |
|
69
|
3 |
|
static::returnToString($executionReturn), |
|
70
|
3 |
|
PHP_EOL |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
return |
|
74
|
1 |
|
sprintf( |
|
75
|
1 |
|
'%s. %s (%s) %s(%s) %s', |
|
76
|
1 |
|
(string)$br->getRank(), |
|
77
|
1 |
|
$this->average($br->getAverage()), |
|
78
|
1 |
|
$this->relativePercent($br->getRelative()), |
|
79
|
1 |
|
$function->humanReadableName(), |
|
80
|
1 |
|
implode(', ', $argumentsTypes), |
|
81
|
1 |
|
$function->comment() |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
3 |
|
return ''; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param array $arguments |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
4 |
|
protected function extractArgumentsTypes(array $arguments): array |
|
92
|
|
|
{ |
|
93
|
4 |
|
$types = []; |
|
94
|
4 |
|
if (!empty($arguments)) { |
|
95
|
1 |
|
foreach ($arguments as $argument) { |
|
96
|
1 |
|
$types[] = typeOf($argument); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
4 |
|
return $types; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param float $average |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
4 |
|
protected function average(float $average): string |
|
107
|
|
|
{ |
|
108
|
4 |
|
return str_pad( |
|
109
|
4 |
|
Pretty::time($average), |
|
110
|
4 |
|
8, |
|
111
|
4 |
|
' ', |
|
112
|
4 |
|
STR_PAD_LEFT |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param float $relative |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
4 |
|
protected function relativePercent(float $relative): string |
|
121
|
|
|
{ |
|
122
|
4 |
|
return str_pad( |
|
123
|
4 |
|
Pretty::percent($relative), |
|
124
|
4 |
|
7, |
|
125
|
4 |
|
' ', |
|
126
|
4 |
|
STR_PAD_LEFT |
|
127
|
|
|
); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param mixed $executionReturn |
|
132
|
|
|
* @return string |
|
133
|
|
|
*/ |
|
134
|
4 |
|
public static function returnToString($executionReturn): string |
|
135
|
|
|
{ |
|
136
|
4 |
|
$type = typeOf($executionReturn); |
|
137
|
4 |
|
$str = var_export($executionReturn, true); |
|
138
|
|
|
return |
|
139
|
4 |
|
$type === 'array' ? |
|
140
|
1 |
|
$str : |
|
141
|
4 |
|
sprintf( |
|
142
|
4 |
|
'%s(%s)', |
|
143
|
4 |
|
$type, |
|
144
|
4 |
|
$str |
|
145
|
|
|
); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @return string |
|
150
|
|
|
*/ |
|
151
|
4 |
|
protected function formatException(): string |
|
152
|
|
|
{ |
|
153
|
|
|
|
|
154
|
4 |
|
if ($e = $this->function->getException()) { |
|
155
|
3 |
|
$argumentsTypes = $this->extractArgumentsTypes($this->function->getArgs()); |
|
156
|
|
|
|
|
157
|
|
|
return |
|
158
|
3 |
|
sprintf( |
|
159
|
3 |
|
'%s(%s) %s [%s] %s', |
|
160
|
3 |
|
$this->function->humanReadableName(), |
|
161
|
3 |
|
implode(', ', $argumentsTypes), |
|
162
|
3 |
|
$this->function->comment(), |
|
163
|
3 |
|
$e->getMessage(), |
|
164
|
3 |
|
PHP_EOL |
|
165
|
|
|
); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
4 |
|
return ''; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|