Skip to content

Commit

Permalink
Integrate FidryCpuCoreCounter
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry committed Dec 11, 2022
1 parent 7b0595a commit 062af34
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 28 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
34 changes: 6 additions & 28 deletions src/Runners/PHPUnit/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit 062af34

Please sign in to comment.