/
concurrency-token-provider.ts
55 lines (48 loc) · 2.3 KB
/
concurrency-token-provider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import os = require('os');
import { StrykerOptions } from '@stryker-mutator/api/core';
import { ReplaySubject, Observable, range } from 'rxjs';
import { Disposable, tokens } from 'typed-inject';
import { commonTokens } from '@stryker-mutator/api/plugin';
import { Logger } from '@stryker-mutator/api/logging';
export class ConcurrencyTokenProvider implements Disposable {
private readonly concurrencyCheckers: number;
private readonly concurrencyTestRunners: number;
private readonly testRunnerTokenSubject: ReplaySubject<number> = new ReplaySubject();
public get testRunnerToken$(): Observable<number> {
return this.testRunnerTokenSubject;
}
public readonly checkerToken$: Observable<number>;
public static readonly inject = tokens(commonTokens.options, commonTokens.logger);
constructor(options: Pick<StrykerOptions, 'concurrency' | 'checkers'>, private readonly log: Logger) {
const cpuCount = os.cpus().length;
const concurrency = options.concurrency ?? (cpuCount > 4 ? cpuCount - 1 : cpuCount);
if (options.checkers.length > 0) {
this.concurrencyCheckers = Math.max(Math.ceil(concurrency / 2), 1);
this.checkerToken$ = range(this.concurrencyCheckers);
this.concurrencyTestRunners = Math.max(Math.floor(concurrency / 2), 1);
log.info('Creating %s checker process(es) and %s test runner process(es).', this.concurrencyCheckers, this.concurrencyTestRunners);
} else {
this.concurrencyCheckers = 0;
this.checkerToken$ = range(1); // at least one checker, the `CheckerFacade` will not create worker process.
this.concurrencyTestRunners = concurrency;
log.info('Creating %s test runner process(es).', this.concurrencyTestRunners);
}
Array.from({ length: this.concurrencyTestRunners }).forEach(() => this.testRunnerTokenSubject.next(this.tick()));
}
public freeCheckers() {
if (this.concurrencyCheckers > 0) {
this.log.debug('Checking done, creating %s additional test runner process(es)', this.concurrencyCheckers);
for (let i = 0; i < this.concurrencyCheckers; i++) {
this.testRunnerTokenSubject.next(this.tick());
}
this.testRunnerTokenSubject.complete();
}
}
private count = 0;
private tick() {
return this.count++;
}
public dispose(): void {
this.testRunnerTokenSubject.complete();
}
}