Skip to content

Commit

Permalink
Fix the Wmic commands (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Dec 10, 2022
1 parent f702e5c commit f97cce1
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
4 changes: 2 additions & 2 deletions e2e/expected-output-windows
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ NProcFinder(all=false): F
NullCpuCoreFinder: F
OnlyOnWindowsFinder(DummyCpuCoreFinder(value=1)): .
SkipOnWindowsFinder(DummyCpuCoreFinder(value=1)): F
WmicPhysicalFinder: F
WmicLogicalFinder: F
WmicPhysicalFinder: .
WmicLogicalFinder: .
15 changes: 15 additions & 0 deletions src/Finder/WmicLogicalFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@

namespace Fidry\CpuCoreCounter\Finder;

use function preg_match;

/**
* Find the number of logical CPU cores for Windows.
*
* @see https://github.com/paratestphp/paratest/blob/c163539818fd96308ca8dc60f46088461e366ed4/src/Runners/PHPUnit/Options.php#L912-L916
*/
final class WmicLogicalFinder extends ProcOpenBasedFinder
{
private const CPU_CORE_COUNT_REGEX = '/NumberOfLogicalProcessors[\s\n]+(?<count>\d+)/';

protected function getCommand(): string
{
return 'wmic cpu get NumberOfLogicalProcessors';
Expand All @@ -29,4 +33,15 @@ public function toString(): string
{
return 'WmicLogicalFinder';
}

public static function countCpuCores(string $process): ?int
{
if (0 === preg_match(self::CPU_CORE_COUNT_REGEX, $process, $matches)) {
return parent::countCpuCores($process);
}

$count = $matches['count'];

return parent::countCpuCores($count);
}
}
17 changes: 16 additions & 1 deletion src/Finder/WmicPhysicalFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,35 @@

namespace Fidry\CpuCoreCounter\Finder;

use function preg_match;

/**
* Find the number of physical CPU cores for Windows.
*
* @see https://github.com/paratestphp/paratest/blob/c163539818fd96308ca8dc60f46088461e366ed4/src/Runners/PHPUnit/Options.php#L912-L916
*/
final class WmicPhysicalFinder extends ProcOpenBasedFinder
{
private const CPU_CORE_COUNT_REGEX = '/NumberOfCores[\s\n]+(?<count>\d+)/';

protected function getCommand(): string
{
return 'wmic cpu get NumberOfProcessors';
return 'wmic cpu get NumberOfCores';
}

public function toString(): string
{
return 'WmicPhysicalFinder';
}

public static function countCpuCores(string $process): ?int
{
if (0 === preg_match(self::CPU_CORE_COUNT_REGEX, $process, $matches)) {
return parent::countCpuCores($process);
}

$count = $matches['count'];

return parent::countCpuCores($count);
}
}
15 changes: 15 additions & 0 deletions tests/Finder/WmicLogicalFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,19 @@ protected function getFinder(): ProcOpenBasedFinder
{
return new WmicLogicalFinder();
}

public static function processResultProvider(): iterable
{
yield from parent::processResultProvider();

yield 'example from the GitHub Actions machine' => [
<<<'EOF'
NumberOfLogicalProcessors
2
EOF
,
2,
];
}
}
15 changes: 15 additions & 0 deletions tests/Finder/WmicPhysicalFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,19 @@ protected function getFinder(): ProcOpenBasedFinder
{
return new WmicPhysicalFinder();
}

public static function processResultProvider(): iterable
{
yield from parent::processResultProvider();

yield 'example from the GitHub Actions machine' => [
<<<'EOF'
NumberOfCores
2
EOF
,
2,
];
}
}

0 comments on commit f97cce1

Please sign in to comment.