1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AlecRabbit\Tools\Reports\Formatters; |
6
|
|
|
|
7
|
|
|
use AlecRabbit\Tools\Internal\BenchmarkRelative; |
8
|
|
|
use AlecRabbit\Tools\Reports\BenchmarkReport; |
9
|
|
|
use function AlecRabbit\brackets; |
10
|
|
|
use function AlecRabbit\format_time_auto; |
11
|
|
|
use function AlecRabbit\typeOf; |
12
|
|
|
|
13
|
|
|
class BenchmarkReportFormatter extends Formatter |
14
|
|
|
{ |
15
|
|
|
/** @var BenchmarkReport */ |
16
|
|
|
protected $report; |
17
|
|
|
|
18
|
4 |
|
public function setStyles(): void |
19
|
|
|
{ |
20
|
4 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
* @throws \Throwable |
25
|
|
|
*/ |
26
|
3 |
|
public function getString(): string |
27
|
|
|
{ |
28
|
3 |
|
$rank = 0; |
29
|
3 |
|
$profilerReport = (string)$this->report->getProfiler()->getReport(); |
30
|
3 |
|
$r = 'Benchmark:' . PHP_EOL; |
31
|
|
|
/** @var BenchmarkRelative $result */ |
32
|
3 |
|
foreach ($this->report->getRelatives() as $indexName => $result) { |
33
|
2 |
|
$relative = $result->getRelative(); |
34
|
2 |
|
$average = $result->getAverage(); |
35
|
2 |
|
$function = $this->report->getFunctionObject($indexName); |
36
|
2 |
|
$function->setRank(++$rank); |
37
|
2 |
|
$arguments = $function->getArgs(); |
38
|
2 |
|
$types = []; |
39
|
2 |
|
if (!empty($arguments)) { |
40
|
1 |
|
foreach ($arguments as $argument) { |
41
|
1 |
|
$types[] = typeOf($argument); |
42
|
|
|
} |
43
|
|
|
} |
44
|
2 |
|
$r .= sprintf( |
45
|
2 |
|
'%s. %s (%s) %s(%s) %s %s', |
46
|
2 |
|
$this->themed->dark((string)$rank), |
47
|
2 |
|
$this->themed->yellow( |
48
|
2 |
|
str_pad( |
49
|
2 |
|
format_time_auto($average), |
50
|
2 |
|
8, |
51
|
2 |
|
' ', |
52
|
2 |
|
STR_PAD_LEFT |
53
|
|
|
) |
54
|
|
|
), |
55
|
2 |
|
$this->colorize( |
56
|
2 |
|
str_pad( |
57
|
2 |
|
Helper::percent($relative), |
58
|
2 |
|
7, |
59
|
2 |
|
' ', |
60
|
2 |
|
STR_PAD_LEFT |
61
|
|
|
), |
62
|
2 |
|
$relative |
63
|
|
|
), |
64
|
2 |
|
$function->getHumanReadableName(), |
65
|
2 |
|
implode(', ', $types), |
66
|
2 |
|
$this->themed->comment($function->getComment()), |
67
|
2 |
|
PHP_EOL |
68
|
|
|
); |
69
|
2 |
|
if ($this->report->isWithResults()) { |
70
|
1 |
|
$result = $function->getResult(); |
71
|
1 |
|
$r .= $this->themed->dark('return: ' . str_replace('double', 'float', typeOf($result)) . ' "' |
72
|
2 |
|
. var_export($function->getResult(), true) . '" ') . PHP_EOL; |
73
|
|
|
} |
74
|
|
|
} |
75
|
3 |
|
if (!empty($exceptionMessages = $this->report->getExceptionMessages())) { |
76
|
1 |
|
$r .= 'Exceptions:' . PHP_EOL; |
77
|
1 |
|
foreach ($exceptionMessages as $name => $exceptionMessage) { |
78
|
1 |
|
$r .= brackets($name) . ': ' . $this->themed->red($exceptionMessage) . PHP_EOL; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
return |
82
|
3 |
|
$r . PHP_EOL . $profilerReport; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $str |
87
|
|
|
* @param float $relative |
88
|
|
|
* @return string |
89
|
|
|
* @throws \Throwable |
90
|
|
|
*/ |
91
|
2 |
|
private function colorize(string $str, float $relative): string |
92
|
|
|
{ |
93
|
2 |
|
if ($relative > 1) { |
94
|
1 |
|
return $this->themed->red($str); |
95
|
|
|
} |
96
|
2 |
|
if ($relative >= 0.03) { |
97
|
1 |
|
return $this->themed->yellow($str); |
98
|
|
|
} |
99
|
|
|
return |
100
|
2 |
|
$this->themed->green($str); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|