Skip to content

Commit 71bda09

Browse files
committed
Scheduler - configurable maximum number of processes (threads to use)
1 parent a98b908 commit 71bda09

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

conf/config.neon

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ parameters:
4141
parallel:
4242
jobSize: 20
4343
processTimeout: 60.0
44+
maximumNumberOfProcesses: 32
4445
polluteScopeWithLoopInitialAssignments: true
4546
polluteScopeWithAlwaysIterableForeach: true
4647
polluteCatchScopeWithTryAssignments: false
@@ -150,7 +151,8 @@ parametersSchema:
150151
reportStaticMethodSignatures: bool()
151152
parallel: structure([
152153
jobSize: int(),
153-
processTimeout: float()
154+
processTimeout: float(),
155+
maximumNumberOfProcesses: int()
154156
])
155157
polluteScopeWithLoopInitialAssignments: bool()
156158
polluteScopeWithAlwaysIterableForeach: bool()
@@ -385,6 +387,7 @@ services:
385387
class: PHPStan\Parallel\Scheduler
386388
arguments:
387389
jobSize: %parallel.jobSize%
390+
maximumNumberOfProcesses: %parallel.maximumNumberOfProcesses%
388391

389392
-
390393
class: PHPStan\Parser\CachedParser

src/Parallel/Scheduler.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,16 @@ class Scheduler
88
/** @var int */
99
private $jobSize;
1010

11-
public function __construct(int $jobSize)
11+
/** @var int */
12+
private $maximumNumberOfProcesses;
13+
14+
public function __construct(
15+
int $jobSize,
16+
int $maximumNumberOfProcesses
17+
)
1218
{
1319
$this->jobSize = $jobSize;
20+
$this->maximumNumberOfProcesses = $maximumNumberOfProcesses;
1421
}
1522

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

29-
return new Schedule($numberOfProcesses, $jobs);
36+
return new Schedule(min($numberOfProcesses, $this->maximumNumberOfProcesses), $jobs);
3037
}
3138

3239
}

tests/PHPStan/Parallel/SchedulerTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,30 @@ public function dataSchedule(): array
1212
return [
1313
[
1414
1,
15+
16,
1516
50,
1617
115,
1718
1,
1819
[50, 50, 15],
1920
],
2021
[
22+
16,
2123
16,
2224
30,
2325
124,
2426
5,
2527
[30, 30, 30, 30, 4],
2628
],
2729
[
30+
16,
31+
3,
32+
30,
33+
124,
34+
3,
35+
[30, 30, 30, 30, 4],
36+
],
37+
[
38+
16,
2839
16,
2940
10,
3041
298,
@@ -37,21 +48,23 @@ public function dataSchedule(): array
3748
/**
3849
* @dataProvider dataSchedule
3950
* @param int $cpuCores
51+
* @param int $maximumNumberOfProcesses
4052
* @param int $jobSize
4153
* @param int $numberOfFiles
4254
* @param int $expectedNumberOfProcesses
4355
* @param array<int> $expectedJobSizes
4456
*/
4557
public function testSchedule(
4658
int $cpuCores,
59+
int $maximumNumberOfProcesses,
4760
int $jobSize,
4861
int $numberOfFiles,
4962
int $expectedNumberOfProcesses,
5063
array $expectedJobSizes
5164
): void
5265
{
5366
$files = array_fill(0, $numberOfFiles, 'file.php');
54-
$scheduler = new Scheduler($jobSize);
67+
$scheduler = new Scheduler($jobSize, $maximumNumberOfProcesses);
5568
$schedule = $scheduler->scheduleWork($cpuCores, $files);
5669

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

0 commit comments

Comments
 (0)