Skip to content

Commit b8487cd

Browse files
committed
benchmark: avoid switch in test runner benchmarks
Select each benchmark variant before starting the timer so the measured loop does not include dispatch overhead. Signed-off-by: Luan Muniz <luan@luanmuniz.com.br>
1 parent 4093ea4 commit b8487cd

2 files changed

Lines changed: 110 additions & 87 deletions

File tree

benchmark/test_runner/hooks.js

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,51 +13,44 @@ const {
1313
} = require('node:test');
1414

1515
const bench = common.createBenchmark(main, {
16-
n: [10, 100, 1000],
17-
hook: ['none', 'before', 'after', 'beforeEach', 'afterEach'],
16+
n: [1, 10, 100, 1000],
17+
hook: ['before', 'after', 'beforeEach', 'afterEach'],
1818
}, {
1919
// We don't want to test the reporter here.
2020
flags: ['--test-reporter=./benchmark/fixtures/empty-test-reporter.js'],
2121
});
2222

23-
async function run({ n, hook }) {
24-
// eslint-disable-next-line no-unused-vars
25-
let avoidV8Optimization;
23+
const noop = () => {};
2624

27-
const noop = () => {};
25+
const hookList = {
26+
before: before,
27+
after: after,
28+
beforeEach: beforeEach,
29+
afterEach: afterEach,
30+
};
2831

29-
for (let i = 0; i < n; i++) {
32+
function run(loopAmount, avoidV8Optimization, hookFn) {
33+
for (let i = 0; i < loopAmount; i++) {
3034
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-
}
35+
hookFn(noop);
4736

4837
it(`${i}`, () => {
4938
avoidV8Optimization = i;
5039
});
5140
});
5241
}
5342

54-
await finished(reporter);
43+
return finished(reporter);
5544
}
5645

5746
function main(params) {
47+
// eslint-disable-next-line prefer-const
48+
let avoidV8Optimization = 0;
49+
const hookFn = hookList[params.hook];
50+
5851
bench.start();
5952

60-
run(params).then(() => {
53+
run(params.n, avoidV8Optimization, hookFn).then(() => {
6154
bench.end(params.n);
6255
});
6356
}

benchmark/test_runner/test-options.js

Lines changed: 92 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const reporter = require('../fixtures/empty-test-reporter');
66
const { it } = require('node:test');
77

88
const bench = common.createBenchmark(main, {
9-
n: [100, 1000],
9+
n: [1, 10, 100, 1000],
1010
option: [
1111
'none',
1212
'skip',
@@ -23,72 +23,102 @@ const bench = common.createBenchmark(main, {
2323
flags: ['--test-reporter=./benchmark/fixtures/empty-test-reporter.js'],
2424
});
2525

26-
async function run({ n, option }) {
27-
// eslint-disable-next-line no-unused-vars
28-
let avoidV8Optimization;
26+
const allTests = {
27+
'none': (loopAmount, avoidV8Optimization) => {
28+
for (let i = 0; i < loopAmount; i++) {
29+
it(`${i}`, () => {
30+
avoidV8Optimization = i;
31+
});
32+
}
2933

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;
34+
return finished(reporter);
35+
},
36+
'skip': (loopAmount) => {
37+
for (let i = 0; i < loopAmount; i++) {
38+
it(`${i}`, { skip: true }, () => {
39+
throw new Error('This test should not run.');
40+
});
8141
}
82-
}
8342

84-
await finished(reporter);
85-
return n;
86-
}
43+
return finished(reporter);
44+
},
45+
'skip-with-message': (loopAmount) => {
46+
for (let i = 0; i < loopAmount; i++) {
47+
it(`${i}`, { skip: 'skip reason' }, () => {
48+
throw new Error('This test should not run.');
49+
});
50+
}
51+
52+
return finished(reporter);
53+
},
54+
'skip-method': (loopAmount, avoidV8Optimization) => {
55+
for (let i = 0; i < loopAmount; i++) {
56+
it(`${i}`, (t) => {
57+
avoidV8Optimization = i;
58+
t.skip();
59+
});
60+
}
61+
62+
return finished(reporter);
63+
},
64+
'skip-method-with-message': (loopAmount, avoidV8Optimization) => {
65+
for (let i = 0; i < loopAmount; i++) {
66+
it(`${i}`, (t) => {
67+
avoidV8Optimization = i;
68+
t.skip('skip reason');
69+
});
70+
}
71+
72+
return finished(reporter);
73+
},
74+
'todo': (loopAmount, avoidV8Optimization) => {
75+
for (let i = 0; i < loopAmount; i++) {
76+
it(`${i}`, { todo: true }, () => {
77+
avoidV8Optimization = i;
78+
});
79+
}
80+
81+
return finished(reporter);
82+
},
83+
'todo-with-message': (loopAmount, avoidV8Optimization) => {
84+
for (let i = 0; i < loopAmount; i++) {
85+
it(`${i}`, { todo: 'todo reason' }, () => {
86+
avoidV8Optimization = i;
87+
});
88+
}
89+
90+
return finished(reporter);
91+
},
92+
'todo-method': (loopAmount, avoidV8Optimization) => {
93+
for (let i = 0; i < loopAmount; i++) {
94+
it(`${i}`, (t) => {
95+
avoidV8Optimization = i;
96+
t.todo();
97+
});
98+
}
99+
100+
return finished(reporter);
101+
},
102+
'todo-method-with-message': (loopAmount, avoidV8Optimization) => {
103+
for (let i = 0; i < loopAmount; i++) {
104+
it(`${i}`, (t) => {
105+
avoidV8Optimization = i;
106+
t.todo('todo reason');
107+
});
108+
}
109+
110+
return finished(reporter);
111+
},
112+
};
113+
114+
function main({ n, option }) {
115+
// eslint-disable-next-line prefer-const
116+
let avoidV8Optimization = 0;
117+
const runOption = allTests[option];
87118

88-
function main(params) {
89119
bench.start();
90120

91-
run(params).then((ops) => {
92-
bench.end(ops);
121+
runOption(n, avoidV8Optimization).then(() => {
122+
bench.end(n);
93123
});
94124
}

0 commit comments

Comments
 (0)