diff --git a/benchmark/test_runner/hooks.js b/benchmark/test_runner/hooks.js new file mode 100644 index 00000000000000..371cfe9d35ef61 --- /dev/null +++ b/benchmark/test_runner/hooks.js @@ -0,0 +1,56 @@ +'use strict'; + +const common = require('../common'); +const { finished } = require('node:stream/promises'); +const reporter = require('../fixtures/empty-test-reporter'); +const { + after, + afterEach, + before, + beforeEach, + describe, + it, +} = require('node:test'); + +const bench = common.createBenchmark(main, { + n: [1, 10, 100, 1000], + hook: ['before', 'after', 'beforeEach', 'afterEach'], +}, { + // We don't want to test the reporter here. + flags: ['--test-reporter=./benchmark/fixtures/empty-test-reporter.js'], +}); + +const noop = () => {}; + +const hookList = { + before: before, + after: after, + beforeEach: beforeEach, + afterEach: afterEach, +}; + +function run(loopAmount, avoidV8Optimization, hookFn) { + for (let i = 0; i < loopAmount; i++) { + describe(`${i}`, () => { + hookFn(noop); + + it(`${i}`, () => { + avoidV8Optimization = i; + }); + }); + } + + return finished(reporter); +} + +function main(params) { + // eslint-disable-next-line prefer-const + let avoidV8Optimization = 0; + const hookFn = hookList[params.hook]; + + bench.start(); + + run(params.n, avoidV8Optimization, hookFn).then(() => { + bench.end(params.n); + }); +} diff --git a/benchmark/test_runner/test-options.js b/benchmark/test_runner/test-options.js new file mode 100644 index 00000000000000..2b76784ca541ef --- /dev/null +++ b/benchmark/test_runner/test-options.js @@ -0,0 +1,124 @@ +'use strict'; + +const common = require('../common'); +const { finished } = require('node:stream/promises'); +const reporter = require('../fixtures/empty-test-reporter'); +const { it } = require('node:test'); + +const bench = common.createBenchmark(main, { + n: [1, 10, 100, 1000], + option: [ + 'none', + 'skip', + 'skip-with-message', + 'skip-method', + 'skip-method-with-message', + 'todo', + 'todo-with-message', + 'todo-method', + 'todo-method-with-message', + ], +}, { + // We don't want to test the reporter here. + flags: ['--test-reporter=./benchmark/fixtures/empty-test-reporter.js'], +}); + +const allTests = { + 'none': (loopAmount, avoidV8Optimization) => { + for (let i = 0; i < loopAmount; i++) { + it(`${i}`, () => { + avoidV8Optimization = i; + }); + } + + return finished(reporter); + }, + 'skip': (loopAmount) => { + for (let i = 0; i < loopAmount; i++) { + it(`${i}`, { skip: true }, () => { + throw new Error('This test should not run.'); + }); + } + + return finished(reporter); + }, + 'skip-with-message': (loopAmount) => { + for (let i = 0; i < loopAmount; i++) { + it(`${i}`, { skip: 'skip reason' }, () => { + throw new Error('This test should not run.'); + }); + } + + return finished(reporter); + }, + 'skip-method': (loopAmount, avoidV8Optimization) => { + for (let i = 0; i < loopAmount; i++) { + it(`${i}`, (t) => { + avoidV8Optimization = i; + t.skip(); + }); + } + + return finished(reporter); + }, + 'skip-method-with-message': (loopAmount, avoidV8Optimization) => { + for (let i = 0; i < loopAmount; i++) { + it(`${i}`, (t) => { + avoidV8Optimization = i; + t.skip('skip reason'); + }); + } + + return finished(reporter); + }, + 'todo': (loopAmount, avoidV8Optimization) => { + for (let i = 0; i < loopAmount; i++) { + it(`${i}`, { todo: true }, () => { + avoidV8Optimization = i; + }); + } + + return finished(reporter); + }, + 'todo-with-message': (loopAmount, avoidV8Optimization) => { + for (let i = 0; i < loopAmount; i++) { + it(`${i}`, { todo: 'todo reason' }, () => { + avoidV8Optimization = i; + }); + } + + return finished(reporter); + }, + 'todo-method': (loopAmount, avoidV8Optimization) => { + for (let i = 0; i < loopAmount; i++) { + it(`${i}`, (t) => { + avoidV8Optimization = i; + t.todo(); + }); + } + + return finished(reporter); + }, + 'todo-method-with-message': (loopAmount, avoidV8Optimization) => { + for (let i = 0; i < loopAmount; i++) { + it(`${i}`, (t) => { + avoidV8Optimization = i; + t.todo('todo reason'); + }); + } + + return finished(reporter); + }, +}; + +function main({ n, option }) { + // eslint-disable-next-line prefer-const + let avoidV8Optimization = 0; + const runOption = allTests[option]; + + bench.start(); + + runOption(n, avoidV8Optimization).then(() => { + bench.end(n); + }); +}