Skip to content

Commit

Permalink
benchmark: add benchmark for node -p
Browse files Browse the repository at this point in the history
PR-URL: #27320
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
joyeecheung committed Apr 28, 2019
1 parent 2b24ffa commit c5817ab
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
59 changes: 59 additions & 0 deletions benchmark/misc/print.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'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);
}
1 change: 1 addition & 0 deletions test/benchmark/test-benchmark-misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ runBenchmark('misc', [
'method=',
'n=1',
'type=',
'code=1',
'val=magyarország.icom.museum',
'script=test/fixtures/semicolon',
'mode=worker'
Expand Down

0 comments on commit c5817ab

Please sign in to comment.