From 60f98baf6ac75d3c2e7ddfb4d1678ccb67577706 Mon Sep 17 00:00:00 2001 From: Rafael Gonzaga Date: Wed, 26 Jul 2023 19:37:56 -0300 Subject: [PATCH] benchmark: add pm startup benchmark PR-URL: https://github.com/nodejs/node/pull/48905 Reviewed-By: Paolo Insogna Reviewed-By: Rich Trott --- benchmark/permission/permission-startup.js | 59 ++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 benchmark/permission/permission-startup.js diff --git a/benchmark/permission/permission-startup.js b/benchmark/permission/permission-startup.js new file mode 100644 index 00000000000000..c800706299bb23 --- /dev/null +++ b/benchmark/permission/permission-startup.js @@ -0,0 +1,59 @@ +'use strict'; + +const common = require('../common.js'); +const path = require('path'); +const { spawnSync } = require('child_process'); + +function mockFiles(n, prefix = '/tmp') { + const files = []; + for (let i = 0; i < n; ++i) { + files.push(prefix + '/file' + i + '.js'); + } + + return files; +} + +const bench = common.createBenchmark(main, { + script: [ + 'test/fixtures/semicolon', + ], + prefixPath: ['/tmp'], + nFiles: [10, 100, 1000], + count: [30], +}); + +function spawnProcess(script, bench, state) { + const cmd = process.execPath || process.argv[0]; + while (state.finished < state.count) { + const child = spawnSync(cmd, script); + if (child.status !== 0) { + console.log('---- STDOUT ----'); + console.log(child.stdout.toString()); + console.log('---- STDERR ----'); + console.log(child.stderr.toString()); + throw new Error(`Child process stopped with exit code ${child.status}`); + } + state.finished++; + if (state.finished === 0) { + // Finished warmup. + bench.start(); + } + + if (state.finished === state.count) { + bench.end(state.count); + } + } +} + +function main({ count, script, nFiles, prefixPath }) { + script = path.resolve(__dirname, '../../', `${script}.js`); + const files = mockFiles(nFiles, prefixPath).join(','); + const optionsWithScript = [ + '--experimental-permission', + `--allow-fs-read=${files},${script}`, + script, + ]; + const warmup = 3; + const state = { count, finished: -warmup }; + spawnProcess(optionsWithScript, bench, state); +}