Skip to content

Commit

Permalink
Consolidate CPUInfo finder (#16)
Browse files Browse the repository at this point in the history
Split the "get the string which contains the info" and the "retrieve the info from the string" in order to add unit tests to the 2nd one.
  • Loading branch information
theofidry authored Dec 4, 2022
1 parent bb5036a commit fc210d1
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 11 deletions.
40 changes: 29 additions & 11 deletions src/CpuInfoFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
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
Expand All @@ -25,8 +24,10 @@
* @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
final class CpuInfoFinder
{
private const CPU_INFO_PATH = '/proc/cpuinfo';

private function __construct()
{
}
Expand All @@ -36,18 +37,35 @@ private function __construct()
*/
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');
$cpuInfo = self::getCpuInfo();

if (false !== $cpuinfo) {
preg_match_all('/^processor/m', $cpuinfo, $matches);
return null === $cpuInfo ? null : self::countCpuCores($cpuInfo);
}

return count($matches[0]);
}
private static function getCpuInfo(): ?string
{
if (!is_file(self::CPU_INFO_PATH)) {
return null;
}

return null;
$cpuInfo = file_get_contents(self::CPU_INFO_PATH);

return false === $cpuInfo
? null
: $cpuInfo;
}

/**
* @internal
*
* @return positive-int|null
*/
public static function countCpuCores(string $cpuInfo): ?int
{
preg_match_all('/^processor/m', $cpuInfo, $matches);

$processorCount = count($matches[0]);

return $processorCount > 0 ? $processorCount : null;
}
}
81 changes: 81 additions & 0 deletions tests/CpuInfoFinderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?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\Test;

use Fidry\CpuCounter\CpuInfoFinder;
use PHPUnit\Framework\TestCase;

/**
* @covers \Fidry\CpuCounter\CpuInfoFinder
*
* @internal
*/
final class CpuInfoFinderTest extends TestCase
{
/**
* @dataProvider cpuInfoProvider
*/
public function test_it_can_count_the_number_of_cpu_cores(
string $cpuInfo,
?int $expected
): void {
$actual = CpuInfoFinder::countCpuCores($cpuInfo);

self::assertSame($expected, $actual);
}

public static function cpuInfoProvider(): iterable
{
yield 'empty' => [
<<<'EOF'

EOF,
null,
];

// $ docker run --tty --rm --platform linux/amd64 alpine:3.14 cat /proc/cpuinfo
yield 'example from an alpine Docker image' => [
<<<'EOF'
processor : 0
BogoMIPS : 48.00
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 asimddp sha512 asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp flagm2 frint
CPU implementer : 0x00
CPU architecture: 8
CPU variant : 0x0
CPU part : 0x000
CPU revision : 0
processor : 1
BogoMIPS : 48.00
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 asimddp sha512 asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp flagm2 frint
CPU implementer : 0x00
CPU architecture: 8
CPU variant : 0x0
CPU part : 0x000
CPU revision : 0
processor : 2
BogoMIPS : 48.00
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 asimddp sha512 asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp flagm2 frint
CPU implementer : 0x00
CPU architecture: 8
CPU variant : 0x0
CPU part : 0x000
CPU revision : 0

EOF,
3,
];
}
}

0 comments on commit fc210d1

Please sign in to comment.