generated from ergebnis/php-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract each find logic in its own isolated finder. This will make it easier to later: - Add test - Re-order the find logic
- Loading branch information
Showing
5 changed files
with
185 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?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\CpuCounter; | ||
|
||
interface CpuCoreFinder | ||
{ | ||
/** | ||
* @return positive-int|null | ||
*/ | ||
public static function find(): ?int; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?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\CpuCounter; | ||
|
||
use function count; | ||
use function file_get_contents; | ||
use function is_file; | ||
use function preg_match_all; | ||
|
||
/** | ||
* Find the number of CPU cores looking up at the cpuinfo file which is available | ||
* on Linux systems and Windows systems with a Linux sub-system. | ||
* | ||
* @see https://github.com/paratestphp/paratest/blob/c163539818fd96308ca8dc60f46088461e366ed4/src/Runners/PHPUnit/Options.php#L903-L909 | ||
* @see https://unix.stackexchange.com/questions/146051/number-of-processors-in-proc-cpuinfo | ||
*/ | ||
final class CpuInfoFinder implements CpuCoreFinder | ||
{ | ||
private function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* @return positive-int|null | ||
*/ | ||
public static function find(): ?int | ||
{ | ||
// from brianium/paratest | ||
if (is_file('/proc/cpuinfo')) { | ||
// Linux (and potentially Windows with linux sub systems) | ||
$cpuinfo = file_get_contents('/proc/cpuinfo'); | ||
|
||
if (false !== $cpuinfo) { | ||
preg_match_all('/^processor/m', $cpuinfo, $matches); | ||
|
||
return count($matches[0]); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?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\CpuCounter; | ||
|
||
use function fgets; | ||
use function is_resource; | ||
use function pclose; | ||
use function popen; | ||
|
||
/** | ||
* Find the number of CPU cores for Linux, BSD and OSX. | ||
* | ||
* @see https://github.com/paratestphp/paratest/blob/c163539818fd96308ca8dc60f46088461e366ed4/src/Runners/PHPUnit/Options.php#L903-L909 | ||
* @see https://opensource.apple.com/source/xnu/xnu-792.2.4/libkern/libkern/sysctl.h.auto.html | ||
*/ | ||
final class HwFinder implements CpuCoreFinder | ||
{ | ||
private function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* @return positive-int|null | ||
*/ | ||
public static function find(): ?int | ||
{ | ||
$process = popen('sysctl -n hw.ncpu', 'rb'); | ||
|
||
if (is_resource($process)) { | ||
// *nix (Linux, BSD and Mac) | ||
$cores = (int) fgets($process); | ||
pclose($process); | ||
|
||
return $cores; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?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\CpuCounter; | ||
|
||
use function fgets; | ||
use function is_resource; | ||
use function pclose; | ||
use function popen; | ||
|
||
/** | ||
* Find the number of CPU cores for Windows. | ||
* | ||
* @see https://github.com/paratestphp/paratest/blob/c163539818fd96308ca8dc60f46088461e366ed4/src/Runners/PHPUnit/Options.php#L912-L916 | ||
*/ | ||
final class WindowsWmicFinder implements CpuCoreFinder | ||
{ | ||
private function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* @return positive-int|null | ||
*/ | ||
public static function find(): ?int | ||
{ | ||
// Windows | ||
$process = popen('wmic cpu get NumberOfLogicalProcessors', 'rb'); | ||
|
||
if (is_resource($process)) { | ||
fgets($process); | ||
$cores = (int) fgets($process); | ||
pclose($process); | ||
|
||
return $cores; | ||
} | ||
|
||
return null; | ||
} | ||
} |