diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2019-04-18 12:33:58 +0800 |
---|---|---|
committer | Joyee Cheung <joyeec9h3@gmail.com> | 2019-04-28 14:46:27 +0800 |
commit | c5817abff5033da7c09302256a331e64473422a8 (patch) | |
tree | 5730e3a8c47aac45abda736b7a06ee476ad199b3 /benchmark | |
parent | 2b24ffae2240163a74ae11e49ee198e98abb07dc (diff) | |
download | node-new-c5817abff5033da7c09302256a331e64473422a8.tar.gz |
benchmark: add benchmark for node -p
PR-URL: https://github.com/nodejs/node/pull/27320
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'benchmark')
-rw-r--r-- | benchmark/misc/print.js | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/benchmark/misc/print.js b/benchmark/misc/print.js new file mode 100644 index 0000000000..e048d22f7b --- /dev/null +++ b/benchmark/misc/print.js @@ -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); +} |