diff --git a/benchmark/fixtures/require-cachable.js b/benchmark/fixtures/require-cachable.js new file mode 100644 index 00000000000000..f651728dc78f35 --- /dev/null +++ b/benchmark/fixtures/require-cachable.js @@ -0,0 +1,13 @@ +'use strict'; + +const list = require('internal/bootstrap/cache'); +const { + isMainThread +} = require('worker_threads'); + +for (const key of list.cachableBuiltins) { + if (!isMainThread && key === 'trace_events') { + continue; + } + require(key); +} diff --git a/benchmark/misc/startup.js b/benchmark/misc/startup.js index 703146f081b3c6..1350cd291e2b18 100644 --- a/benchmark/misc/startup.js +++ b/benchmark/misc/startup.js @@ -1,36 +1,76 @@ 'use strict'; const common = require('../common.js'); -const spawn = require('child_process').spawn; +const { spawn } = require('child_process'); const path = require('path'); -const emptyJsFile = path.resolve(__dirname, '../../test/fixtures/semicolon.js'); -const bench = common.createBenchmark(startNode, { - dur: [1] +let Worker; // Lazy loaded in main + +const bench = common.createBenchmark(main, { + dur: [1], + script: ['benchmark/fixtures/require-cachable', 'test/fixtures/semicolon'], + mode: ['process', 'worker'] +}, { + flags: ['--expose-internals', '--experimental-worker'] // for workers }); -function startNode({ dur }) { - var go = true; - var starts = 0; +function spawnProcess(script) { + const cmd = process.execPath || process.argv[0]; + const argv = ['--expose-internals', script]; + return spawn(cmd, argv); +} + +function spawnWorker(script) { + return new Worker(script, { stderr: true, stdout: true }); +} + +function start(state, script, bench, getNode) { + const node = getNode(script); + let stdout = ''; + let stderr = ''; + + node.stdout.on('data', (data) => { + stdout += data; + }); + + node.stderr.on('data', (data) => { + stderr += data; + }); + + node.on('exit', (code) => { + if (code !== 0) { + console.error('------ stdout ------'); + console.error(stdout); + console.error('------ stderr ------'); + console.error(stderr); + throw new Error(`Error during node startup, exit code ${code}`); + } + state.throughput++; + + if (state.go) { + start(state, script, bench, getNode); + } else { + bench.end(state.throughput); + } + }); +} + +function main({ dur, script, mode }) { + const state = { + go: true, + throughput: 0 + }; setTimeout(function() { - go = false; + state.go = false; }, dur * 1000); - bench.start(); - start(); - - function start() { - const node = spawn(process.execPath || process.argv[0], [emptyJsFile]); - node.on('exit', function(exitCode) { - if (exitCode !== 0) { - throw new Error('Error during node startup'); - } - starts++; - - if (go) - start(); - else - bench.end(starts); - }); + script = path.resolve(__dirname, '../../', `${script}.js`); + if (mode === 'worker') { + Worker = require('worker_threads').Worker; + bench.start(); + start(state, script, bench, spawnWorker); + } else { + bench.start(); + start(state, script, bench, spawnProcess); } } diff --git a/test/parallel/test-benchmark-misc.js b/test/parallel/test-benchmark-misc.js index fc7e340a80d0bb..b88415280833bc 100644 --- a/test/parallel/test-benchmark-misc.js +++ b/test/parallel/test-benchmark-misc.js @@ -11,4 +11,6 @@ runBenchmark('misc', [ 'n=1', 'type=', 'val=magyarország.icom.museum', + 'script=test/fixtures/semicolon', + 'mode=worker' ], { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });