Conditions | 2 |
Paths | 2 |
Total Lines | 19 |
Code Lines | 12 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 6 |
Changes | 0 |
1 | <?php |
||
16 | public function __invoke(): Cpu |
||
17 | { |
||
18 | $process = new Process('top -bn1 | grep \'%Cpu\''); |
||
19 | $process->run(); |
||
20 | |||
21 | if (!$process->isSuccessful()) { |
||
22 | throw new CpuUsageNotAccessible; |
||
23 | } |
||
24 | |||
25 | $percentages = (new Str($process->getOutput())) |
||
26 | ->trim() |
||
27 | ->capture( |
||
28 | '~^%Cpu\(s\): *(?P<user>\d+\.?\d*) us, *(?P<sys>\d+\.?\d*) sy, *(\d+\.?\d*) ni, *(?P<idle>\d+\.?\d*) id~' |
||
29 | ); |
||
30 | |||
31 | return new Cpu( |
||
32 | new Percentage((float) (string) $percentages->get('user')), |
||
33 | new Percentage((float) (string) $percentages->get('sys')), |
||
34 | new Percentage((float) (string) $percentages->get('idle')) |
||
35 | ); |
||
38 |