Server::getCpuUsage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4286
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Innmind\ProvisionerBundle\Server;
4
5
use Symfony\Component\Process\Process;
6
7
/**
8
 * Helper to retrieve information about the server usage (cpu and load average)
9
 */
10
class Server implements ServerInterface
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15 3
    public function getCpuUsage()
16
    {
17 3
        $usage = 0;
18
19 3
        foreach ($this->getProcesses() as $process) {
20 3
            $usage += $process['usage'];
21 3
        }
22
23 3
        return $usage;
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 12
    public function getProcesses()
30
    {
31 12
        $processes = [];
32 12
        $process = new Process('ps -eo pcpu,command');
33 12
        $process->run();
34 12
        $output = $process->getOutput();
35 12
        $output = explode("\n", $output);
36
37 12
        foreach ($output as $line) {
38 12
            $line = trim($line);
39
40 12
            if (empty($line)) {
41 12
                continue;
42
            }
43
44 12
            list($cpu, $process) = explode(' ', $line, 2);
45
46 12
            if (is_numeric($cpu)) {
47 12
                $processes[] = [
48 12
                    'usage' => (float) $cpu,
49
                    'name' => $process
50 12
                ];
51 12
            }
52 12
        }
53
54 12
        return $processes;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 3 View Code Duplication
    public function getProcessUsage($processName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
61
    {
62 3
        $usage = 0;
63
64 3
        foreach ($this->getProcesses() as $process) {
65 3
            if (strpos($process['name'], $processName) !== false) {
66 3
                $usage += $process['usage'];
67 3
            }
68 3
        }
69
70 3
        return $usage;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 3 View Code Duplication
    public function getProcessCount($command)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
77
    {
78 3
        $count = 0;
79
80 3
        foreach ($this->getProcesses() as $process) {
81 3
            if (strpos($process['name'], $command) !== false) {
82 3
                $count++;
83 3
            }
84 3
        }
85
86 3
        return $count;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 6
    public function getLoadAverage()
93
    {
94 6
        return sys_getloadavg();
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 3
    public function getCurrentLoadAverage()
101
    {
102 3
        return $this->getLoadAverage()[0];
103
    }
104
}
105