|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* User: alec |
|
4
|
|
|
* Date: 10.12.18 |
|
5
|
|
|
* Time: 14:22 |
|
6
|
|
|
*/ |
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace AlecRabbit\Tools\Reports\Formatters; |
|
10
|
|
|
|
|
11
|
|
|
use AlecRabbit\Accessories\Pretty; |
|
12
|
|
|
use AlecRabbit\Tools\Reports\TimerReport; |
|
13
|
|
|
use const AlecRabbit\Traits\Constants\DEFAULT_NAME; |
|
14
|
|
|
|
|
15
|
|
|
class TimerReportFormatter extends ReportFormatter |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var TimerReport */ |
|
18
|
|
|
protected $report; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @return string |
|
22
|
|
|
*/ |
|
23
|
4 |
|
public function process(): string |
|
24
|
|
|
{ |
|
25
|
4 |
|
if (0 === $this->report->getCount() && DEFAULT_NAME === $this->report->getName()) { |
|
26
|
1 |
|
return $this->simple(); |
|
27
|
|
|
} |
|
28
|
3 |
|
return $this->full(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param bool $eol |
|
33
|
|
|
* @return string |
|
34
|
|
|
*/ |
|
35
|
1 |
|
public function simple(bool $eol = true): string |
|
36
|
|
|
{ |
|
37
|
|
|
return |
|
38
|
1 |
|
sprintf( |
|
39
|
1 |
|
self::ELAPSED . ': %s %s', |
|
40
|
1 |
|
$this->ftime($this->report->getElapsed()), |
|
41
|
1 |
|
$eol ? PHP_EOL : '' |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
4 |
|
protected function ftime(float $seconds): string |
|
46
|
|
|
{ |
|
47
|
4 |
|
return Pretty::seconds($seconds); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param bool $eol |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
3 |
|
public function full(bool $eol = true): string |
|
55
|
|
|
{ |
|
56
|
3 |
|
$r = $this->report; |
|
57
|
3 |
|
return sprintf( |
|
58
|
3 |
|
self::TIMER . '%s: ' . |
|
59
|
3 |
|
self::AVERAGE . ': %s, ' . |
|
60
|
3 |
|
self::LAST . ': %s, ' . |
|
61
|
3 |
|
self::MIN . '(%s): %s, ' . |
|
62
|
3 |
|
self::MAX . '(%s): %s, ' . |
|
63
|
3 |
|
self::COUNT . ': %s, ' . |
|
64
|
3 |
|
self::ELAPSED . ': %s%s', |
|
65
|
3 |
|
$this->fname($r->getName()), |
|
66
|
3 |
|
$this->ftime($r->getAverageValue()), |
|
67
|
3 |
|
$this->ftime($r->getLastValue()), |
|
68
|
3 |
|
$r->getMinValueIteration(), |
|
69
|
3 |
|
$this->ftime($r->getMinValue()), |
|
70
|
3 |
|
$r->getMaxValueIteration(), |
|
71
|
3 |
|
$this->ftime($r->getMaxValue()), |
|
72
|
3 |
|
$r->getCount(), |
|
73
|
3 |
|
$this->ftime($r->getElapsed()), |
|
74
|
3 |
|
$eol ? PHP_EOL : '' |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
3 |
|
protected function fname(string $name): string |
|
79
|
|
|
{ |
|
80
|
3 |
|
if (DEFAULT_NAME === $name) { |
|
81
|
1 |
|
return ''; |
|
82
|
|
|
} |
|
83
|
3 |
|
return '[' . $name . ']'; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|