Skip to content

Commit

Permalink
fix: Fix the types for the limit (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Aug 5, 2024
1 parent 89bf603 commit ff45221
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 23 deletions.
3 changes: 0 additions & 3 deletions phpstan.src.neon
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,3 @@ parameters:

- path: src/CpuCoreCounter.php
message: '#ParallelisationResult constructor expects int\<1\, max\>, int given\.#'

- path: src/ParallelisationResult.php
message: '#\$correctedLimit \(int\<1\, max\>\|null\) does not accept int\|null#'
56 changes: 36 additions & 20 deletions src/CpuCoreCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,26 @@ public function __construct(?array $finders = null)
}

/**
* @param positive-int|0 $reservedCpus Number of CPUs to reserve. This is useful when you want
* to reserve some CPUs for other processes. If the main
* process is going to be busy still, you may want to set
* this value to 1.
* @param positive-int $limit
* @param float|null $loadLimit Element of [0., 1.]. Percentage representing the
* amount of cores that should be used among the available
* resources. For instance, if set to 0.7, it will use 70%
* of the available cores, i.e. if 1 core is reserved, 11
* cores are available and 5 are busy, it will use 70%
* of (11-1-5)=5 cores, so 3 cores. Set this parameter to null
* to skip this check. Beware that 1 does not mean "no limit",
* but 100% of the _available_ resources, i.e. with the
* previous example, it will return 5 cores. How busy is
* the system is determined by the system load average
* (see $systemLoadAverage).
* @param float|null $systemLoadAverage The system load average. If not provided, it will be
* retrieved using `sys_getloadavg()` to check the load
* of the system in the past minute. Should be a positive
* float.
* @param positive-int|0 $reservedCpus Number of CPUs to reserve. This is useful when you want
* to reserve some CPUs for other processes. If the main
* process is going to be busy still, you may want to set
* this value to 1.
* @param positive-int|null $limit
* @param float|null $loadLimit Element of [0., 1.]. Percentage representing the
* amount of cores that should be used among the available
* resources. For instance, if set to 0.7, it will use 70%
* of the available cores, i.e. if 1 core is reserved, 11
* cores are available and 5 are busy, it will use 70%
* of (11-1-5)=5 cores, so 3 cores. Set this parameter to null
* to skip this check. Beware that 1 does not mean "no limit",
* but 100% of the _available_ resources, i.e. with the
* previous example, it will return 5 cores. How busy is
* the system is determined by the system load average
* (see $systemLoadAverage).
* @param float|null $systemLoadAverage The system load average. If not provided, it will be
* retrieved using `sys_getloadavg()` to check the load
* of the system in the past minute. Should be a positive
* float.
*
* @see https://php.net/manual/en/function.sys-getloadavg.php
*/
Expand All @@ -72,6 +72,7 @@ public function getAvailableForParallelisation(
?float $loadLimit = null,
?float $systemLoadAverage = null
): ParallelisationResult {
self::checkLimit($limit);
self::checkLoadLimit($loadLimit);
self::checkSystemLoadAverage($systemLoadAverage);

Expand Down Expand Up @@ -203,13 +204,28 @@ public function getFinderAndCores(): array
throw NumberOfCpuCoreNotFound::create();
}

/**
* @return positive-int|null
*/
public static function getKubernetesLimit(): ?int
{
$finder = new EnvVariableFinder('KUBERNETES_CPU_LIMIT');

return $finder->find();
}

private static function checkLimit(?int $limit): void
{
if (null !== $limit && 0 >= $limit) {
throw new InvalidArgumentException(
sprintf(
'The limit must be a positive integer. Got "%s".',
$limit
)
);
}
}

private static function checkLoadLimit(?float $loadLimit): void
{
if (null === $loadLimit) {
Expand Down
1 change: 1 addition & 0 deletions src/ParallelisationResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ final class ParallelisationResult
/**
* @param positive-int|0 $passedReservedCpus
* @param positive-int|null $passedLimit
* @param positive-int|null $correctedLimit
* @param positive-int $totalCoresCount
* @param positive-int $availableCpus
*/
Expand Down
46 changes: 46 additions & 0 deletions tests/CpuCoreCounterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,52 @@ public static function availableCpuCoreProvider(): iterable
);
}

/**
* @dataProvider limitProvider
*/
public function test_it_does_not_accept_invalid_limit(
int $countLimit,
?string $expectedExceptionMessage
): void {
$cpuCoreCounter = new CpuCoreCounter();

if (null !== $expectedExceptionMessage) {
$this->expectExceptionMessage($expectedExceptionMessage);
}

$cpuCoreCounter->getAvailableForParallelisation(
1,
$countLimit
);

if (null === $expectedExceptionMessage) {
$this->addToAssertionCount(1);
}
}

public static function limitProvider(): iterable
{
yield 'below limit' => [
-2,
'The limit must be a positive integer. Got "-2".',
];

yield 'invalid limit' => [
0,
'The limit must be a positive integer. Got "0".',
];

yield 'within the limit (upper)' => [
1,
null,
];

yield 'above limit' => [
2,
null,
];
}

/**
* @dataProvider loadLimitProvider
*/
Expand Down

0 comments on commit ff45221

Please sign in to comment.