Skip to content

Commit 5e167bd

Browse files
joyeecheungdanielleadams
authored andcommittedJan 3, 2023
benchmark: make benchmarks runnable in older versions of Node.js
Also remove the require-cachable.js benchmarks because now all builtin modules are cacheable, it would be comparing oranges to apples when we try to compare the performance of loading all cacheable modules in different Node.js binaries since the set of modules are just different. Comparison of startup performance that involves loading of the long-standing, stable builtins is already covered by the require-builtins benchmark. PR-URL: #45746 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent a1831da commit 5e167bd

File tree

3 files changed

+43
-64
lines changed

3 files changed

+43
-64
lines changed
 

‎benchmark/common.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Benchmark {
3838
this.config = this.queue[0];
3939

4040
process.nextTick(() => {
41-
if (Object.hasOwn(process.env, 'NODE_RUN_BENCHMARK_FN')) {
41+
if (process.env.NODE_RUN_BENCHMARK_FN !== undefined) {
4242
fn(this.config);
4343
} else {
4444
// _run will use fork() to create a new process for each configuration
@@ -91,7 +91,7 @@ class Benchmark {
9191
process.exit(1);
9292
}
9393
const [, key, value] = match;
94-
if (Object.hasOwn(configs, key)) {
94+
if (configs[key] !== undefined) {
9595
if (!cliOptions[key])
9696
cliOptions[key] = [];
9797
cliOptions[key].push(
@@ -290,10 +290,10 @@ function sendResult(data) {
290290
if (process.send) {
291291
// If forked, report by process send
292292
process.send(data, () => {
293-
if (Object.hasOwn(process.env, 'NODE_RUN_BENCHMARK_FN')) {
293+
if (process.env.NODE_RUN_BENCHMARK_FN !== undefined) {
294294
// If, for any reason, the process is unable to self close within
295295
// a second after completing, forcefully close it.
296-
setTimeout(() => {
296+
require('timers').setTimeout(() => {
297297
process.exit(0);
298298
}, 5000).unref();
299299
}

‎benchmark/fixtures/require-cachable.js

-10
This file was deleted.

‎benchmark/misc/startup.js

+39-50
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,69 @@
11
'use strict';
22
const common = require('../common.js');
3-
const { spawn } = require('child_process');
3+
const { spawnSync } = require('child_process');
44
const path = require('path');
55

66
let Worker; // Lazy loaded in main
77

88
const bench = common.createBenchmark(main, {
9-
dur: [1],
109
script: [
1110
'benchmark/fixtures/require-builtins',
12-
'benchmark/fixtures/require-cachable',
1311
'test/fixtures/semicolon',
1412
],
15-
mode: ['process', 'worker']
16-
}, {
17-
flags: ['--expose-internals']
13+
mode: ['process', 'worker'],
14+
count: [30],
1815
});
1916

20-
function spawnProcess(script) {
17+
function spawnProcess(script, bench, state) {
2118
const cmd = process.execPath || process.argv[0];
22-
const argv = ['--expose-internals', script];
23-
return spawn(cmd, argv);
24-
}
19+
while (state.finished < state.count) {
20+
const child = spawnSync(cmd, [script]);
21+
if (child.status !== 0) {
22+
console.log('---- STDOUT ----');
23+
console.log(child.stdout.toString());
24+
console.log('---- STDERR ----');
25+
console.log(child.stderr.toString());
26+
throw new Error(`Child process stopped with exit code ${child.status}`);
27+
}
28+
state.finished++;
29+
if (state.finished === 0) {
30+
// Finished warmup.
31+
bench.start();
32+
}
2533

26-
function spawnWorker(script) {
27-
return new Worker(script, { stderr: true, stdout: true });
34+
if (state.finished === state.count) {
35+
bench.end(state.count);
36+
}
37+
}
2838
}
2939

30-
function start(state, script, bench, getNode) {
31-
const node = getNode(script);
32-
let stdout = '';
33-
let stderr = '';
34-
35-
node.stdout.on('data', (data) => {
36-
stdout += data;
37-
});
38-
39-
node.stderr.on('data', (data) => {
40-
stderr += data;
41-
});
42-
43-
node.on('exit', (code) => {
40+
function spawnWorker(script, bench, state) {
41+
const child = new Worker(script);
42+
child.on('exit', (code) => {
4443
if (code !== 0) {
45-
console.error('------ stdout ------');
46-
console.error(stdout);
47-
console.error('------ stderr ------');
48-
console.error(stderr);
49-
throw new Error(`Error during node startup, exit code ${code}`);
44+
throw new Error(`Worker stopped with exit code ${code}`);
5045
}
51-
state.throughput++;
52-
53-
if (state.go) {
54-
start(state, script, bench, getNode);
46+
state.finished++;
47+
if (state.finished === 0) {
48+
// Finished warmup.
49+
bench.start();
50+
}
51+
if (state.finished < state.count) {
52+
spawnProcess(script, bench, state);
5553
} else {
56-
bench.end(state.throughput);
54+
bench.end(state.count);
5755
}
5856
});
5957
}
6058

61-
function main({ dur, script, mode }) {
62-
const state = {
63-
go: true,
64-
throughput: 0
65-
};
66-
67-
setTimeout(() => {
68-
state.go = false;
69-
}, dur * 1000);
70-
59+
function main({ count, script, mode }) {
7160
script = path.resolve(__dirname, '../../', `${script}.js`);
61+
const warmup = 3;
62+
const state = { count, finished: -warmup };
7263
if (mode === 'worker') {
7364
Worker = require('worker_threads').Worker;
74-
bench.start();
75-
start(state, script, bench, spawnWorker);
65+
spawnWorker(script, bench, state);
7666
} else {
77-
bench.start();
78-
start(state, script, bench, spawnProcess);
67+
spawnProcess(script, bench, state);
7968
}
8069
}

0 commit comments

Comments
 (0)
Please sign in to comment.