0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-29 23:16:30 +01:00
nodejs/benchmark/misc/startup.js
Joyee Cheung 1698fc96af 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>
2018-11-09 15:55:19 -08:00

77 lines
1.7 KiB
JavaScript

'use strict';
const common = require('../common.js');
const { spawn } = require('child_process');
const path = require('path');
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 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() {
state.go = false;
}, dur * 1000);
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);
}
}