|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace AlecRabbit\Tools\Reports\Formatters; |
|
4
|
|
|
|
|
5
|
|
|
use AlecRabbit\Accessories\Pretty; |
|
6
|
|
|
use AlecRabbit\Tools\Reports\Contracts\ReportInterface; |
|
7
|
|
|
use AlecRabbit\Tools\Reports\TimerReport; |
|
8
|
|
|
use Carbon\CarbonInterval; |
|
9
|
|
|
use function AlecRabbit\typeOf; |
|
10
|
|
|
use const AlecRabbit\Traits\Constants\DEFAULT_NAME; |
|
11
|
|
|
|
|
12
|
|
|
class TimerReportFormatter extends ReportFormatter |
|
13
|
|
|
{ |
|
14
|
|
|
protected const MILLISECONDS_THRESHOLD = 10000; |
|
15
|
|
|
|
|
16
|
|
|
/** {@inheritdoc} */ |
|
17
|
|
|
public function process(ReportInterface $report): string |
|
18
|
|
|
{ |
|
19
|
|
|
if ($report instanceof TimerReport) { |
|
20
|
|
|
if (0 === $report->getCount() && DEFAULT_NAME === $report->getName()) { |
|
21
|
|
|
return $this->simple($report); |
|
22
|
|
|
} |
|
23
|
4 |
|
return $this->full($report); |
|
24
|
|
|
} |
|
25
|
4 |
|
throw new \RuntimeException(TimerReport::class . ' expected ' . typeOf($report) . ' given'); |
|
26
|
1 |
|
} |
|
27
|
|
|
|
|
28
|
3 |
|
/** |
|
29
|
|
|
* @param TimerReport $report |
|
30
|
|
|
* @param bool $eol |
|
31
|
|
|
* @return string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function simple(TimerReport $report, bool $eol = true): string |
|
34
|
|
|
{ |
|
35
|
1 |
|
return |
|
36
|
|
|
sprintf( |
|
37
|
|
|
self::ELAPSED . ': %s %s', |
|
38
|
1 |
|
$this->refineElapsed($report->getElapsed()), |
|
39
|
1 |
|
$eol ? PHP_EOL : '' |
|
40
|
1 |
|
); |
|
41
|
1 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param \DateInterval $elapsed |
|
45
|
4 |
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
4 |
|
protected function refineElapsed(\DateInterval $elapsed): string |
|
48
|
|
|
{ |
|
49
|
|
|
return static::formatElapsed($elapsed); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public static function formatElapsed(\DateInterval $elapsed): string |
|
53
|
|
|
{ |
|
54
|
3 |
|
$c = CarbonInterval::instance($elapsed); |
|
55
|
|
|
if ($c->totalMilliseconds < self::MILLISECONDS_THRESHOLD) { |
|
56
|
3 |
|
return |
|
57
|
3 |
|
Pretty::milliseconds($c->totalMilliseconds); |
|
58
|
3 |
|
} |
|
59
|
3 |
|
return (string)$c; |
|
60
|
3 |
|
} |
|
61
|
3 |
|
|
|
62
|
3 |
|
/** |
|
63
|
3 |
|
* @param TimerReport $report |
|
64
|
3 |
|
* @param bool $eol |
|
65
|
3 |
|
* @return string |
|
66
|
3 |
|
*/ |
|
67
|
3 |
|
protected function full(TimerReport $report, bool $eol = true): string |
|
68
|
3 |
|
{ |
|
69
|
3 |
|
$r = $report; |
|
70
|
3 |
|
return sprintf( |
|
71
|
3 |
|
self::TIMER . '%s: ' . |
|
72
|
3 |
|
self::AVERAGE . ': %s, ' . |
|
73
|
3 |
|
self::LAST . ': %s, ' . |
|
74
|
3 |
|
self::MIN . '(%s): %s, ' . |
|
75
|
|
|
self::MAX . '(%s): %s, ' . |
|
76
|
|
|
self::COUNT . ': %s, ' . |
|
77
|
|
|
self::ELAPSED . ': %s%s', |
|
78
|
3 |
|
$this->refineName($r->getName()), |
|
79
|
|
|
$this->refineSeconds($r->getAverageValue()), |
|
80
|
3 |
|
$this->refineSeconds($r->getLastValue()), |
|
81
|
1 |
|
$r->getMinValueIteration(), |
|
82
|
|
|
$this->refineSeconds($r->getMinValue()), |
|
83
|
3 |
|
$r->getMaxValueIteration(), |
|
84
|
|
|
$this->refineSeconds($r->getMaxValue()), |
|
85
|
|
|
$r->getCount(), |
|
86
|
|
|
$this->refineElapsed($r->getElapsed()), |
|
87
|
|
|
$eol ? PHP_EOL : '' |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
protected function refineName(string $name): string |
|
92
|
|
|
{ |
|
93
|
|
|
if (DEFAULT_NAME === $name) { |
|
94
|
|
|
return ''; |
|
95
|
|
|
} |
|
96
|
|
|
return '[' . $name . ']'; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
protected function refineSeconds(?float $seconds): string |
|
100
|
|
|
{ |
|
101
|
|
|
return |
|
102
|
|
|
$seconds ? Pretty::seconds($seconds) : 'NULL'; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|