Skip to content

Commit

Permalink
Update OSHelper:
Browse files Browse the repository at this point in the history
- add getCpu()
  • Loading branch information
webeweb committed May 17, 2022
1 parent 73c8273 commit a5143e2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/core/Helper/OSHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace WBW\Library\Core\Helper;

use WBW\Library\Core\Model\Cpu;
use WBW\Library\Core\Model\Memory;

/**
Expand All @@ -21,6 +22,35 @@
*/
class OSHelper {

/**
* Get the CPU.
*
* @return Cpu|null Returns the CPU.
*/
public static function getCpu(): ?Cpu {

if (true === static::isWindows()) {
return null;
}

$grep = "%Cpu(s):";
exec("top -b -n 1 |grep '$grep'", $output);

preg_match_all("/[0-9.]+/", $output[0], $values);

$cpu = new Cpu();
$cpu->setUs(floatval(trim($values[0][0])));
$cpu->setSy(floatval(trim($values[0][1])));
$cpu->setNi(floatval(trim($values[0][2])));
$cpu->setId(floatval(trim($values[0][3])));
$cpu->setWa(floatval(trim($values[0][4])));
$cpu->setHi(floatval(trim($values[0][5])));
$cpu->setSi(floatval(trim($values[0][6])));
$cpu->setSt(floatval(trim($values[0][7])));

return $cpu;
}

/**
* Get the memory.
*
Expand Down
19 changes: 19 additions & 0 deletions tests/core/Helper/OSHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@
*/
class OSHelperTest extends AbstractTestCase {

/**
* Tests getCpu()
*
* @return void
*/
public function testGetCpu(): void {

$obj = OSHelper::getCpu();

$this->assertGreaterThanOrEqual(0, $obj->getUs());
$this->assertGreaterThanOrEqual(0, $obj->getSy());
$this->assertGreaterThanOrEqual(0, $obj->getNi());
$this->assertGreaterThanOrEqual(0, $obj->getId());
$this->assertGreaterThanOrEqual(0, $obj->getWa());
$this->assertGreaterThanOrEqual(0, $obj->getHi());
$this->assertGreaterThanOrEqual(0, $obj->getSi());
$this->assertGreaterThanOrEqual(0, $obj->getST());
}

/**
* Tests getMemory()
*
Expand Down

0 comments on commit a5143e2

Please sign in to comment.