Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

benchmark: support more options in startup benchmark #24220

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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.
joyeecheung committed Nov 7, 2018
commit ab62bc95d9103a8beebd0d3bd6fea81ed1771cca
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 spwanProcess(script) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function spwanProcess(script) {
function spawnProcess(script) {

const cmd = process.execPath || process.argv[0];
const argv = ['--expose-internals', script];
return spawn(cmd, argv);
}

function spwanWorker(script) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function spwanWorker(script) {
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, spwanWorker);
} else {
bench.start();
start(state, script, bench, spwanProcess);
}
}
2 changes: 2 additions & 0 deletions test/parallel/test-benchmark-misc.js
Original file line number Diff line number Diff line change
@@ -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 });