Skip to content

Commit

Permalink
[Messenger] Broker maximum process option
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed Apr 2, 2024
1 parent 17b3f9c commit 29d99f8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ protected function configure(): void
),
1
)
->addOption(
'maximum-processes',
null,
InputOption::VALUE_REQUIRED,
sprintf(
'Maximum number of processes (used only if "concurrent" is set to "%s")',
self::OPTION_VALUE_CONCURRENT_AUTO
)
)
->addOption(
'timeout',
null,
Expand Down Expand Up @@ -140,6 +149,20 @@ private function calculateAutoConcurrent(InputInterface $input): int
));
}

return max([$minProcesses, (int) round($processesPerCore * $this->cpuCounter->count())]);
$process = max([$minProcesses, (int) round($processesPerCore * $this->cpuCounter->count())]);

if ($maxProcesses = null === $input->getOption('maximum-processes')) {
return $process;
}

$maxProcesses = (int) $input->getOption('maximum-processes');
if ($maxProcesses <= 0) {
throw new InvalidOptionException(sprintf(
'Maximum processes value [%d] is invalid. Must be greater than 0',
$maxProcesses
));
}

return min([$maxProcesses, $process]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ public static function provideTestOption(): iterable
1,
];

yield [
'maximum-processes',
null,
InputOption::VALUE_REQUIRED,
null,
];

yield [
'timeout',
null,
Expand Down Expand Up @@ -134,6 +141,20 @@ public function testExecuteInvalidMinimumProcessesWithAutoConcurrent(): void
]);
}

public function testExecuteInvalidMaximumProcessesWithAutoConcurrent(): void
{
$maxProcesses = random_int(\PHP_INT_MIN, 0);
$this->expectExceptionObject(new InvalidOptionException(sprintf(
'Maximum processes value [%d] is invalid. Must be greater than 0',
$maxProcesses
)));

$this->execute([
'--concurrent' => 'auto',
'--maximum-processes' => $maxProcesses,
]);
}

public function testExecute(): void
{
$concurrent = random_int(1, 10);
Expand Down Expand Up @@ -205,6 +226,7 @@ public function testExecuteWithAutoConcurrent(
int $numCpus,
float $processesPerCore,
int $minProcesses,
?int $maxProcesses,
int $concurrent
): void {
$this
Expand All @@ -223,11 +245,17 @@ function (BrokerStartedEvent $event) use ($concurrent): void {
);

$this
->execute([
'--concurrent' => 'auto',
'--processes-per-core' => $processesPerCore,
'--minimum-processes' => $minProcesses,
])
->execute(
array_filter(
[
'--concurrent' => 'auto',
'--processes-per-core' => $processesPerCore,
'--minimum-processes' => $minProcesses,
'--maximum-processes' => $maxProcesses,
],
static fn ($value) => null !== $value
)
)
->test(CommandDataTester::create(
0,
[
Expand All @@ -245,21 +273,32 @@ public static function provideDataForTestExecuteWithAutoConcurrent(): iterable
'$numCpus' => 4,
'$processesPerCore' => 1.0,
'$minProcesses' => 2,
'$maxProcesses' => null,
'$concurrent' => 4,
];

yield 'float multiplier' => [
'$numCpus' => 4,
'$processesPerCore' => 0.8,
'$minProcesses' => 1,
'$maxProcesses' => null,
'$concurrent' => 3,
];

yield 'minimum processes' => [
'$numCpus' => 2,
'$processesPerCore' => 0.8,
'$minProcesses' => 5,
'$maxProcesses' => null,
'$concurrent' => 5,
];

yield 'maximum processes' => [
'$numCpus' => 2,
'$processesPerCore' => 5,
'$minProcesses' => 1,
'$maxProcesses' => 1,
'$concurrent' => 1,
];
}
}

0 comments on commit 29d99f8

Please sign in to comment.