Skip to content

Commit 4093ea4

Browse files
committed
benchmark: add test runner hooks and options
Add benchmarks for node:test hooks and test options. The hooks benchmark covers before, after, beforeEach, and afterEach, with a none mode as the baseline. The test options benchmark covers skip and todo behavior. This adds coverage for part of the benchmark/test_runner gaps tracked in the issue. Refs: #55723 Signed-off-by: Luan Muniz <luan@luanmuniz.com.br>
1 parent be7ea27 commit 4093ea4

2 files changed

Lines changed: 157 additions & 0 deletions

File tree

benchmark/test_runner/hooks.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { finished } = require('node:stream/promises');
5+
const reporter = require('../fixtures/empty-test-reporter');
6+
const {
7+
after,
8+
afterEach,
9+
before,
10+
beforeEach,
11+
describe,
12+
it,
13+
} = require('node:test');
14+
15+
const bench = common.createBenchmark(main, {
16+
n: [10, 100, 1000],
17+
hook: ['none', 'before', 'after', 'beforeEach', 'afterEach'],
18+
}, {
19+
// We don't want to test the reporter here.
20+
flags: ['--test-reporter=./benchmark/fixtures/empty-test-reporter.js'],
21+
});
22+
23+
async function run({ n, hook }) {
24+
// eslint-disable-next-line no-unused-vars
25+
let avoidV8Optimization;
26+
27+
const noop = () => {};
28+
29+
for (let i = 0; i < n; i++) {
30+
describe(`${i}`, () => {
31+
switch (hook) {
32+
case 'before':
33+
before(noop);
34+
break;
35+
case 'after':
36+
after(noop);
37+
break;
38+
case 'beforeEach':
39+
beforeEach(noop);
40+
break;
41+
case 'afterEach':
42+
afterEach(noop);
43+
break;
44+
case 'none':
45+
break;
46+
}
47+
48+
it(`${i}`, () => {
49+
avoidV8Optimization = i;
50+
});
51+
});
52+
}
53+
54+
await finished(reporter);
55+
}
56+
57+
function main(params) {
58+
bench.start();
59+
60+
run(params).then(() => {
61+
bench.end(params.n);
62+
});
63+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { finished } = require('node:stream/promises');
5+
const reporter = require('../fixtures/empty-test-reporter');
6+
const { it } = require('node:test');
7+
8+
const bench = common.createBenchmark(main, {
9+
n: [100, 1000],
10+
option: [
11+
'none',
12+
'skip',
13+
'skip-with-message',
14+
'skip-method',
15+
'skip-method-with-message',
16+
'todo',
17+
'todo-with-message',
18+
'todo-method',
19+
'todo-method-with-message',
20+
],
21+
}, {
22+
// We don't want to test the reporter here.
23+
flags: ['--test-reporter=./benchmark/fixtures/empty-test-reporter.js'],
24+
});
25+
26+
async function run({ n, option }) {
27+
// eslint-disable-next-line no-unused-vars
28+
let avoidV8Optimization;
29+
30+
for (let i = 0; i < n; i++) {
31+
switch (option) {
32+
case 'none':
33+
it(`${i}`, () => {
34+
avoidV8Optimization = i;
35+
});
36+
break;
37+
case 'skip':
38+
it(`${i}`, { skip: true }, () => {
39+
throw new Error('This test should not run.');
40+
});
41+
break;
42+
case 'skip-with-message':
43+
it(`${i}`, { skip: 'skip reason' }, () => {
44+
throw new Error('This test should not run.');
45+
});
46+
break;
47+
case 'skip-method':
48+
it(`${i}`, (t) => {
49+
avoidV8Optimization = i;
50+
t.skip();
51+
});
52+
break;
53+
case 'skip-method-with-message':
54+
it(`${i}`, (t) => {
55+
avoidV8Optimization = i;
56+
t.skip('skip reason');
57+
});
58+
break;
59+
case 'todo':
60+
it(`${i}`, { todo: true }, () => {
61+
avoidV8Optimization = i;
62+
});
63+
break;
64+
case 'todo-with-message':
65+
it(`${i}`, { todo: 'todo reason' }, () => {
66+
avoidV8Optimization = i;
67+
});
68+
break;
69+
case 'todo-method':
70+
it(`${i}`, (t) => {
71+
avoidV8Optimization = i;
72+
t.todo();
73+
});
74+
break;
75+
case 'todo-method-with-message':
76+
it(`${i}`, (t) => {
77+
avoidV8Optimization = i;
78+
t.todo('todo reason');
79+
});
80+
break;
81+
}
82+
}
83+
84+
await finished(reporter);
85+
return n;
86+
}
87+
88+
function main(params) {
89+
bench.start();
90+
91+
run(params).then((ops) => {
92+
bench.end(ops);
93+
});
94+
}

0 commit comments

Comments
 (0)