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.
- Loading branch information
Showing
9 changed files
with
387 additions
and
0 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
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
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,47 @@ | ||
<?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; | ||
|
||
/** | ||
* The number of logical cores. | ||
* | ||
* @see https://stackoverflow.com/a/23378780/5846754 | ||
*/ | ||
final class LscpuLogicalFinder extends ProcOpenBasedFinder | ||
{ | ||
public function getCommand(): string | ||
{ | ||
return 'lscpu -p'; | ||
} | ||
|
||
protected function countCpuCores(string $lscpu): ?int | ||
{ | ||
$lines = explode(PHP_EOL, $lscpu); | ||
|
||
$actualLines = preg_grep('/^[0-9]+\,/', $lines); | ||
|
||
if (false === $actualLines) { | ||
return null; | ||
} | ||
|
||
$count = count($actualLines); | ||
|
||
return 0 === $count ? null : $count; | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return 'LscpuLogicalFinder'; | ||
} | ||
} |
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,61 @@ | ||
<?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; | ||
|
||
/** | ||
* The number of physical processors. | ||
* | ||
* @see https://stackoverflow.com/a/23378780/5846754 | ||
*/ | ||
final class LscpuPhysicalFinder extends ProcOpenBasedFinder | ||
{ | ||
public function toString(): string | ||
{ | ||
return 'LscpuPhysicalFinder'; | ||
} | ||
|
||
public function getCommand(): string | ||
{ | ||
return 'lscpu -p'; | ||
} | ||
|
||
protected function countCpuCores(string $lscpu): ?int | ||
{ | ||
$lines = explode(PHP_EOL, $lscpu); | ||
|
||
$actualLines = preg_grep('/^[0-9]+\,/', $lines); | ||
|
||
if (false === $actualLines) { | ||
return null; | ||
} | ||
|
||
$cores = []; | ||
foreach ($actualLines as $line) { | ||
strtok($line, ','); | ||
$core = strtok(','); | ||
|
||
if (false === $core) { | ||
continue; | ||
} | ||
|
||
$cores[$core] = true; | ||
} | ||
|
||
unset($cores['-']); | ||
|
||
$count = count($cores); | ||
|
||
return 0 === $count ? null : $count; | ||
} | ||
} |
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,125 @@ | ||
<?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\CpuCoreFinder; | ||
use Fidry\CpuCoreCounter\Finder\LscpuLogicalFinder; | ||
use Fidry\CpuCoreCounter\Test\Executor\DummyExecutor; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @covers \Fidry\CpuCoreCounter\Finder\LscpuLogicalFinder | ||
* | ||
* @internal | ||
*/ | ||
final class LscpuLogicalFinderTest extends TestCase | ||
{ | ||
/** | ||
* @var DummyExecutor | ||
*/ | ||
protected $executor; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->executor = new DummyExecutor(); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
unset($this->executor); | ||
} | ||
|
||
/** | ||
* @dataProvider finderProvider | ||
*/ | ||
public function test_it_can_describe_itself(CpuCoreFinder $finder, string $expected): void | ||
{ | ||
$actual = $finder->toString(); | ||
|
||
self::assertSame($expected, $actual); | ||
} | ||
|
||
public static function finderProvider(): iterable | ||
{ | ||
yield [ | ||
new LscpuLogicalFinder(), | ||
FinderShortClassName::get(new LscpuLogicalFinder()) | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider lscpuProvider | ||
*/ | ||
public function test_it_can_count_the_number_of_cpu_cores( | ||
?array $processResult, | ||
?int $expected | ||
): void { | ||
$finder = new LscpuLogicalFinder($this->executor); | ||
$this->executor->setOutput($processResult); | ||
|
||
$actual = $finder->find(); | ||
|
||
self::assertSame($expected, $actual); | ||
} | ||
|
||
public static function lscpuProvider(): iterable | ||
{ | ||
yield 'example with four logical but two physical' => [ | ||
[ | ||
<<<'EOF' | ||
# The following is the parsable format, which can be fed to other | ||
# programs. Each different item in every column has an unique ID | ||
# starting from zero. | ||
# CPU,Core,Socket,Node,,L1d,L1i,L2,L3 | ||
0,0,0,0,,0,0,0,0 | ||
1,1,0,0,,1,1,1,0 | ||
2,0,0,0,,0,0,0,0 | ||
3,1,0,0,,1,1,1,0 | ||
|
||
EOF | ||
, | ||
'' | ||
], | ||
4 | ||
]; | ||
|
||
yield 'example with two cores' => [ | ||
[ | ||
<<<'EOF' | ||
# The following is the parsable format, which can be fed to other | ||
# programs. Each different item in every column has an unique ID | ||
# starting from zero. | ||
# CPU,Core,Socket,Node,,L1d,L1i,L2 | ||
0,0,0,0,,0,0,0 | ||
1,1,0,0,,1,1,1 | ||
|
||
EOF | ||
, | ||
'' | ||
], | ||
2 | ||
]; | ||
|
||
yield 'handling lscpu failure' => [ | ||
[ | ||
'', | ||
<<<'EOF' | ||
lscpu: failed to determine number of CPUs: /sys/devices/system/cpu/possible: No such file or directory | ||
|
||
EOF | ||
], | ||
null | ||
]; | ||
} | ||
} |
Oops, something went wrong.