test_runner: run single test file benchmark · nodejs/node@4274c6a

1+

'use strict';

2+

const common = require('../common');

3+4+

const tmpdir = require('../../test/common/tmpdir');

5+

const { run } = require('node:test');

6+

const { writeFileSync, mkdirSync } = require('node:fs');

7+

const { join } = require('node:path');

8+9+

const fixtureContent = "const test = require('node:test'); test('test has ran');";

10+11+

function makeTestDirWithFiles(dirPath, count) {

12+

mkdirSync(dirPath);

13+

for (let i = 0; i < count; i++) {

14+

writeFileSync(join(dirPath, `test-${i}.js`), fixtureContent);

15+

}

16+

}

17+18+

function getTestDirPath(numberOfTestFiles) {

19+

return join(tmpdir.path, `${numberOfTestFiles}-tests`);

20+

}

21+22+

function setup(numberOfTestFiles) {

23+

tmpdir.refresh();

24+

const dirPath = getTestDirPath(numberOfTestFiles);

25+

makeTestDirWithFiles(dirPath, numberOfTestFiles);

26+

}

27+28+

/**

29+

* This benchmark evaluates the overhead of running a single test file under different

30+

* isolation modes.

31+

* Specifically, it compares the performance of running tests in the

32+

* same process versus creating multiple processes.

33+

*/

34+

const bench = common.createBenchmark(main, {

35+

numberOfTestFiles: [1, 10, 100],

36+

isolation: ['none', 'process'],

37+

}, {

38+

// We don't want to test the reporter here

39+

flags: ['--test-reporter=./benchmark/fixtures/empty-test-reporter.js'],

40+

});

41+42+

async function runBenchmark({ numberOfTestFiles, isolation }) {

43+

const dirPath = getTestDirPath(numberOfTestFiles);

44+

const stream = run({

45+

cwd: dirPath,

46+

isolation,

47+

concurrency: false, // We don't want to run tests concurrently

48+

});

49+50+

// eslint-disable-next-line no-unused-vars

51+

for await (const _ of stream);

52+53+

return numberOfTestFiles;

54+

}

55+56+

function main(params) {

57+

setup(params.numberOfTestFiles);

58+

bench.start();

59+

runBenchmark(params).then(() => {

60+

bench.end(1);

61+

});

62+

}