Skip to content

Commit 2c0ddc5

Browse files
committed
test: improve node:bench test coverage
Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode
1 parent b84ea8f commit 2c0ddc5

23 files changed

Lines changed: 603 additions & 19 deletions

lib/internal/bench_runner/cli.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,8 @@ function getChildArgs(path, options) {
428428
},
429429
);
430430
ArrayPrototypePushApply(args, unknownExecArgv);
431+
// Option serialization omits port 0, which would otherwise become 9229.
432+
if (process.debugPort === 0) ArrayPrototypePush(args, '--inspect-port=0');
431433
ArrayPrototypePush(args, '--bench', '--bench-isolation=none');
432434
if (options.namePatternSource.length > 0) {
433435
ArrayPrototypePush(
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
const mode = process.env.NODE_BENCH_EXIT_MODE;
4+
5+
if (mode === 'code') process.exit(2);
6+
if (mode === 'signal') process.kill(process.pid, 'SIGTERM');
7+
8+
const { bench } = require('node:bench');
9+
10+
if (mode === 'late') {
11+
process.on('beforeExit', () => { process.exitCode = 2; });
12+
}
13+
14+
bench('abrupt exit', { samples: 1 }, (b) => {
15+
b.start();
16+
process.hrtime.bigint();
17+
b.end(1);
18+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
module.exports = async function* destroyingReporter(source) {
4+
source.once('bench:start', () => {
5+
source.destroy(new Error('benchmark reporter closed the stream'));
6+
});
7+
yield* source;
8+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
process.send = () => {};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
const { bench } = require('node:bench');
4+
5+
const inspectPort = process.execArgv.filter(
6+
(arg) => arg.startsWith('--inspect-port=')).at(-1);
7+
8+
bench('inspector option', {
9+
params: { inspectPort },
10+
samples: 1,
11+
}, (b) => {
12+
b.start();
13+
process.hrtime.bigint();
14+
b.end(1);
15+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
const common = require('../../common');
4+
5+
const record = process.env.NODE_BENCH_MALFORMED_RECORD === 'summary' ? {
6+
type: 'bench:summary',
7+
data: {
8+
counts: { completed: 0, failed: 0, skipped: 0, total: -1 },
9+
duration_ns: 1n,
10+
success: true,
11+
},
12+
} : null;
13+
14+
process.send?.({ type: 'node:bench:record', record });
15+
setTimeout(() => process.exit(2), common.platformTimeout(10_000));
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
const { bench } = require('node:bench');
4+
5+
bench('many records', { samples: 30 }, (b) => {
6+
process.stdout.write(`${b.index}\n`);
7+
b.start();
8+
process.hrtime.bigint();
9+
b.end(1);
10+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
if (process.env.NODE_BENCH_SEND_ERROR === 'callback') {
4+
process.send = (_message, _handle, _options, callback) => {
5+
callback(new Error('benchmark send callback failed'));
6+
};
7+
} else {
8+
process.send = () => { throw new Error('benchmark send threw'); };
9+
}
10+
11+
const { bench } = require('node:bench');
12+
13+
bench('send error', { samples: 1 }, (b) => {
14+
b.start();
15+
process.hrtime.bigint();
16+
b.end(1);
17+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
const { setTimeout } = require('timers/promises');
4+
5+
module.exports = async function* slowReporter(source) {
6+
const { promise, resolve } = Promise.withResolvers();
7+
let emitted = 0;
8+
const onRecord = () => {
9+
if (++emitted === source.readableHighWaterMark) resolve();
10+
};
11+
for (const type of [
12+
'bench:start',
13+
'bench:sample',
14+
'bench:complete',
15+
'bench:diagnostic',
16+
'bench:summary',
17+
]) {
18+
source.on(type, onRecord);
19+
}
20+
await promise;
21+
22+
let samples = 0;
23+
let stdout = '';
24+
for await (const record of source) {
25+
await setTimeout(2);
26+
if (record.type === 'bench:sample') samples++;
27+
if (record.type === 'bench:diagnostic' &&
28+
record.data.stream === 'stdout') {
29+
stdout += record.data.message;
30+
}
31+
if (record.type === 'bench:summary') {
32+
yield `${JSON.stringify({ samples, stdout })}\n`;
33+
}
34+
}
35+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
throw null;

0 commit comments

Comments
 (0)