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