From c2103354e61b08870ccf2a95a3df3418ebbc8174 Mon Sep 17 00:00:00 2001 From: Aviv Keller Date: Mon, 11 Nov 2024 15:45:59 -0500 Subject: [PATCH] benchmark: add `test_runner/mock-fn` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/55771 Refs: https://github.com/nodejs/node/issues/55723 Reviewed-By: Pietro Marchini Reviewed-By: Chemi Atlow Reviewed-By: Vinícius Lourenço Claro Cardoso --- benchmark/test_runner/mock-fn.js | 48 ++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 benchmark/test_runner/mock-fn.js diff --git a/benchmark/test_runner/mock-fn.js b/benchmark/test_runner/mock-fn.js new file mode 100644 index 00000000000000..6489ccf815e294 --- /dev/null +++ b/benchmark/test_runner/mock-fn.js @@ -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); + } +}