Skip to content

Commit

Permalink
Make generating names more performant (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Dec 10, 2022
1 parent 6db5e45 commit 31903e8
Show file tree
Hide file tree
Showing 28 changed files with 159 additions and 67 deletions.
5 changes: 1 addition & 4 deletions src/Finder/CpuInfoFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
use function file_get_contents;
use function is_file;
use function sprintf;
use function strrpos;
use function substr;
use function substr_count;
use const PHP_EOL;

Expand Down Expand Up @@ -70,8 +68,7 @@ public function find(): ?int

public function toString(): string
{
/** @phpstan-ignore-next-line */
return substr(__CLASS__, strrpos(__CLASS__, '\\') + 1);
return 'CpuInfoFinder';
}

private static function getCpuInfo(): ?string
Expand Down
6 changes: 1 addition & 5 deletions src/Finder/DummyCpuCoreFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
namespace Fidry\CpuCoreCounter\Finder;

use function sprintf;
use function strrpos;
use function substr;

/**
* This finder returns whatever value you gave to it. This is useful for testing
Expand Down Expand Up @@ -52,9 +50,7 @@ public function find(): ?int
public function toString(): string
{
return sprintf(
'%s(value=%d)',
/** @phpstan-ignore-next-line */
substr(__CLASS__, strrpos(__CLASS__, '\\') + 1),
'DummyCpuCoreFinder(value=%d)',
$this->count
);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Finder/HwLogicalFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ protected function getCommand(): string
{
return 'sysctl -n hw.logicalcpu';
}

public function toString(): string
{
return 'HwLogicalFinder';
}
}
5 changes: 5 additions & 0 deletions src/Finder/HwPhysicalFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ protected function getCommand(): string
{
return 'sysctl -n hw.physicalcpu';
}

public function toString(): string
{
return 'HwPhysicalFinder';
}
}
6 changes: 1 addition & 5 deletions src/Finder/NProcFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
use function function_exists;
use function is_int;
use function sprintf;
use function strrpos;
use function substr;
use function trim;
use const FILTER_VALIDATE_INT;
use const PHP_EOL;
Expand Down Expand Up @@ -117,9 +115,7 @@ public function find(): ?int
public function toString(): string
{
return sprintf(
'%s(all=%s)',
/** @phpstan-ignore-next-line */
substr(__CLASS__, strrpos(__CLASS__, '\\') + 1),
'NProcFinder(all=%s)',
$this->all ? 'true' : 'false'
);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Finder/NProcessorFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ protected function getCommand(): string
{
return 'getconf NPROCESSORS_ONLN';
}

public function toString(): string
{
return 'NProcessorFinder';
}
}
6 changes: 1 addition & 5 deletions src/Finder/NullCpuCoreFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@

namespace Fidry\CpuCoreCounter\Finder;

use function strrpos;
use function substr;

/**
* This finder returns whatever value you gave to it. This is useful for testing.
*/
Expand All @@ -33,7 +30,6 @@ public function find(): ?int

public function toString(): string
{
/** @phpstan-ignore-next-line */
return substr(__CLASS__, strrpos(__CLASS__, '\\') + 1);
return 'NullCpuCoreFinder';
}
}
10 changes: 0 additions & 10 deletions src/Finder/ProcOpenBasedFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
use function function_exists;
use function is_int;
use function sprintf;
use function strrpos;
use function substr;
use function trim;
use const FILTER_VALIDATE_INT;
use const PHP_EOL;
Expand Down Expand Up @@ -83,14 +81,6 @@ public function find(): ?int
: self::countCpuCores($stdout);
}

public function toString(): string
{
$class = static::class;

/** @phpstan-ignore-next-line */
return substr($class, strrpos($class, '\\') + 1);
}

/**
* @internal
*
Expand Down
5 changes: 5 additions & 0 deletions src/Finder/WmicLogicalFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ protected function getCommand(): string
{
return 'wmic cpu get NumberOfLogicalProcessors';
}

public function toString(): string
{
return 'WmicLogicalFinder';
}
}
5 changes: 5 additions & 0 deletions src/Finder/WmicPhysicalFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ protected function getCommand(): string
{
return 'wmic cpu get NumberOfProcessors';
}

public function toString(): string
{
return 'WmicPhysicalFinder';
}
}
5 changes: 5 additions & 0 deletions src/Finder/_NProcessorFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ protected function getCommand(): string
{
return 'getconf _NPROCESSORS_ONLN';
}

public function toString(): string
{
return '_NProcessorFinder';
}
}
5 changes: 4 additions & 1 deletion tests/Finder/CpuInfoFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ protected function tearDown(): void

public function test_it_can_describe_itself(): void
{
self::assertSame('CpuInfoFinder', $this->finder->toString());
self::assertSame(
FinderShortClassName::get($this->finder),
$this->finder->toString()
);
}

/**
Expand Down
9 changes: 8 additions & 1 deletion tests/Finder/DummyCpuCoreFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Fidry\CpuCoreCounter\Finder\DummyCpuCoreFinder;
use PHPUnit\Framework\TestCase;
use function sprintf;

/**
* @covers \Fidry\CpuCoreCounter\Finder\DummyCpuCoreFinder
Expand All @@ -34,6 +35,12 @@ public function test_it_can_describe_itself(): void
{
$finder = new DummyCpuCoreFinder(5);

self::assertSame('DummyCpuCoreFinder(value=5)', $finder->toString());
self::assertSame(
sprintf(
'%s(value=5)',
FinderShortClassName::get($finder)
),
$finder->toString()
);
}
}
5 changes: 5 additions & 0 deletions tests/Finder/DummyProcOpenBasedFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ protected function getCommand(): string
{
return '';
}

public function toString(): string
{
return 'DummyProcOpenBasedFinder';
}
}
33 changes: 33 additions & 0 deletions tests/Finder/FinderShortClassName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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 function get_class;
use function strrpos;
use function substr;

final class FinderShortClassName
{
public static function get(CpuCoreFinder $finder): string
{
$class = get_class($finder);

return substr($class, strrpos($class, '\\') + 1);
}

private function __construct()
{
}
}
34 changes: 34 additions & 0 deletions tests/Finder/FinderShortClassNameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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 PHPUnit\Framework\TestCase;

/**
* @covers \Fidry\CpuCoreCounter\Test\Finder\FinderShortClassName
*
* @internal
*/
final class FinderShortClassNameTest extends TestCase
{
public function test_it_can_find_a_finder_short_name(): void
{
$finder = new FakeFinder();

$expected = 'FakeFinder';
$actual = FinderShortClassName::get($finder);

self::assertSame($expected, $actual);
}
}
5 changes: 0 additions & 5 deletions tests/Finder/HwLogicalFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
*/
final class HwLogicalFinderTest extends ProcOpenBasedFinderTestCase
{
public function test_it_can_describe_itself(): void
{
self::assertSame('HwLogicalFinder', $this->getFinder()->toString());
}

protected function getFinder(): ProcOpenBasedFinder
{
return new HwLogicalFinder();
Expand Down
5 changes: 0 additions & 5 deletions tests/Finder/HwPhysicalFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
*/
final class HwPhysicalFinderTest extends ProcOpenBasedFinderTestCase
{
public function test_it_can_describe_itself(): void
{
self::assertSame('HwPhysicalFinder', $this->getFinder()->toString());
}

protected function getFinder(): ProcOpenBasedFinder
{
return new HwPhysicalFinder();
Expand Down
11 changes: 9 additions & 2 deletions tests/Finder/NProcFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Fidry\CpuCoreCounter\Finder\CpuCoreFinder;
use Fidry\CpuCoreCounter\Finder\NProcFinder;
use PHPUnit\Framework\TestCase;
use function sprintf;

/**
* @covers \Fidry\CpuCoreCounter\Finder\NProcFinder
Expand All @@ -38,12 +39,18 @@ public static function finderProvider(): iterable
{
yield [
new NProcFinder(true),
'NProcFinder(all=true)',
sprintf(
'%s(all=true)',
FinderShortClassName::get(new NProcFinder())
)
];

yield [
new NProcFinder(false),
'NProcFinder(all=false)',
sprintf(
'%s(all=false)',
FinderShortClassName::get(new NProcFinder())
)
];
}

Expand Down
5 changes: 0 additions & 5 deletions tests/Finder/NProcessorFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
*/
final class NProcessorFinderTest extends ProcOpenBasedFinderTestCase
{
public function test_it_can_describe_itself(): void
{
self::assertSame('NProcessorFinder', $this->getFinder()->toString());
}

protected function getFinder(): ProcOpenBasedFinder
{
return new NProcessorFinder();
Expand Down
5 changes: 4 additions & 1 deletion tests/Finder/NullCpuCoreFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public function test_it_can_describe_itself(): void
{
$finder = new NullCpuCoreFinder();

self::assertSame('NullCpuCoreFinder', $finder->toString());
self::assertSame(
FinderShortClassName::get($finder),
$finder->toString()
);
}
}
9 changes: 8 additions & 1 deletion tests/Finder/OnlyOnWindowsFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use PHPUnit\Framework\TestCase;
use function define;
use function defined;
use function sprintf;

/**
* @covers \Fidry\CpuCoreCounter\Finder\OnlyOnWindowsFinder
Expand All @@ -31,7 +32,13 @@ public function test_it_can_describe_itself(): void
{
$finder = new OnlyOnWindowsFinder(new NullCpuCoreFinder());

self::assertSame('OnlyOnWindowsFinder(NullCpuCoreFinder)', $finder->toString());
self::assertSame(
sprintf(
'%s(NullCpuCoreFinder)',
FinderShortClassName::get($finder)
),
$finder->toString()
);
}

public function test_it_enriches_the_decorated_finder_diagnosis(): void
Expand Down
5 changes: 4 additions & 1 deletion tests/Finder/ProcOpenBasedFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ public function test_it_can_describe_itself(): void
{
$finder = new DummyProcOpenBasedFinder();

self::assertSame('DummyProcOpenBasedFinder', $finder->toString());
self::assertSame(
FinderShortClassName::get($finder),
$finder->toString()
);
}
}
8 changes: 8 additions & 0 deletions tests/Finder/ProcOpenBasedFinderTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,13 @@ public static function processResultProvider(): iterable
];
}

public function test_it_can_describe_itself(): void
{
self::assertSame(
FinderShortClassName::get($this->getFinder()),
$this->getFinder()->toString()
);
}

abstract protected function getFinder(): ProcOpenBasedFinder;
}
Loading

0 comments on commit 31903e8

Please sign in to comment.