Skip to content

Commit

Permalink
benchmark: support more options in startup benchmark
Browse files Browse the repository at this point in the history
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: nodejs#24220
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
  • Loading branch information
joyeecheung authored and Trott committed Nov 9, 2018
1 parent 5e1bf05 commit 1698fc9
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 24 deletions.
13 changes: 13 additions & 0 deletions benchmark/fixtures/require-cachable.js
Original file line number Diff line number Diff line change
@@ -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);
}
88 changes: 64 additions & 24 deletions benchmark/misc/startup.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 2 additions & 0 deletions test/parallel/test-benchmark-misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

0 comments on commit 1698fc9

Please sign in to comment.