Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Messenger] Broker maximum process option #242

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
];
}
}
Loading