benchmark: support more options in startup benchmark

1. Add options to benchmark the startup performance of a node
  "instance" after running a script. By default there are two options:
  `test/fixtures/semicolon` which is basically an empty file,
  and `benchmark/fixtures/require-cachable` which require all
  the cachable modules before exiting. This allows us to measure
  the overhead of bootstrap in more scenarios.
2. Add options to benchmark the overhead of spinning
  node through a process and through a worker.

PR-URL: https://github.com/nodejs/node/pull/24220
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
This commit is contained in:
Joyee Cheung 2018-11-07 04:28:50 +08:00 committed by Rich Trott
parent 5e1bf058f4
commit 1698fc96af
3 changed files with 79 additions and 24 deletions

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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 });