Skip to content

Commit

Permalink
Scheduler - configurable maximum number of processes (threads to use)
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 16, 2020
1 parent a98b908 commit 71bda09
Show file tree
Hide file tree
Showing 3 changed files with 27 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 @@ -41,6 +41,7 @@ parameters:
parallel:
jobSize: 20
processTimeout: 60.0
maximumNumberOfProcesses: 32
polluteScopeWithLoopInitialAssignments: true
polluteScopeWithAlwaysIterableForeach: true
polluteCatchScopeWithTryAssignments: false
Expand Down Expand Up @@ -150,7 +151,8 @@ parametersSchema:
reportStaticMethodSignatures: bool()
parallel: structure([
jobSize: int(),
processTimeout: float()
processTimeout: float(),
maximumNumberOfProcesses: int()
])
polluteScopeWithLoopInitialAssignments: bool()
polluteScopeWithAlwaysIterableForeach: bool()
Expand Down Expand Up @@ -385,6 +387,7 @@ services:
class: PHPStan\Parallel\Scheduler
arguments:
jobSize: %parallel.jobSize%
maximumNumberOfProcesses: %parallel.maximumNumberOfProcesses%

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

public function __construct(int $jobSize)
/** @var int */
private $maximumNumberOfProcesses;

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

/**
Expand All @@ -26,7 +33,7 @@ public function scheduleWork(
$jobs = array_chunk($files, $this->jobSize);
$numberOfProcesses = min(count($jobs), $cpuCores);

return new Schedule($numberOfProcesses, $jobs);
return new Schedule(min($numberOfProcesses, $this->maximumNumberOfProcesses), $jobs);
}

}
15 changes: 14 additions & 1 deletion tests/PHPStan/Parallel/SchedulerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,30 @@ public function dataSchedule(): array
return [
[
1,
16,
50,
115,
1,
[50, 50, 15],
],
[
16,
16,
30,
124,
5,
[30, 30, 30, 30, 4],
],
[
16,
3,
30,
124,
3,
[30, 30, 30, 30, 4],
],
[
16,
16,
10,
298,
Expand All @@ -37,21 +48,23 @@ public function dataSchedule(): array
/**
* @dataProvider dataSchedule
* @param int $cpuCores
* @param int $maximumNumberOfProcesses
* @param int $jobSize
* @param int $numberOfFiles
* @param int $expectedNumberOfProcesses
* @param array<int> $expectedJobSizes
*/
public function testSchedule(
int $cpuCores,
int $maximumNumberOfProcesses,
int $jobSize,
int $numberOfFiles,
int $expectedNumberOfProcesses,
array $expectedJobSizes
): void
{
$files = array_fill(0, $numberOfFiles, 'file.php');
$scheduler = new Scheduler($jobSize);
$scheduler = new Scheduler($jobSize, $maximumNumberOfProcesses);
$schedule = $scheduler->scheduleWork($cpuCores, $files);

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

0 comments on commit 71bda09

Please sign in to comment.