Skip to content

Commit cfc7bdd

Browse files
joyeecheungtargos
authored andcommitted
benchmark: add benchmark for node -p
PR-URL: #27320 Reviewed-By: James M Snell <[email protected]>
1 parent c2a03d5 commit cfc7bdd

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

benchmark/misc/print.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const { spawn } = require('child_process');
4+
5+
const bench = common.createBenchmark(main, {
6+
dur: [1],
7+
code: ['1', '"string"', 'process.versions', 'process']
8+
});
9+
10+
function spawnProcess(code) {
11+
const cmd = process.execPath || process.argv[0];
12+
const argv = ['-p', code];
13+
return spawn(cmd, argv);
14+
}
15+
16+
function start(state, code, bench, getNode) {
17+
const node = getNode(code);
18+
let stdout = '';
19+
let stderr = '';
20+
21+
node.stdout.on('data', (data) => {
22+
stdout += data;
23+
});
24+
25+
node.stderr.on('data', (data) => {
26+
stderr += data;
27+
});
28+
29+
node.on('exit', (code) => {
30+
if (code !== 0) {
31+
console.error('------ stdout ------');
32+
console.error(stdout);
33+
console.error('------ stderr ------');
34+
console.error(stderr);
35+
throw new Error(`Error during node startup, exit code ${code}`);
36+
}
37+
state.throughput++;
38+
39+
if (state.go) {
40+
start(state, code, bench, getNode);
41+
} else {
42+
bench.end(state.throughput);
43+
}
44+
});
45+
}
46+
47+
function main({ dur, code }) {
48+
const state = {
49+
go: true,
50+
throughput: 0
51+
};
52+
53+
setTimeout(() => {
54+
state.go = false;
55+
}, dur * 1000);
56+
57+
bench.start();
58+
start(state, code, bench, spawnProcess);
59+
}

test/benchmark/test-benchmark-misc.js

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ runBenchmark('misc', [
1010
'method=',
1111
'n=1',
1212
'type=',
13+
'code=1',
1314
'val=magyarország.icom.museum',
1415
'script=test/fixtures/semicolon',
1516
'mode=worker'

0 commit comments

Comments
 (0)