From 6818d715f7ae7743a5d97d44bd923dffe0e8561f Mon Sep 17 00:00:00 2001 From: Raz Luvaton <16746759+rluvaton@users.noreply.github.com> Date: Sat, 29 Jul 2023 21:07:44 +0300 Subject: [PATCH] benchmark: add benchmarks for the test_runner PR-URL: https://github.com/nodejs/node/pull/48931 Reviewed-By: Moshe Atlow Reviewed-By: Chemi Atlow Reviewed-By: Debadree Chatterjee --- benchmark/fixtures/empty-test-reporter.js | 8 +++ .../test_runner/global-concurrent-tests.js | 46 +++++++++++++ .../test_runner/global-sequential-tests.js | 46 +++++++++++++ benchmark/test_runner/suite-tests.js | 65 +++++++++++++++++++ 4 files changed, 165 insertions(+) create mode 100644 benchmark/fixtures/empty-test-reporter.js create mode 100644 benchmark/test_runner/global-concurrent-tests.js create mode 100644 benchmark/test_runner/global-sequential-tests.js create mode 100644 benchmark/test_runner/suite-tests.js diff --git a/benchmark/fixtures/empty-test-reporter.js b/benchmark/fixtures/empty-test-reporter.js new file mode 100644 index 00000000000000..da6a175defadf1 --- /dev/null +++ b/benchmark/fixtures/empty-test-reporter.js @@ -0,0 +1,8 @@ +const { PassThrough } = require('node:stream'); + +module.exports = new PassThrough({ + objectMode: true, + transform(chunk, encoding, callback) { + callback(null) + }, +}); diff --git a/benchmark/test_runner/global-concurrent-tests.js b/benchmark/test_runner/global-concurrent-tests.js new file mode 100644 index 00000000000000..6e3fb5c8ba5a1b --- /dev/null +++ b/benchmark/test_runner/global-concurrent-tests.js @@ -0,0 +1,46 @@ +'use strict'; +const common = require('../common'); +const { it } = require('node:test'); + +const bench = common.createBenchmark(main, { + n: [100, 1000, 1e4], + type: ['sync', 'async'], +}, { + // We don't want to test the reporter here + flags: ['--test-reporter=./benchmark/fixtures/empty-test-reporter.js'], +}); + +async function run(n, type) { + const promises = new Array(n); + + // eslint-disable-next-line no-unused-vars + let avoidV8Optimization; + + switch (type) { + case 'sync': { + for (let i = 0; i < n; i++) { + promises[i] = it(`${i}`, () => { + avoidV8Optimization = i; + }); + } + break; + } + + case 'async': + for (let i = 0; i < n; i++) { + promises[i] = it(`${i}`, async () => { + avoidV8Optimization = i; + }); + } + break; + } + + await Promise.all(promises); +} + +function main({ n, type }) { + bench.start(); + run(n, type).then(() => { + bench.end(n); + }); +} diff --git a/benchmark/test_runner/global-sequential-tests.js b/benchmark/test_runner/global-sequential-tests.js new file mode 100644 index 00000000000000..296022717c9b2f --- /dev/null +++ b/benchmark/test_runner/global-sequential-tests.js @@ -0,0 +1,46 @@ +'use strict'; +const common = require('../common'); +const { it } = require('node:test'); + + +const bench = common.createBenchmark(main, { + n: [100, 1000, 1e4], + type: ['sync', 'async'], +}, { + // We don't want to test the reporter here + flags: ['--test-reporter=./benchmark/fixtures/empty-test-reporter.js'], +}); + +async function run(n, type) { + // eslint-disable-next-line no-unused-vars + let avoidV8Optimization; + + const promises = new Array(n); + switch (type) { + case 'sync': { + for (let i = 0; i < n; i++) { + await it(`${i}`, () => { + avoidV8Optimization = i; + }); + } + break; + } + + case 'async': + for (let i = 0; i < n; i++) { + await it(`${i}`, async () => { + avoidV8Optimization = i; + }); + } + break; + } + + await Promise.all(promises); +} + +function main({ n }) { + bench.start(); + run(n).then(() => { + bench.end(n); + }); +} diff --git a/benchmark/test_runner/suite-tests.js b/benchmark/test_runner/suite-tests.js new file mode 100644 index 00000000000000..88b2aa1c74b19e --- /dev/null +++ b/benchmark/test_runner/suite-tests.js @@ -0,0 +1,65 @@ +'use strict'; +const common = require('../common'); +const { finished } = require('node:stream/promises'); + +const reporter = require('../fixtures/empty-test-reporter'); + +const { describe, it } = require('node:test'); + +const bench = common.createBenchmark(main, { + numberOfSuites: [10, 100], + testsPerSuite: [10, 100, 1000], + testType: ['sync', 'async'], + concurrency: ['yes', 'no'], +}, { + // We don't want to test the reporter here + flags: ['--test-reporter=./benchmark/fixtures/empty-test-reporter.js'], +}); + +async function run({ numberOfSuites, testsPerSuite, testType, concurrency }) { + concurrency = concurrency === 'yes'; + + // eslint-disable-next-line no-unused-vars + let avoidV8Optimization; + + switch (testType) { + case 'sync': { + for (let i = 0; i < numberOfSuites; i++) { + describe(`${i}`, { concurrency }, () => { + for (let j = 0; j < testsPerSuite; j++) { + it(`${j}`, () => { + avoidV8Optimization = i; + }); + } + }); + } + + break; + } + + case 'async': { + for (let i = 0; i < numberOfSuites; i++) { + describe(`${i}`, { concurrency }, () => { + for (let j = 0; j < testsPerSuite; j++) { + it(`${j}`, async () => { + avoidV8Optimization = i; + }); + } + }); + } + + break; + } + } + + await finished(reporter); + + return numberOfSuites * testsPerSuite; +} + +function main(params) { + bench.start(); + run(params).then((ops) => { + bench.end(ops); + }); +}