1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* This file is part of PhuninNode. |
6
|
|
|
* |
7
|
|
|
** (c) 2013 - 2016 Cees-Jan Kiewiet |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace WyriHaximus\PhuninNode\Plugins; |
14
|
|
|
|
15
|
|
|
use React\Promise\PromiseInterface; |
16
|
|
|
use WyriHaximus\PhuninNode\Configuration; |
17
|
|
|
use WyriHaximus\PhuninNode\Metric; |
18
|
|
|
use WyriHaximus\PhuninNode\Node; |
19
|
|
|
use WyriHaximus\PhuninNode\PluginInterface; |
20
|
|
|
use function React\Promise\resolve; |
21
|
|
|
use function WyriHaximus\PhuninNode\metricPromisesToObjectStorage; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class MemoryUsage |
25
|
|
|
* @package WyriHaximus\PhuninNode\Plugins |
26
|
|
|
*/ |
27
|
|
|
class MemoryUsage implements PluginInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var Node |
31
|
|
|
*/ |
32
|
|
|
private $node; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Cached configuration state |
36
|
|
|
* |
37
|
|
|
* @var Configuration |
38
|
|
|
*/ |
39
|
|
|
private $configuration; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
8 |
|
public function setNode(Node $node) |
45
|
|
|
{ |
46
|
8 |
|
$this->node = $node; |
47
|
8 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
1 |
|
public function getSlug(): string |
53
|
|
|
{ |
54
|
1 |
|
return 'memory_usage'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
1 |
|
public function getCategorySlug(): string |
61
|
|
|
{ |
62
|
1 |
|
return 'phunin_node'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
2 |
View Code Duplication |
public function getConfiguration(): PromiseInterface |
|
|
|
|
69
|
|
|
{ |
70
|
2 |
|
if ($this->configuration instanceof Configuration) { |
71
|
1 |
|
return resolve($this->configuration); |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
$this->configuration = new Configuration(); |
75
|
2 |
|
$this->configuration->setPair('graph_category', 'phunin_node'); |
76
|
2 |
|
$this->configuration->setPair('graph_title', 'Memory Usage'); |
77
|
2 |
|
$this->configuration->setPair('memory_usage.label', 'Current Memory Usage'); |
78
|
2 |
|
$this->configuration->setPair('memory_peak_usage.label', 'Peak Memory Usage'); |
79
|
2 |
|
$this->configuration->setPair('internal_memory_usage.label', 'Internal Current Memory Usage'); |
80
|
2 |
|
$this->configuration->setPair('internal_memory_peak_usage.label', 'Internal Peak Memory Usage'); |
81
|
|
|
|
82
|
2 |
|
return resolve($this->configuration); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
2 |
|
public function getValues(): PromiseInterface |
89
|
|
|
{ |
90
|
2 |
|
return metricPromisesToObjectStorage([ |
91
|
2 |
|
new Metric('memory_usage', memory_get_usage(true)), |
92
|
2 |
|
new Metric('memory_peak_usage', memory_get_peak_usage(true)), |
93
|
2 |
|
new Metric('internal_memory_usage', memory_get_usage()), |
94
|
2 |
|
new Metric('internal_memory_peak_usage', memory_get_peak_usage()), |
95
|
|
|
]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
1 |
|
public function getCapabilities(): array |
102
|
|
|
{ |
103
|
1 |
|
return []; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.