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