| @@ 14-37 (lines=24) @@ | ||
| 11 | use Innmind\Immutable\Str; |
|
| 12 | use Symfony\Component\Process\Process; |
|
| 13 | ||
| 14 | final class LinuxFacade |
|
| 15 | { |
|
| 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 | ); |
|
| 36 | } |
|
| 37 | } |
|
| 38 | ||
| @@ 14-37 (lines=24) @@ | ||
| 11 | use Innmind\Immutable\Str; |
|
| 12 | use Symfony\Component\Process\Process; |
|
| 13 | ||
| 14 | final class OSXFacade |
|
| 15 | { |
|
| 16 | public function __invoke(): Cpu |
|
| 17 | { |
|
| 18 | $process = new Process('top -l 1 -s 0 | grep \'CPU usage\''); |
|
| 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 usage: (?P<user>\d+\.?\d*)% user, (?P<sys>\d+\.?\d*)% sys, (?P<idle>\d+\.?\d*)% idle$~' |
|
| 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 | ); |
|
| 36 | } |
|
| 37 | } |
|
| 38 | ||