Skip to content

Commit

Permalink
Add a way to get the logical and physical number of cores for Windows (
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Dec 7, 2022
1 parent 4a0413c commit 1fd5dca
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/CpuCoreCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Fidry\CpuCoreCounter\Finder\CpuInfoFinder;
use Fidry\CpuCoreCounter\Finder\HwLogicalFinder;
use Fidry\CpuCoreCounter\Finder\NProcFinder;
use Fidry\CpuCoreCounter\Finder\WindowsWmicFinder;
use Fidry\CpuCoreCounter\Finder\WindowsWmicLogicalFinder;

final class CpuCoreCounter
{
Expand Down Expand Up @@ -97,7 +97,7 @@ public static function getDefaultFinders(): array
{
return [
new NProcFinder(),
new WindowsWmicFinder(),
new WindowsWmicLogicalFinder(),
new HwLogicalFinder(),
new CpuInfoFinder(),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
use function defined;

/**
* Find the number of CPU cores for Windows.
* 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 WindowsWmicFinder extends PopenBasedFinder
final class WindowsWmicLogicalFinder extends PopenBasedFinder
{
/**
* @return positive-int|null
Expand Down
43 changes: 43 additions & 0 deletions src/Finder/WindowsWmicPhysicalFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Fidry\CpuCoreCounter\Finder;

use function defined;

/**
* 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 WindowsWmicPhysicalFinder extends PopenBasedFinder
{
/**
* @return positive-int|null
*/
public function find(): ?int
{
if (!defined('PHP_WINDOWS_VERSION_MAJOR')) {
// Skip if not on Windows. Rely on PHP to detect the platform
// rather than reading the platform name or others.
return null;
}

return parent::find();
}

protected function getCommand(): string
{
return 'wmic cpu get NumberOfProcessors';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
namespace Fidry\CpuCoreCounter\Test\Finder;

use Fidry\CpuCoreCounter\Finder\PopenBasedFinder;
use Fidry\CpuCoreCounter\Finder\WindowsWmicFinder;
use Fidry\CpuCoreCounter\Finder\WindowsWmicLogicalFinder;

/**
* @covers \Fidry\CpuCoreCounter\Finder\WindowsWmicFinder
* @covers \Fidry\CpuCoreCounter\Finder\WindowsWmicLogicalFinder
*
* @internal
*/
final class WindowsWmicTest extends PopenBasedFinderTestCase
final class WindowsWmicLogicalFinderTest extends PopenBasedFinderTestCase
{
protected function getFinder(): PopenBasedFinder
{
return new WindowsWmicFinder();
return new WindowsWmicLogicalFinder();
}
}
30 changes: 30 additions & 0 deletions tests/Finder/WindowsWmicPhysicalFinderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Fidry\CpuCoreCounter\Test\Finder;

use Fidry\CpuCoreCounter\Finder\PopenBasedFinder;
use Fidry\CpuCoreCounter\Finder\WindowsWmicPhysicalFinder;

/**
* @covers \Fidry\CpuCoreCounter\Finder\WindowsWmicPhysicalFinder
*
* @internal
*/
final class WindowsWmicPhysicalFinderTest extends PopenBasedFinderTestCase
{
protected function getFinder(): PopenBasedFinder
{
return new WindowsWmicPhysicalFinder();
}
}

0 comments on commit 1fd5dca

Please sign in to comment.