From cfc7bdd3036953e27c366d2d2ed97ea382a70369 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 18 Apr 2019 12:33:58 +0800 Subject: [PATCH] benchmark: add benchmark for node -p PR-URL: https://github.com/nodejs/node/pull/27320 Reviewed-By: James M Snell --- benchmark/misc/print.js | 59 +++++++++++++++++++++++++++ test/benchmark/test-benchmark-misc.js | 1 + 2 files changed, 60 insertions(+) create mode 100644 benchmark/misc/print.js diff --git a/benchmark/misc/print.js b/benchmark/misc/print.js new file mode 100644 index 00000000000000..e048d22f7b76ab --- /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); +} diff --git a/test/benchmark/test-benchmark-misc.js b/test/benchmark/test-benchmark-misc.js index b88415280833bc..13bd41e54016f5 100644 --- a/test/benchmark/test-benchmark-misc.js +++ b/test/benchmark/test-benchmark-misc.js @@ -10,6 +10,7 @@ runBenchmark('misc', [ 'method=', 'n=1', 'type=', + 'code=1', 'val=magyarország.icom.museum', 'script=test/fixtures/semicolon', 'mode=worker'