Skip to content

Commit

Permalink
refactor: LscpuFinder tweaks (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Dec 24, 2022
1 parent 3ae77c8 commit b58e5a3
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 102 deletions.
11 changes: 9 additions & 2 deletions infection.json5
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,19 @@
"Fidry\\CpuCoreCounter\\Executor\\ProcOpenExecutor::execute"
]
},
"Continue_": false,
"FunctionCallRemoval": {
"ignore": [
// I can't find a case in practice where this would happen
"Fidry\\CpuCoreCounter\\Executor\\ProcOpenExecutor::execute",
"Fidry\\CpuCoreCounter\\Executor\\ProcOpenExecutor::execute"
]
},
"PublicVisibility": false
"PublicVisibility": false,
"TrueValue": {
"ignore": [
// This is a case where the value does not matter
"Fidry\\CpuCoreCounter\\Finder\\LscpuPhysicalFinder::countCpuCores"
]
}
}
}
2 changes: 1 addition & 1 deletion src/Finder/FinderRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public static function getDefaultLogicalFinders(): array
new HwLogicalFinder(),
new _NProcessorFinder(),
new NProcessorFinder(),
new CpuInfoFinder(),
new LscpuLogicalFinder(),
new CpuInfoFinder(),
];
}

Expand Down
15 changes: 10 additions & 5 deletions src/Finder/LscpuLogicalFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@

namespace Fidry\CpuCoreCounter\Finder;

use function count;
use function explode;
use function is_array;
use function preg_grep;
use const PHP_EOL;

/**
* The number of logical cores.
*
Expand All @@ -25,13 +31,12 @@ public function getCommand(): string
return 'lscpu -p';
}

protected function countCpuCores(string $lscpu): ?int
protected function countCpuCores(string $process): ?int
{
$lines = explode(PHP_EOL, $lscpu);

$actualLines = preg_grep('/^[0-9]+\,/', $lines);
$lines = explode(PHP_EOL, $process);
$actualLines = preg_grep('/^\d+,/', $lines);

if (false === $actualLines) {
if (!is_array($actualLines)) {
return null;
}

Expand Down
17 changes: 11 additions & 6 deletions src/Finder/LscpuPhysicalFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@

namespace Fidry\CpuCoreCounter\Finder;

use function count;
use function explode;
use function is_array;
use function preg_grep;
use function strtok;
use const PHP_EOL;

/**
* The number of physical processors.
*
Expand All @@ -30,13 +37,12 @@ public function getCommand(): string
return 'lscpu -p';
}

protected function countCpuCores(string $lscpu): ?int
protected function countCpuCores(string $process): ?int
{
$lines = explode(PHP_EOL, $lscpu);

$actualLines = preg_grep('/^[0-9]+\,/', $lines);
$lines = explode(PHP_EOL, $process);
$actualLines = preg_grep('/^\d+/', $lines);

if (false === $actualLines) {
if (!is_array($actualLines)) {
return null;
}

Expand All @@ -51,7 +57,6 @@ protected function countCpuCores(string $lscpu): ?int

$cores[$core] = true;
}

unset($cores['-']);

$count = count($cores);
Expand Down
109 changes: 65 additions & 44 deletions tests/Finder/LscpuLogicalFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,68 +13,84 @@

namespace Fidry\CpuCoreCounter\Test\Finder;

use Fidry\CpuCoreCounter\Finder\CpuCoreFinder;
use Fidry\CpuCoreCounter\Executor\ProcessExecutor;
use Fidry\CpuCoreCounter\Finder\LscpuLogicalFinder;
use Fidry\CpuCoreCounter\Test\Executor\DummyExecutor;
use PHPUnit\Framework\TestCase;
use Fidry\CpuCoreCounter\Finder\ProcOpenBasedFinder;

/**
* @covers \Fidry\CpuCoreCounter\Finder\LscpuLogicalFinder
*
* @internal
*/
final class LscpuLogicalFinderTest extends TestCase
final class LscpuLogicalFinderTest extends ProcOpenBasedFinderTestCase
{
/**
* @var DummyExecutor
*/
protected $executor;

protected function setUp(): void
public static function processResultProvider(): iterable
{
$this->executor = new DummyExecutor();
}
yield 'command could not be executed' => [
null,
null,
];

protected function tearDown(): void
{
unset($this->executor);
}
yield 'empty stdout & stderr' => [
['', ''],
null,
];

/**
* @dataProvider finderProvider
*/
public function test_it_can_describe_itself(CpuCoreFinder $finder, string $expected): void
{
$actual = $finder->toString();
yield 'whitespace stdout' => [
[' ', ''],
null,
];

self::assertSame($expected, $actual);
}
yield 'whitespace stderr' => [
['', ' '],
null,
];

public static function finderProvider(): iterable
{
yield [
new LscpuLogicalFinder(),
FinderShortClassName::get(new LscpuLogicalFinder())
yield 'whitespace stdout & stderr' => [
[' ', ' '],
null,
];
}

/**
* @dataProvider lscpuProvider
*/
public function test_it_can_count_the_number_of_cpu_cores(
?array $processResult,
?int $expected
): void {
$finder = new LscpuLogicalFinder($this->executor);
$this->executor->setOutput($processResult);
yield 'linux line return for stdout' => [
["\n", ''],
null,
];

$actual = $finder->find();
yield 'linux line return for stderr' => [
['', "\n"],
null,
];

self::assertSame($expected, $actual);
}
yield 'linux line return for stdout & stderr' => [
["\n", "\n"],
null,
];

yield 'windows line return for stdout' => [
["\r\n", ''],
null,
];

yield 'windows line return for stderr' => [
['', "\r\n"],
null,
];

yield 'windows line return for stdout & stderr' => [
["\r\n", "\r\n"],
null,
];

yield 'no processor' => [
['0', ''],
null,
];

yield 'valid result with stderr' => [
['3', 'something'],
null,
];

public static function lscpuProvider(): iterable
{
yield 'example with four logical but two physical' => [
[
<<<'EOF'
Expand Down Expand Up @@ -122,4 +138,9 @@ public static function lscpuProvider(): iterable
null
];
}

protected function createFinder(ProcessExecutor $executor): ProcOpenBasedFinder
{
return new LscpuLogicalFinder($executor);
}
}
109 changes: 65 additions & 44 deletions tests/Finder/LscpuPhysicalFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,68 +13,84 @@

namespace Fidry\CpuCoreCounter\Test\Finder;

use Fidry\CpuCoreCounter\Finder\CpuCoreFinder;
use Fidry\CpuCoreCounter\Executor\ProcessExecutor;
use Fidry\CpuCoreCounter\Finder\LscpuPhysicalFinder;
use Fidry\CpuCoreCounter\Test\Executor\DummyExecutor;
use PHPUnit\Framework\TestCase;
use Fidry\CpuCoreCounter\Finder\ProcOpenBasedFinder;

/**
* @covers \Fidry\CpuCoreCounter\Finder\LscpuPhysicalFinder
*
* @internal
*/
final class LscpuPhysicalFinderTest extends TestCase
final class LscpuPhysicalFinderTest extends ProcOpenBasedFinderTestCase
{
/**
* @var DummyExecutor
*/
protected $executor;

protected function setUp(): void
public static function processResultProvider(): iterable
{
$this->executor = new DummyExecutor();
}
yield 'command could not be executed' => [
null,
null,
];

protected function tearDown(): void
{
unset($this->executor);
}
yield 'empty stdout & stderr' => [
['', ''],
null,
];

/**
* @dataProvider finderProvider
*/
public function test_it_can_describe_itself(CpuCoreFinder $finder, string $expected): void
{
$actual = $finder->toString();
yield 'whitespace stdout' => [
[' ', ''],
null,
];

self::assertSame($expected, $actual);
}
yield 'whitespace stderr' => [
['', ' '],
null,
];

public static function finderProvider(): iterable
{
yield [
new LscpuPhysicalFinder(),
FinderShortClassName::get(new LscpuPhysicalFinder())
yield 'whitespace stdout & stderr' => [
[' ', ' '],
null,
];
}

/**
* @dataProvider lscpuProvider
*/
public function test_it_can_count_the_number_of_cpu_cores(
?array $processResult,
?int $expected
): void {
$finder = new LscpuPhysicalFinder($this->executor);
$this->executor->setOutput($processResult);
yield 'linux line return for stdout' => [
["\n", ''],
null,
];

$actual = $finder->find();
yield 'linux line return for stderr' => [
['', "\n"],
null,
];

self::assertSame($expected, $actual);
}
yield 'linux line return for stdout & stderr' => [
["\n", "\n"],
null,
];

yield 'windows line return for stdout' => [
["\r\n", ''],
null,
];

yield 'windows line return for stderr' => [
['', "\r\n"],
null,
];

yield 'windows line return for stdout & stderr' => [
["\r\n", "\r\n"],
null,
];

yield 'no processor' => [
['0', ''],
null,
];

yield 'valid result with stderr' => [
['3', 'something'],
null,
];

public static function lscpuProvider(): iterable
{
yield 'example with four logical but two physical' => [
[
<<<'EOF'
Expand Down Expand Up @@ -139,4 +155,9 @@ public static function lscpuProvider(): iterable
null
];
}

protected function createFinder(ProcessExecutor $executor): ProcOpenBasedFinder
{
return new LscpuPhysicalFinder($executor);
}
}

0 comments on commit b58e5a3

Please sign in to comment.