Conditions | 3 |
Paths | 3 |
Total Lines | 32 |
Code Lines | 21 |
Lines | 0 |
Ratio | 0 % |
Tests | 5 |
CRAP Score | 7.1521 |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
17 | 3 | public function __invoke(): Cpu |
|
18 | { |
||
19 | 3 | $process = Process::fromShellCommandline('top -l 1 -s 0 | grep \'CPU usage\''); |
|
20 | 3 | $process->run(); |
|
21 | |||
22 | 3 | if (!$process->isSuccessful()) { |
|
23 | 3 | throw new CpuUsageNotAccessible; |
|
24 | } |
||
25 | |||
26 | $percentages = Str::of($process->getOutput()) |
||
27 | ->trim() |
||
28 | ->capture( |
||
29 | '~^CPU usage: (?P<user>\d+\.?\d*)% user, (?P<sys>\d+\.?\d*)% sys, (?P<idle>\d+\.?\d*)% idle$~' |
||
30 | ); |
||
31 | |||
32 | $process = Process::fromShellCommandline('sysctl -a | grep \'hw.ncpu:\''); |
||
33 | $process->run(); |
||
34 | |||
35 | if ($process->isSuccessful()) { |
||
36 | $match = Str::of($process->getOutput()) |
||
37 | ->trim() |
||
38 | ->capture( |
||
39 | '~^hw.ncpu: (?P<cores>\d+)$~' |
||
40 | ); |
||
41 | $cores = $match->get('cores')->toString(); |
||
42 | } |
||
43 | |||
44 | return new Cpu( |
||
45 | new Percentage((float) $percentages->get('user')->toString()), |
||
46 | new Percentage((float) $percentages->get('sys')->toString()), |
||
47 | new Percentage((float) $percentages->get('idle')->toString()), |
||
48 | new Cores((int) ($cores ?? 1)), |
||
49 | ); |
||
52 |