|
| 1 | +'use strict'; |
| 2 | +constcommon=require('../common'); |
| 3 | + |
| 4 | +consttmpdir=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 | +constfixtureContent="const test = require('node:test'); test('test has ran');"; |
| 10 | + |
| 11 | +functionmakeTestDirWithFiles(dirPath,count){ |
| 12 | +mkdirSync(dirPath); |
| 13 | +for(leti=0;i<count;i++){ |
| 14 | +writeFileSync(join(dirPath,`test-${i}.js`),fixtureContent); |
| 15 | +} |
| 16 | +} |
| 17 | + |
| 18 | +functiongetTestDirPath(numberOfTestFiles){ |
| 19 | +returnjoin(tmpdir.path,`${numberOfTestFiles}-tests`); |
| 20 | +} |
| 21 | + |
| 22 | +functionsetup(numberOfTestFiles){ |
| 23 | +tmpdir.refresh(); |
| 24 | +constdirPath=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 | +constbench=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 | +asyncfunctionrunBenchmark({ numberOfTestFiles, isolation }){ |
| 43 | +constdirPath=getTestDirPath(numberOfTestFiles); |
| 44 | +conststream=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 | +forawait(const_ofstream); |
| 52 | + |
| 53 | +returnnumberOfTestFiles; |
| 54 | +} |
| 55 | + |
| 56 | +functionmain(params){ |
| 57 | +setup(params.numberOfTestFiles); |
| 58 | +bench.start(); |
| 59 | +runBenchmark(params).then(()=>{ |
| 60 | +bench.end(1); |
| 61 | +}); |
| 62 | +} |
0 commit comments