Skip to content

Commit

Permalink
benchmark: add test_runner/mock-fn
Browse files Browse the repository at this point in the history
PR-URL: #55771
Refs: #55723
Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
  • Loading branch information
RedYetiDev authored and aduh95 committed Nov 16, 2024
1 parent 9c99255 commit c210335
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions benchmark/test_runner/mock-fn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';

const common = require('../common');
const assert = require('node:assert');
const { test } = require('node:test');

const bench = common.createBenchmark(main, {
n: [1e6],
mode: ['define', 'execute'],
}, {
// We don't want to test the reporter here
flags: ['--test-reporter=./benchmark/fixtures/empty-test-reporter.js'],
});

const noop = () => {};

function benchmarkDefine(n) {
let noDead;
test((t) => {
bench.start();
for (let i = 0; i < n; i++) {
noDead = t.mock.fn(noop);
}
bench.end(n);
assert.ok(noDead);
});
}

function benchmarkExecute(n) {
let noDead;
test((t) => {
const mocked = t.mock.fn(noop);
bench.start();
for (let i = 0; i < n; i++) {
noDead = mocked();
}
bench.end(n);
assert.strictEqual(noDead, noop());
});
}

function main({ n, mode }) {
if (mode === 'define') {
benchmarkDefine(n);
} else if (mode === 'execute') {
benchmarkExecute(n);
}
}

0 comments on commit c210335

Please sign in to comment.