Skip to content

Commit c34d0b5

Browse files
alan-agius4filipesilva
authored andcommitted
refactor(@angular-devkit/benchmark): make changes to make this package strict compliant
1 parent a3f6cd3 commit c34d0b5

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

packages/angular_devkit/benchmark/src/monitored-process.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import { SpawnOptions, spawn } from 'child_process';
9+
import * as pidusage from 'pidusage';
910
import { Observable, Subject, from, timer } from 'rxjs';
1011
import { concatMap, map, onErrorResumeNext, tap } from 'rxjs/operators';
1112
import { Command } from './command';
1213
import { AggregatedProcessStats, MonitoredProcess } from './interfaces';
13-
const pidusage = require('pidusage');
14+
1415
const pidtree = require('pidtree');
1516
const treeKill = require('tree-kill');
1617

17-
1818
// Cleanup when the parent process exits.
1919
const defaultProcessExitCb = () => { };
2020
let processExitCb = defaultProcessExitCb;
@@ -28,10 +28,11 @@ export class LocalMonitoredProcess implements MonitoredProcess {
2828
private stdout = new Subject<Buffer>();
2929
private stderr = new Subject<Buffer>();
3030
private pollingRate = 100;
31+
private elapsedTimer = 0;
32+
3133
stats$: Observable<AggregatedProcessStats> = this.stats.asObservable();
3234
stdout$: Observable<Buffer> = this.stdout.asObservable();
3335
stderr$: Observable<Buffer> = this.stderr.asObservable();
34-
private elapsedTimer: number;
3536

3637
constructor(
3738
private command: Command,
@@ -52,24 +53,27 @@ export class LocalMonitoredProcess implements MonitoredProcess {
5253
// Emit output and stats.
5354
childProcess.stdout.on('data', (data: Buffer) => this.stdout.next(data));
5455
childProcess.stderr.on('data', (data: Buffer) => this.stderr.next(data));
56+
5557
const statsSubs = timer(0, this.pollingRate).pipe(
56-
concatMap(() => from(pidtree(childProcess.pid, { root: true }))),
58+
concatMap(() => from(pidtree(childProcess.pid, { root: true }) as Promise<number[]>)),
5759
concatMap((pids: number[]) => from(pidusage(pids, { maxage: 5 * this.pollingRate }))),
58-
map((statsByProcess: { [key: string]: AggregatedProcessStats }) => {
60+
map(statsByProcess => {
5961
// Ignore the spawned shell in the total process number.
6062
const pids = Object.keys(statsByProcess)
6163
.filter(pid => pid != childProcess.pid.toString());
6264
const processes = pids.length;
6365
// We want most stats from the parent process.
6466
const { pid, ppid, ctime, elapsed, timestamp } = statsByProcess[childProcess.pid];
67+
6568
// CPU and memory should be agreggated.
66-
let cpu = 0, memory = 0;
69+
let cpu = 0;
70+
let memory = 0;
6771
for (const pid of pids) {
6872
cpu += statsByProcess[pid].cpu;
6973
memory += statsByProcess[pid].memory;
7074
}
7175

72-
return {
76+
const stats: AggregatedProcessStats = {
7377
processes,
7478
cpu,
7579
memory,
@@ -78,7 +82,9 @@ export class LocalMonitoredProcess implements MonitoredProcess {
7882
ctime,
7983
elapsed: this.useProcessTime ? elapsed : (Date.now() - this.elapsedTimer),
8084
timestamp,
81-
} as AggregatedProcessStats;
85+
};
86+
87+
return stats;
8288
}),
8389
tap(stats => this.stats.next(stats)),
8490
onErrorResumeNext(),

packages/angular_devkit/benchmark/src/run-benchmark.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export function runBenchmark({
4141
let aggregatedMetricGroups: MetricGroup[] = [];
4242

4343
// Run the process and captures, wait for both to finish, and average out the metrics.
44-
return new Observable(obs => {
44+
return new Observable<[LocalMonitoredProcess, ...Observable<MetricGroup>[]]>(obs => {
4545
const monitoredProcess = new LocalMonitoredProcess(command);
4646
const metric$ = captures.map(capture => capture(monitoredProcess.stats$));
4747
obs.next([monitoredProcess, ...metric$]);

0 commit comments

Comments
 (0)