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.
Introduce a way to diagnose a finder, i.e. get information about what a specific finder may find. Those are then aggregated and but run all by a diagnoser. The idea of this tool is to provide a quick insight on how each finder will operator. For example on my machine: ``` $ make diagnose ./bin/diagnose.php Running diagnosis... CpuInfoFinder: -------------------------------------------- The file "/proc/cpuinfo" could not be found. -------------------------------------------- DummyCpuCoreFinder(value=1): ---------------- Will return "1". ---------------- HwLogicalFinder: --------------------------------------------------------------------------------- Executed the command "sysctl -n hw.logicalcpu 2>&1" and got the following output: 8 --------------------------------------------------------------------------------- HwPhysicalFinder: ---------------------------------------------------------------------------------- Executed the command "sysctl -n hw.physicalcpu 2>&1" and got the following output: 8 ---------------------------------------------------------------------------------- NProcFinder(all=true): ---------------------------------------------------- The command "nproc --all" gave the following output: 8 ---------------------------------------------------- NProcFinder(all=false): ---------------------------------------------- The command "nproc" gave the following output: 8 ---------------------------------------------- NullCpuCoreFinder: ------------------- Will return "null". ------------------- WindowsWmicPhysicalFinder: -------------------------------------------------------------------------------------------------------------------------------------- Executed the command "wmic cpu get NumberOfProcessors 2>&1" which exited with a non-success exit code (127) with the following output: sh: wmic: command not found -------------------------------------------------------------------------------------------------------------------------------------- WindowsWmicLogicalFinder: --------------------------------------------------------------------------------------------------------------------------------------------- Executed the command "wmic cpu get NumberOfLogicalProcessors 2>&1" which exited with a non-success exit code (127) with the following output: sh: wmic: command not found --------------------------------------------------------------------------------------------------------------------------------------------- ``` It also allows to execute each finders: ``` $ make execute ./bin/execute.php Executing finders... CpuInfoFinder: NULL DummyCpuCoreFinder(value=1): 1 HwLogicalFinder: 8 HwPhysicalFinder: 8 NProcFinder(all=true): 8 NProcFinder(all=false): 8 NullCpuCoreFinder: NULL WindowsWmicPhysicalFinder: NULL WindowsWmicLogicalFinder: NULL ``` With this, it should be easy to test it on a specific system.
- Loading branch information
Showing
26 changed files
with
652 additions
and
141 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,21 @@ | ||
#!/usr/bin/env php | ||
<?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); | ||
|
||
use Fidry\CpuCoreCounter\Diagnoser; | ||
use Fidry\CpuCoreCounter\Finder\FinderRegistry; | ||
|
||
require_once __DIR__.'/../vendor/autoload.php'; | ||
|
||
echo 'Running diagnosis...'.PHP_EOL.PHP_EOL; | ||
echo Diagnoser::diagnose(FinderRegistry::getAllVariants()).PHP_EOL; |
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,21 @@ | ||
#!/usr/bin/env php | ||
<?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); | ||
|
||
use Fidry\CpuCoreCounter\Diagnoser; | ||
use Fidry\CpuCoreCounter\Finder\FinderRegistry; | ||
|
||
require_once __DIR__.'/../vendor/autoload.php'; | ||
|
||
echo 'Executing finders...'.PHP_EOL.PHP_EOL; | ||
echo Diagnoser::execute(FinderRegistry::getAllVariants()).PHP_EOL; |
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
CpuInfoFinder: F | ||
DummyCpuCoreFinder: . | ||
DummyCpuCoreFinder(value=1): . | ||
HwLogicalFinder: . | ||
HwPhysicalFinder: . | ||
NProcFinder{all=true}: . | ||
NProcFinder{all=false}: . | ||
NProcFinder(all=true): . | ||
NProcFinder(all=false): . | ||
NullCpuCoreFinder: F | ||
WindowsWmicLogicalFinder: F | ||
WindowsWmicPhysicalFinder: F | ||
WindowsWmicLogicalFinder: F |
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,99 @@ | ||
<?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; | ||
|
||
use Fidry\CpuCoreCounter\Finder\CpuCoreFinder; | ||
use function array_map; | ||
use function explode; | ||
use function implode; | ||
use function max; | ||
use function str_repeat; | ||
use const PHP_EOL; | ||
|
||
/** | ||
* Utility to debug. | ||
* | ||
* @private | ||
*/ | ||
final class Diagnoser | ||
{ | ||
/** | ||
* Provides an aggregated diagnosis based on each finders diagnosis. | ||
* | ||
* @param list<CpuCoreFinder> $finders | ||
*/ | ||
public static function diagnose(array $finders): string | ||
{ | ||
$diagnoses = array_map( | ||
static fn (CpuCoreFinder $finder): string => self::diagnoseFinder($finder), | ||
$finders | ||
); | ||
|
||
return implode(PHP_EOL, $diagnoses); | ||
} | ||
|
||
/** | ||
* Executes each finders. | ||
* | ||
* @param list<CpuCoreFinder> $finders | ||
*/ | ||
public static function execute(array $finders): string | ||
{ | ||
$diagnoses = array_map( | ||
static function (CpuCoreFinder $finder): string { | ||
$coresCount = $finder->find(); | ||
|
||
return implode( | ||
'', | ||
[ | ||
$finder->toString(), | ||
': ', | ||
null === $coresCount ? 'NULL' : $coresCount, | ||
] | ||
); | ||
}, | ||
$finders | ||
); | ||
|
||
return implode(PHP_EOL, $diagnoses); | ||
} | ||
|
||
private static function diagnoseFinder(CpuCoreFinder $finder): string | ||
{ | ||
$diagnosis = $finder->diagnose(); | ||
|
||
$maxLineLength = max( | ||
array_map( | ||
'strlen', | ||
explode(PHP_EOL, $diagnosis) | ||
) | ||
); | ||
|
||
$separator = str_repeat('-', $maxLineLength); | ||
|
||
return implode( | ||
'', | ||
[ | ||
$finder->toString().':'.PHP_EOL, | ||
$separator.PHP_EOL, | ||
$diagnosis.PHP_EOL, | ||
$separator.PHP_EOL, | ||
] | ||
); | ||
} | ||
|
||
private function __construct() | ||
{ | ||
} | ||
} |
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,42 @@ | ||
<?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; | ||
|
||
/** | ||
* @private | ||
*/ | ||
final class FinderRegistry | ||
{ | ||
/** | ||
* @return list<CpuCoreFinder> List of all the known finders with all their variants. | ||
*/ | ||
public static function getAllVariants(): array | ||
{ | ||
return [ | ||
new CpuInfoFinder(), | ||
new DummyCpuCoreFinder(1), | ||
new HwLogicalFinder(), | ||
new HwPhysicalFinder(), | ||
new NProcFinder(true), | ||
new NProcFinder(false), | ||
new NullCpuCoreFinder(), | ||
new WindowsWmicPhysicalFinder(), | ||
new WindowsWmicLogicalFinder(), | ||
]; | ||
} | ||
|
||
private function __construct() | ||
{ | ||
} | ||
} |
Oops, something went wrong.