Skip to content

Commit

Permalink
Scheduler - configurable minimum number of jobs per process
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 16, 2020
1 parent 71bda09 commit 7e28fb8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
5 changes: 4 additions & 1 deletion conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ parameters:
jobSize: 20
processTimeout: 60.0
maximumNumberOfProcesses: 32
minimumNumberOfJobsPerProcess: 2
polluteScopeWithLoopInitialAssignments: true
polluteScopeWithAlwaysIterableForeach: true
polluteCatchScopeWithTryAssignments: false
Expand Down Expand Up @@ -152,7 +153,8 @@ parametersSchema:
parallel: structure([
jobSize: int(),
processTimeout: float(),
maximumNumberOfProcesses: int()
maximumNumberOfProcesses: int(),
minimumNumberOfJobsPerProcess: int()
])
polluteScopeWithLoopInitialAssignments: bool()
polluteScopeWithAlwaysIterableForeach: bool()
Expand Down Expand Up @@ -388,6 +390,7 @@ services:
arguments:
jobSize: %parallel.jobSize%
maximumNumberOfProcesses: %parallel.maximumNumberOfProcesses%
minimumNumberOfJobsPerProcess: %parallel.minimumNumberOfJobsPerProcess%

-
class: PHPStan\Parser\CachedParser
Expand Down
12 changes: 10 additions & 2 deletions src/Parallel/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ class Scheduler
/** @var int */
private $maximumNumberOfProcesses;

/** @var int */
private $minimumNumberOfJobsPerProcess;

public function __construct(
int $jobSize,
int $maximumNumberOfProcesses
int $maximumNumberOfProcesses,
int $minimumNumberOfJobsPerProcess
)
{
$this->jobSize = $jobSize;
$this->maximumNumberOfProcesses = $maximumNumberOfProcesses;
$this->minimumNumberOfJobsPerProcess = $minimumNumberOfJobsPerProcess;
}

/**
Expand All @@ -31,7 +36,10 @@ public function scheduleWork(
): Schedule
{
$jobs = array_chunk($files, $this->jobSize);
$numberOfProcesses = min(count($jobs), $cpuCores);
$numberOfProcesses = min(
(int) floor(count($jobs) / $this->minimumNumberOfJobsPerProcess),
$cpuCores
);

return new Schedule(min($numberOfProcesses, $this->maximumNumberOfProcesses), $jobs);
}
Expand Down
17 changes: 16 additions & 1 deletion tests/PHPStan/Parallel/SchedulerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public function dataSchedule(): array
[
1,
16,
1,
50,
115,
1,
Expand All @@ -21,6 +22,7 @@ public function dataSchedule(): array
[
16,
16,
1,
30,
124,
5,
Expand All @@ -29,6 +31,7 @@ public function dataSchedule(): array
[
16,
3,
1,
30,
124,
3,
Expand All @@ -37,18 +40,29 @@ public function dataSchedule(): array
[
16,
16,
1,
10,
298,
16,
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 8],
],
[
16,
16,
2,
30,
124,
2,
[30, 30, 30, 30, 4],
],
];
}

/**
* @dataProvider dataSchedule
* @param int $cpuCores
* @param int $maximumNumberOfProcesses
* @param int $minimumNumberOfJobsPerProcess
* @param int $jobSize
* @param int $numberOfFiles
* @param int $expectedNumberOfProcesses
Expand All @@ -57,14 +71,15 @@ public function dataSchedule(): array
public function testSchedule(
int $cpuCores,
int $maximumNumberOfProcesses,
int $minimumNumberOfJobsPerProcess,
int $jobSize,
int $numberOfFiles,
int $expectedNumberOfProcesses,
array $expectedJobSizes
): void
{
$files = array_fill(0, $numberOfFiles, 'file.php');
$scheduler = new Scheduler($jobSize, $maximumNumberOfProcesses);
$scheduler = new Scheduler($jobSize, $maximumNumberOfProcesses, $minimumNumberOfJobsPerProcess);
$schedule = $scheduler->scheduleWork($cpuCores, $files);

$this->assertSame($expectedNumberOfProcesses, $schedule->getNumberOfProcesses());
Expand Down

0 comments on commit 7e28fb8

Please sign in to comment.