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\Internal\BenchmarkFunction; |
9
|
|
|
use AlecRabbit\Tools\Reports\Formatters\Contracts\Formatter; |
10
|
|
|
use function AlecRabbit\typeOf; |
11
|
|
|
|
12
|
|
|
class BenchmarkFunctionFormatter implements Formatter |
13
|
|
|
{ |
14
|
|
|
/** @var BenchmarkFunction */ |
15
|
|
|
protected $function; |
16
|
|
|
|
17
|
4 |
|
public function __construct(BenchmarkFunction $function) |
18
|
|
|
{ |
19
|
4 |
|
$this->function = $function; |
20
|
4 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
4 |
|
public function getString(): string |
26
|
|
|
{ |
27
|
|
|
return |
28
|
4 |
|
$this->formatBenchmarkRelative() . |
29
|
4 |
|
(empty($exception = $this->formatException()) ? |
30
|
4 |
|
PHP_EOL : |
31
|
4 |
|
'Exceptions:' . PHP_EOL . $exception); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
4 |
|
protected function formatBenchmarkRelative(): string |
38
|
|
|
{ |
39
|
4 |
|
$function = $this->function; |
40
|
4 |
|
if ($br = $function->getBenchmarkRelative()) { |
41
|
4 |
|
$argumentsTypes = $this->extractArgumentsTypes($function->getArgs()); |
42
|
4 |
|
$executionReturn = $function->getReturn(); |
43
|
|
|
|
44
|
|
|
return |
45
|
4 |
|
sprintf( |
46
|
4 |
|
'%s. %s (%s) %s(%s) %s %s %s(%s) %s', |
47
|
4 |
|
(string)$br->getRank(), |
48
|
4 |
|
$this->average($br->getAverage()), |
49
|
4 |
|
$this->relativePercent($br->getRelative()), |
50
|
4 |
|
$function->humanReadableName(), |
51
|
4 |
|
implode(', ', $argumentsTypes), |
52
|
4 |
|
$function->comment(), |
53
|
4 |
|
PHP_EOL, |
54
|
4 |
|
typeOf($executionReturn), |
55
|
4 |
|
var_export($executionReturn, true), |
56
|
4 |
|
PHP_EOL |
57
|
|
|
); |
58
|
|
|
} |
59
|
3 |
|
return ''; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param array $arguments |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
4 |
|
protected function extractArgumentsTypes(array $arguments): array |
67
|
|
|
{ |
68
|
4 |
|
$types = []; |
69
|
4 |
|
if (!empty($arguments)) { |
70
|
1 |
|
foreach ($arguments as $argument) { |
71
|
1 |
|
$types[] = typeOf($argument); |
72
|
|
|
} |
73
|
|
|
} |
74
|
4 |
|
return $types; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param float $average |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
4 |
|
protected function average(float $average): string |
82
|
|
|
{ |
83
|
4 |
|
return str_pad( |
84
|
4 |
|
Pretty::time($average), |
85
|
4 |
|
8, |
86
|
4 |
|
' ', |
87
|
4 |
|
STR_PAD_LEFT |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param float $relative |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
4 |
|
protected function relativePercent(float $relative): string |
96
|
|
|
{ |
97
|
4 |
|
return str_pad( |
98
|
4 |
|
Pretty::percent($relative), |
99
|
4 |
|
7, |
100
|
4 |
|
' ', |
101
|
4 |
|
STR_PAD_LEFT |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
4 |
|
protected function formatException(): string |
109
|
|
|
{ |
110
|
|
|
|
111
|
4 |
|
if ($e = $this->function->getException()) { |
112
|
3 |
|
$argumentsTypes = $this->extractArgumentsTypes($this->function->getArgs()); |
113
|
|
|
|
114
|
|
|
return |
115
|
3 |
|
sprintf( |
116
|
3 |
|
'%s(%s) %s [%s] %s', |
117
|
3 |
|
$this->function->humanReadableName(), |
118
|
3 |
|
implode(', ', $argumentsTypes), |
119
|
3 |
|
$this->function->comment(), |
120
|
3 |
|
$e->getMessage(), |
121
|
3 |
|
PHP_EOL |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
4 |
|
return ''; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|