Skip to content

Commit e47544a

Browse files
Aviv Kellerpull[bot]
Aviv Keller
authored andcommitted
benchmark: add test_runner/mock-fn
PR-URL: #55771 Refs: #55723 Reviewed-By: Pietro Marchini <[email protected]> Reviewed-By: Chemi Atlow <[email protected]> Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]>
1 parent 6397055 commit e47544a

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

benchmark/test_runner/mock-fn.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('node:assert');
5+
const { test } = require('node:test');
6+
7+
const bench = common.createBenchmark(main, {
8+
n: [1e6],
9+
mode: ['define', 'execute'],
10+
}, {
11+
// We don't want to test the reporter here
12+
flags: ['--test-reporter=./benchmark/fixtures/empty-test-reporter.js'],
13+
});
14+
15+
const noop = () => {};
16+
17+
function benchmarkDefine(n) {
18+
let noDead;
19+
test((t) => {
20+
bench.start();
21+
for (let i = 0; i < n; i++) {
22+
noDead = t.mock.fn(noop);
23+
}
24+
bench.end(n);
25+
assert.ok(noDead);
26+
});
27+
}
28+
29+
function benchmarkExecute(n) {
30+
let noDead;
31+
test((t) => {
32+
const mocked = t.mock.fn(noop);
33+
bench.start();
34+
for (let i = 0; i < n; i++) {
35+
noDead = mocked();
36+
}
37+
bench.end(n);
38+
assert.strictEqual(noDead, noop());
39+
});
40+
}
41+
42+
function main({ n, mode }) {
43+
if (mode === 'define') {
44+
benchmarkDefine(n);
45+
} else if (mode === 'execute') {
46+
benchmarkExecute(n);
47+
}
48+
}

0 commit comments

Comments
 (0)