1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Accessories\MemoryUsage; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\Formatters\Contracts\FormatterInterface; |
6
|
|
|
use AlecRabbit\Reports\Core\AbstractReport; |
7
|
|
|
use AlecRabbit\Reports\Core\AbstractReportable; |
8
|
|
|
|
9
|
|
|
class MemoryUsageReport extends AbstractReport implements MemoryUsageReportInterface |
10
|
|
|
{ |
11
|
|
|
/** @var int */ |
12
|
|
|
protected $usage; |
13
|
|
|
|
14
|
|
|
/** @var int */ |
15
|
|
|
protected $peakUsage; |
16
|
|
|
|
17
|
|
|
/** @var int */ |
18
|
|
|
protected $usageReal; |
19
|
|
|
|
20
|
|
|
/** @var int */ |
21
|
|
|
protected $peakUsageReal; |
22
|
|
|
|
23
|
|
|
/** @var null|MemoryUsageReportFormatter */ |
24
|
|
|
protected $formatter; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* MemoryUsageReport constructor. |
28
|
|
|
* @param int $usage |
29
|
|
|
* @param int $peakUsage |
30
|
|
|
* @param int $usageReal |
31
|
|
|
* @param int $peakUsageReal |
32
|
|
|
* @param FormatterInterface|null $formatter |
33
|
|
|
* @param AbstractReportable|null $reportable |
34
|
|
|
*/ |
35
|
7 |
|
public function __construct( |
36
|
|
|
int $usage = null, |
37
|
|
|
int $peakUsage = null, |
38
|
|
|
int $usageReal = null, |
39
|
|
|
int $peakUsageReal = null, |
40
|
|
|
FormatterInterface $formatter = null, |
41
|
|
|
AbstractReportable $reportable = null |
42
|
|
|
) { |
43
|
7 |
|
parent::__construct($formatter, $reportable); |
44
|
7 |
|
$this->usage = $usage ?? memory_get_usage(); |
45
|
7 |
|
$this->peakUsage = $peakUsage ?? memory_get_peak_usage(); |
46
|
7 |
|
$this->usageReal = $usageReal ?? memory_get_usage(true); |
47
|
7 |
|
$this->peakUsageReal = $peakUsageReal ?? memory_get_peak_usage(true); |
48
|
7 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
6 |
|
public function getUsage(): int |
54
|
|
|
{ |
55
|
6 |
|
return $this->usage; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
6 |
|
public function getPeakUsage(): int |
62
|
|
|
{ |
63
|
6 |
|
return $this->peakUsage; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
6 |
|
public function getUsageReal(): int |
70
|
|
|
{ |
71
|
6 |
|
return $this->usageReal; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
6 |
|
public function getPeakUsageReal(): int |
78
|
|
|
{ |
79
|
6 |
|
return $this->peakUsageReal; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
2 |
|
public function getUsageString(?string $unit = null, ?int $decimals = null): string |
86
|
|
|
{ |
87
|
2 |
|
if ($this->formatter instanceof MemoryUsageReportFormatter) { |
88
|
|
|
return |
89
|
2 |
|
$this->formatter->getUsageString($this, $unit, $decimals); |
90
|
|
|
} |
91
|
|
|
// @codeCoverageIgnoreStart |
92
|
|
|
return ''; |
93
|
|
|
// @codeCoverageIgnoreEnd |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
2 |
|
public function getPeakUsageString(?string $unit = null, ?int $decimals = null): string |
100
|
|
|
{ |
101
|
2 |
|
if ($this->formatter instanceof MemoryUsageReportFormatter) { |
102
|
|
|
return |
103
|
2 |
|
$this->formatter->getPeakUsageString($this, $unit, $decimals); |
104
|
|
|
} |
105
|
|
|
// @codeCoverageIgnoreStart |
106
|
|
|
return ''; |
107
|
|
|
// @codeCoverageIgnoreEnd |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
2 |
|
public function getUsageRealString(?string $unit = null, ?int $decimals = null): string |
114
|
|
|
{ |
115
|
2 |
|
if ($this->formatter instanceof MemoryUsageReportFormatter) { |
116
|
|
|
return |
117
|
2 |
|
$this->formatter->getUsageRealString($this, $unit, $decimals); |
118
|
|
|
} |
119
|
|
|
// @codeCoverageIgnoreStart |
120
|
|
|
return ''; |
121
|
|
|
// @codeCoverageIgnoreEnd |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
2 |
|
public function getPeakUsageRealString(?string $unit = null, ?int $decimals = null): string |
128
|
|
|
{ |
129
|
2 |
|
if ($this->formatter instanceof MemoryUsageReportFormatter) { |
130
|
|
|
return |
131
|
2 |
|
$this->formatter->getPeakUsageRealString($this, $unit, $decimals); |
132
|
|
|
} |
133
|
|
|
// @codeCoverageIgnoreStart |
134
|
|
|
return ''; |
135
|
|
|
// @codeCoverageIgnoreEnd |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|