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