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: rewrite the startup performance benchmark #45746

Merged
merged 5 commits into from Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions benchmark/common.js
Expand Up @@ -38,7 +38,7 @@ class Benchmark {
this.config = this.queue[0];

process.nextTick(() => {
if (Object.hasOwn(process.env, 'NODE_RUN_BENCHMARK_FN')) {
if (process.env.NODE_RUN_BENCHMARK_FN !== undefined) {
fn(this.config);
} else {
// _run will use fork() to create a new process for each configuration
Expand Down Expand Up @@ -91,7 +91,7 @@ class Benchmark {
process.exit(1);
}
const [, key, value] = match;
if (Object.hasOwn(configs, key)) {
if (configs[key] !== undefined) {
if (!cliOptions[key])
cliOptions[key] = [];
cliOptions[key].push(
Expand Down Expand Up @@ -290,10 +290,10 @@ function sendResult(data) {
if (process.send) {
// If forked, report by process send
process.send(data, () => {
if (Object.hasOwn(process.env, 'NODE_RUN_BENCHMARK_FN')) {
if (process.env.NODE_RUN_BENCHMARK_FN !== undefined) {
// If, for any reason, the process is unable to self close within
// a second after completing, forcefully close it.
setTimeout(() => {
require('timers').setTimeout(() => {
process.exit(0);
}, 5000).unref();
}
Expand Down
10 changes: 0 additions & 10 deletions benchmark/fixtures/require-cachable.js

This file was deleted.

89 changes: 39 additions & 50 deletions benchmark/misc/startup.js
@@ -1,80 +1,69 @@
'use strict';
const common = require('../common.js');
const { spawn } = require('child_process');
const { spawnSync } = require('child_process');
const path = require('path');

let Worker; // Lazy loaded in main

const bench = common.createBenchmark(main, {
dur: [1],
script: [
'benchmark/fixtures/require-builtins',
'benchmark/fixtures/require-cachable',
'test/fixtures/semicolon',
],
mode: ['process', 'worker']
}, {
flags: ['--expose-internals']
mode: ['process', 'worker'],
count: [30],
});

function spawnProcess(script) {
function spawnProcess(script, bench, state) {
const cmd = process.execPath || process.argv[0];
const argv = ['--expose-internals', script];
return spawn(cmd, argv);
}
while (state.finished < state.count) {
const child = spawnSync(cmd, [script]);
if (child.status !== 0) {
console.log('---- STDOUT ----');
console.log(child.stdout.toString());
console.log('---- STDERR ----');
console.log(child.stderr.toString());
throw new Error(`Child process stopped with exit code ${child.status}`);
}
state.finished++;
if (state.finished === 0) {
// Finished warmup.
bench.start();
}

function spawnWorker(script) {
return new Worker(script, { stderr: true, stdout: true });
if (state.finished === state.count) {
bench.end(state.count);
}
}
}

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) => {
function spawnWorker(script, bench, state) {
const child = new Worker(script);
child.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}`);
throw new Error(`Worker stopped with exit code ${code}`);
}
state.throughput++;

if (state.go) {
start(state, script, bench, getNode);
state.finished++;
if (state.finished === 0) {
// Finished warmup.
bench.start();
}
if (state.finished < state.count) {
spawnProcess(script, bench, state);
} else {
bench.end(state.throughput);
bench.end(state.count);
}
});
}

function main({ dur, script, mode }) {
const state = {
go: true,
throughput: 0
};

setTimeout(() => {
state.go = false;
}, dur * 1000);

function main({ count, script, mode }) {
script = path.resolve(__dirname, '../../', `${script}.js`);
const warmup = 3;
const state = { count, finished: -warmup };
if (mode === 'worker') {
Worker = require('worker_threads').Worker;
bench.start();
start(state, script, bench, spawnWorker);
spawnWorker(script, bench, state);
} else {
bench.start();
start(state, script, bench, spawnProcess);
spawnProcess(script, bench, state);
}
}