From 1db5cc12deaf5f32c41ee0713b79021ad418f8fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= <5175937+theofidry@users.noreply.github.com> Date: Mon, 12 Dec 2022 08:32:08 +0100 Subject: [PATCH] Integrate FidryCpuCoreCounter (#703) Co-authored-by: Filippo Tessarotto --- composer.json | 1 + src/Runners/PHPUnit/Options.php | 34 ++++++--------------------------- 2 files changed, 7 insertions(+), 28 deletions(-) diff --git a/composer.json b/composer.json index 639ba8d6..6d7dcf41 100644 --- a/composer.json +++ b/composer.json @@ -38,6 +38,7 @@ "ext-pcre": "*", "ext-reflection": "*", "ext-simplexml": "*", + "fidry/cpu-core-counter": "^0.4.0", "jean85/pretty-package-versions": "^2.0.5", "phpunit/php-code-coverage": "^9.2.19", "phpunit/php-file-iterator": "^3.0.6", diff --git a/src/Runners/PHPUnit/Options.php b/src/Runners/PHPUnit/Options.php index 5408d029..3d056021 100644 --- a/src/Runners/PHPUnit/Options.php +++ b/src/Runners/PHPUnit/Options.php @@ -4,6 +4,8 @@ namespace ParaTest\Runners\PHPUnit; +use Fidry\CpuCoreCounter\CpuCoreCounter; +use Fidry\CpuCoreCounter\NumberOfCpuCoreNotFound; use InvalidArgumentException; use ParaTest\Util\Str; use PHPUnit\TextUI\DefaultResultPrinter; @@ -22,9 +24,7 @@ use function dirname; use function escapeshellarg; use function explode; -use function fgets; use function file_exists; -use function file_get_contents; use function implode; use function in_array; use function intdiv; @@ -34,10 +34,7 @@ use function is_numeric; use function is_string; use function ksort; -use function pclose; -use function popen; use function preg_match; -use function preg_match_all; use function realpath; use function sprintf; use function strlen; @@ -899,30 +896,11 @@ private static function isAbsolutePath(string $path): bool */ public static function getNumberOfCPUCores(): int { - $cores = 2; - if (is_file('/proc/cpuinfo')) { - // Linux (and potentially Windows with linux sub systems) - $cpuinfo = file_get_contents('/proc/cpuinfo'); - assert($cpuinfo !== false); - preg_match_all('/^processor/m', $cpuinfo, $matches); - $cores = count($matches[0]); - // @codeCoverageIgnoreStart - } elseif (DIRECTORY_SEPARATOR === '\\') { - // Windows - if (($process = @popen('wmic cpu get NumberOfLogicalProcessors', 'rb')) !== false) { - fgets($process); - $cores = (int) fgets($process); - pclose($process); - } - } elseif (($process = @popen('sysctl -n hw.ncpu', 'rb')) !== false) { - // *nix (Linux, BSD and Mac) - $cores = (int) fgets($process); - pclose($process); + try { + return (new CpuCoreCounter())->getCount(); + } catch (NumberOfCpuCoreNotFound $exception) { + return 2; } - - // @codeCoverageIgnoreEnd - - return $cores; } /** @return string[]|null */