Skip to content

Commit 0c206e0

Browse files
joyeecheungMylesBorins
authored andcommitted
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: #24220 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Richard Lau <[email protected]>
1 parent 8c107a3 commit 0c206e0

File tree

3 files changed

+79
-24
lines changed

3 files changed

+79
-24
lines changed
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
const list = require('internal/bootstrap/cache');
4+
const {
5+
isMainThread
6+
} = require('worker_threads');
7+
8+
for (const key of list.cachableBuiltins) {
9+
if (!isMainThread && key === 'trace_events') {
10+
continue;
11+
}
12+
require(key);
13+
}

benchmark/misc/startup.js

+64-24
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,76 @@
11
'use strict';
22
const common = require('../common.js');
3-
const spawn = require('child_process').spawn;
3+
const { spawn } = require('child_process');
44
const path = require('path');
5-
const emptyJsFile = path.resolve(__dirname, '../../test/fixtures/semicolon.js');
65

7-
const bench = common.createBenchmark(startNode, {
8-
dur: [1]
6+
let Worker; // Lazy loaded in main
7+
8+
const bench = common.createBenchmark(main, {
9+
dur: [1],
10+
script: ['benchmark/fixtures/require-cachable', 'test/fixtures/semicolon'],
11+
mode: ['process', 'worker']
12+
}, {
13+
flags: ['--expose-internals', '--experimental-worker'] // for workers
914
});
1015

11-
function startNode({ dur }) {
12-
var go = true;
13-
var starts = 0;
16+
function spawnProcess(script) {
17+
const cmd = process.execPath || process.argv[0];
18+
const argv = ['--expose-internals', script];
19+
return spawn(cmd, argv);
20+
}
21+
22+
function spawnWorker(script) {
23+
return new Worker(script, { stderr: true, stdout: true });
24+
}
25+
26+
function start(state, script, bench, getNode) {
27+
const node = getNode(script);
28+
let stdout = '';
29+
let stderr = '';
30+
31+
node.stdout.on('data', (data) => {
32+
stdout += data;
33+
});
34+
35+
node.stderr.on('data', (data) => {
36+
stderr += data;
37+
});
38+
39+
node.on('exit', (code) => {
40+
if (code !== 0) {
41+
console.error('------ stdout ------');
42+
console.error(stdout);
43+
console.error('------ stderr ------');
44+
console.error(stderr);
45+
throw new Error(`Error during node startup, exit code ${code}`);
46+
}
47+
state.throughput++;
48+
49+
if (state.go) {
50+
start(state, script, bench, getNode);
51+
} else {
52+
bench.end(state.throughput);
53+
}
54+
});
55+
}
56+
57+
function main({ dur, script, mode }) {
58+
const state = {
59+
go: true,
60+
throughput: 0
61+
};
1462

1563
setTimeout(function() {
16-
go = false;
64+
state.go = false;
1765
}, dur * 1000);
1866

19-
bench.start();
20-
start();
21-
22-
function start() {
23-
const node = spawn(process.execPath || process.argv[0], [emptyJsFile]);
24-
node.on('exit', function(exitCode) {
25-
if (exitCode !== 0) {
26-
throw new Error('Error during node startup');
27-
}
28-
starts++;
29-
30-
if (go)
31-
start();
32-
else
33-
bench.end(starts);
34-
});
67+
script = path.resolve(__dirname, '../../', `${script}.js`);
68+
if (mode === 'worker') {
69+
Worker = require('worker_threads').Worker;
70+
bench.start();
71+
start(state, script, bench, spawnWorker);
72+
} else {
73+
bench.start();
74+
start(state, script, bench, spawnProcess);
3575
}
3676
}

test/parallel/test-benchmark-misc.js

+2
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ runBenchmark('misc', [
1111
'n=1',
1212
'type=',
1313
'val=magyarország.icom.museum',
14+
'script=test/fixtures/semicolon',
15+
'mode=worker'
1416
], { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });

0 commit comments

Comments
 (0)