0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-24 12:10:08 +01:00
nodejs/benchmark/misc/print.js
Antoine du Hamel 806a516851
benchmark: add trailing commas in benchmark/misc
PR-URL: https://github.com/nodejs/node/pull/46474
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
2023-02-04 18:19:36 +00:00

60 lines
1.2 KiB
JavaScript

'use strict';
const common = require('../common.js');
const { spawn } = require('child_process');
const bench = common.createBenchmark(main, {
dur: [1],
code: ['1', '"string"', 'process.versions', 'process'],
});
function spawnProcess(code) {
const cmd = process.execPath || process.argv[0];
const argv = ['-p', code];
return spawn(cmd, argv);
}
function start(state, code, bench, getNode) {
const node = getNode(code);
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, code, bench, getNode);
} else {
bench.end(state.throughput);
}
});
}
function main({ dur, code }) {
const state = {
go: true,
throughput: 0,
};
setTimeout(() => {
state.go = false;
}, dur * 1000);
bench.start();
start(state, code, bench, spawnProcess);
}