From 7d28de9a7991ce83e304600458e26dc958f55aa2 Mon Sep 17 00:00:00 2001 From: Aviv Keller Date: Fri, 15 Nov 2024 22:55:13 -0500 Subject: [PATCH] benchmark: add `test-reporters` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/55757 Refs: https://github.com/nodejs/node/issues/55723 Reviewed-By: Pietro Marchini Reviewed-By: Vinícius Lourenço Claro Cardoso Reviewed-By: Raz Luvaton --- benchmark/fixtures/basic-test-runner.js | 11 +++++++ benchmark/test_runner/test-reporters.js | 41 +++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 benchmark/fixtures/basic-test-runner.js create mode 100644 benchmark/test_runner/test-reporters.js diff --git a/benchmark/fixtures/basic-test-runner.js b/benchmark/fixtures/basic-test-runner.js new file mode 100644 index 00000000000000..d57f70da0d40a5 --- /dev/null +++ b/benchmark/fixtures/basic-test-runner.js @@ -0,0 +1,11 @@ +const { test } = require('node:test'); + +test('should pass', () => {}); +test('should fail', () => { throw new Error('fail'); }); +test('should skip', { skip: true }, () => {}); +test('parent', (t) => { + t.test('should fail', () => { throw new Error('fail'); }); + t.test('should pass but parent fail', (t, done) => { + setImmediate(done); + }); +}); diff --git a/benchmark/test_runner/test-reporters.js b/benchmark/test_runner/test-reporters.js new file mode 100644 index 00000000000000..4eecd1c4306005 --- /dev/null +++ b/benchmark/test_runner/test-reporters.js @@ -0,0 +1,41 @@ +'use strict'; + +const common = require('../common'); +const { run } = require('node:test'); +const reporters = require('node:test/reporters'); +const { Readable } = require('node:stream'); +const assert = require('node:assert'); + +const bench = common.createBenchmark(main, { + n: [1e4], + reporter: Object.keys(reporters), +}); + +// No need to run this for every benchmark, +// it should always be the same data. +const stream = run({ + files: ['../fixtures/basic-test-runner.js'], +}); +let testResults; + +async function main({ n, reporter: r }) { + testResults ??= await stream.toArray(); + + // Create readable streams for each iteration + const readables = Array.from({ length: n }, () => Readable.from(testResults)); + + // Get the selected reporter + const reporter = reporters[r]; + + bench.start(); + + let noDead; + for (const readable of readables) { + // Process each readable stream through the reporter + noDead = await readable.compose(reporter).toArray(); + } + + bench.end(n); + + assert.ok(noDead); +}