1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Accessories; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\Accessories\MemoryUsage\MemoryUsageReport; |
6
|
|
|
use AlecRabbit\Accessories\MemoryUsage\MemoryUsageReportFormatter; |
7
|
|
|
use AlecRabbit\Reports\Core\AbstractReportable; |
8
|
|
|
|
9
|
|
|
class MemoryUsage extends AbstractReportable |
10
|
|
|
{ |
11
|
8 |
|
public function __construct() |
12
|
|
|
{ |
13
|
8 |
|
parent::__construct(); |
14
|
8 |
|
$this->setBindings( |
15
|
8 |
|
MemoryUsageReport::class, |
16
|
8 |
|
MemoryUsageReportFormatter::class |
17
|
|
|
); |
18
|
8 |
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param bool $real |
22
|
|
|
* @param null|string $unit |
23
|
|
|
* @param int|null $decimals |
24
|
|
|
* @return string |
25
|
|
|
*/ |
26
|
2 |
|
public static function get(bool $real = false, ?string $unit = null, ?int $decimals = null): string |
27
|
|
|
{ |
28
|
|
|
return |
29
|
2 |
|
Pretty::bytes(memory_get_usage($real), $unit, $decimals); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param bool $real |
34
|
|
|
* @param null|string $unit |
35
|
|
|
* @param int|null $decimals |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
2 |
|
public static function getPeak(bool $real = false, ?string $unit = null, ?int $decimals = null): string |
39
|
|
|
{ |
40
|
|
|
return |
41
|
2 |
|
Pretty::bytes(memory_get_peak_usage($real), $unit, $decimals); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return MemoryUsageReport |
46
|
|
|
*/ |
47
|
4 |
|
public static function getReport(): MemoryUsageReport |
48
|
|
|
{ |
49
|
|
|
/** @var MemoryUsageReport $report */ |
50
|
4 |
|
$report = (new static)->report(); |
51
|
4 |
|
return $report; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param MemoryUsageReport $firstReport |
56
|
|
|
* @return MemoryUsageReport |
57
|
|
|
*/ |
58
|
1 |
|
public static function diff(MemoryUsageReport $firstReport): MemoryUsageReport |
59
|
|
|
{ |
60
|
|
|
/** @var MemoryUsageReport $report */ |
61
|
1 |
|
$report = (new static)->report(); |
62
|
1 |
|
return $report->diff($firstReport); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|