Skip to content

Commit 1af6895

Browse files
alan-agius4dgp1130
authored andcommitted
refactor(@angular-devkit/benchmark): add capabilities to benchmark watch processes
1 parent 301bf18 commit 1af6895

4 files changed

Lines changed: 70 additions & 72 deletions

File tree

packages/angular_devkit/benchmark/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ $ benchmark -- node fibonacci.js 40
7474

7575
## Example in watch mode
7676

77+
`watch-script` accepts a node script when using the CLI. When using the API you can provide a `Command` instance.
78+
7779
```
78-
benchmark --verbose --watch-timeout=10000 --watch-matcher="Compiled successfully" --watch-script watch-script.js -- ng serve
80+
benchmark --verbose --watch-timeout 10000 --watch-matcher "Compiled successfully" --watch-script watch-script.js -- ng serve
7981
[benchmark] Benchmarking process over 5 iterations, with up to 5 retries.
8082
[benchmark] ng serve (at D:\sandbox\latest-project)
8183
[benchmark] Process Stats

packages/angular_devkit/benchmark/src/main_spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class MockWriteStream {
2424
}
2525
}
2626

27-
fdescribe('benchmark binary', () => {
27+
describe('benchmark binary', () => {
2828
const benchmarkScript = require.resolve(join(__dirname, './test/fibonacci.js'));
2929
const exitCodeOneScript = require.resolve(join(__dirname, './test/exit-code-one.js'));
3030
const benchmarkWatchScript = require.resolve(join(__dirname, './test/watch-test-cmd.js'));
@@ -179,7 +179,7 @@ fdescribe('benchmark binary', () => {
179179
it('should error when watch-timeout is exceeded', async () => {
180180
const args = [
181181
'--watch-timeout',
182-
'20',
182+
'250',
183183
'--watch-matcher',
184184
'Wrong Match',
185185
'--watch-script',
@@ -191,5 +191,5 @@ fdescribe('benchmark binary', () => {
191191
const res = await main({ args, stdout, stderr });
192192
expect(stderr.lines).toContain('[benchmark] Timeout has occurred\n');
193193
expect(res).toEqual(1);
194-
});
194+
}, 30000);
195195
});

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

Lines changed: 62 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -56,79 +56,75 @@ export function runBenchmarkWatch({
5656
);
5757

5858
return combineLatest([
59-
monitoredProcess.run().pipe(
60-
// In watch mode typically the run will not emit an exit code when it's running sucessfully.
61-
startWith(undefined),
62-
concatMap(processExitCode => {
63-
if (processExitCode !== undefined && processExitCode != command.expectedExitCode) {
64-
logger.debug(`${debugPrefix()} exited with ${processExitCode} but `
65-
+ `${command.expectedExitCode} was expected`);
59+
monitoredProcess.run().pipe(
60+
startWith(undefined),
61+
tap(processExitCode => {
62+
if (processExitCode !== undefined && processExitCode != command.expectedExitCode) {
63+
logger.debug(`${debugPrefix()} exited with ${processExitCode} but `
64+
+ `${command.expectedExitCode} was expected`);
6665

67-
return throwError(processFailed);
68-
}
69-
70-
return of(processExitCode);
71-
}),
72-
),
73-
monitoredProcess.stdout$.pipe(
74-
filter(stdout => stdout.includes(watchMatcher)),
75-
timeout(watchTimeout),
76-
take(1),
77-
),
78-
])
79-
.pipe(
80-
concatMap(() => {
81-
const { cmd, cwd, args } = watchCommand;
82-
failedRuns = 0;
66+
throw processFailed;
67+
}
68+
}),
69+
),
70+
monitoredProcess.stdout$.pipe(
71+
filter(stdout => stdout.toString().includes(watchMatcher)),
72+
take(1),
73+
),
74+
]).pipe(
75+
timeout(watchTimeout),
76+
concatMap(() => {
77+
const { cmd, cwd, args } = watchCommand;
78+
failedRuns = 0;
8379

84-
return of(null)
85-
.pipe(
86-
tap(() => {
87-
const { status, error } = spawnSync(cmd, args, { cwd });
88-
monitoredProcess.resetElapsedTimer();
80+
return of(null)
81+
.pipe(
82+
tap(() => {
83+
const { status, error } = spawnSync(cmd, args, { cwd });
84+
monitoredProcess.resetElapsedTimer();
8985

90-
if (status != command.expectedExitCode) {
91-
logger.debug(`${debugPrefix()} exited with ${status}\n${error?.message}`);
92-
throw processFailed;
93-
}
86+
if (status != command.expectedExitCode) {
87+
logger.debug(`${debugPrefix()} exited with ${status}\n${error?.message}`);
88+
throw processFailed;
89+
}
9490

95-
// Reset fail counter for this iteration.
96-
failedRuns = 0;
97-
}),
98-
tap(() => logger.debug(`${debugPrefix()} starting`)),
99-
concatMap(() => forkJoin(captures.map(capture => capture(stats$)))),
100-
throwIfEmpty(() => new Error('Nothing was captured')),
101-
tap(() => logger.debug(`${debugPrefix()} finished successfully`)),
102-
tap(() => successfulRuns++),
103-
repeat(iterations),
104-
retryWhen(errors => errors
105-
.pipe(concatMap(val => {
106-
// Check if we're still within the retry threshold.
107-
failedRuns++;
91+
// Reset fail counter for this iteration.
92+
failedRuns = 0;
93+
}),
94+
tap(() => logger.debug(`${debugPrefix()} starting`)),
95+
concatMap(() => forkJoin(captures.map(capture => capture(stats$)))),
96+
throwIfEmpty(() => new Error('Nothing was captured')),
97+
tap(() => logger.debug(`${debugPrefix()} finished successfully`)),
98+
tap(() => successfulRuns++),
99+
repeat(iterations),
100+
retryWhen(errors => errors
101+
.pipe(concatMap(val => {
102+
// Check if we're still within the retry threshold.
103+
failedRuns++;
108104

109-
return failedRuns < retries ? of(val) : throwError(val);
110-
})),
111-
),
112-
);
113-
}),
114-
retryWhen(errors => errors
115-
.pipe(concatMap(val => {
116-
// Check if we're still within the retry threshold.
117-
failedRuns++;
105+
return failedRuns < retries ? of(val) : throwError(val);
106+
})),
107+
),
108+
);
109+
}),
110+
retryWhen(errors => errors
111+
.pipe(concatMap(val => {
112+
// Check if we're still within the retry threshold.
113+
failedRuns++;
118114

119-
if (failedRuns < retries) {
120-
return of(val);
121-
}
115+
if (failedRuns < retries) {
116+
return of(val);
117+
}
122118

123-
return throwError(
124-
val === processFailed ?
119+
return throwError(
120+
val === processFailed ?
125121
new MaximumRetriesExceeded(retries) :
126122
val,
127-
);
128-
})),
129-
),
130-
take(iterations),
131-
reduce((acc, val) => acc.map((_, idx) => aggregateMetricGroups(acc[idx], val[idx]))),
132-
tap(groups => reporters.forEach(reporter => reporter(command, groups))),
133-
);
123+
);
124+
})),
125+
),
126+
take(iterations),
127+
reduce((acc, val) => acc.map((_, idx) => aggregateMetricGroups(acc[idx], val[idx]))),
128+
tap(groups => reporters.forEach(reporter => reporter(command, groups))),
129+
);
134130
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { watchFile } = require('fs');
1+
const { watch } = require('fs');
22

33
console.log('Complete');
4-
watchFile(require.resolve('./watch-test-file.txt'), () => console.log('Complete'));
4+
watch(require.resolve('./watch-test-file.txt'), () => console.log('Complete'));

0 commit comments

Comments
 (0)