From 98d1110721faf0a5c62f5402e863f2c203783677 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Fri, 26 Jan 2018 00:47:18 +0800 Subject: [PATCH] benchmark: implement duration in http test double PR-URL: https://github.com/nodejs/node/pull/18380 Reviewed-By: James M Snell Reviewed-By: Colin Ihrig --- benchmark/_http-benchmarkers.js | 9 ++++++--- benchmark/_test-double-benchmarker.js | 28 +++++++++++++++++++++++--- test/sequential/test-benchmark-http.js | 5 ++++- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/benchmark/_http-benchmarkers.js b/benchmark/_http-benchmarkers.js index 54b7481afa..55ebcc96ba 100644 --- a/benchmark/_http-benchmarkers.js +++ b/benchmark/_http-benchmarkers.js @@ -89,11 +89,14 @@ class TestDoubleBenchmarker { } create(options) { + const env = Object.assign({ + duration: options.duration, + test_url: `http://127.0.0.1:${options.port}${options.path}`, + }, process.env); + const child = child_process.fork(this.executable, { silent: true, - env: Object.assign({}, process.env, { - test_url: `http://127.0.0.1:${options.port}${options.path}` - }) + env }); return child; } diff --git a/benchmark/_test-double-benchmarker.js b/benchmark/_test-double-benchmarker.js index 8c2f744fbf..e2a0eb1312 100644 --- a/benchmark/_test-double-benchmarker.js +++ b/benchmark/_test-double-benchmarker.js @@ -2,6 +2,28 @@ const http = require('http'); -http.get(process.env.test_url, function() { - console.log(JSON.stringify({ throughput: 1 })); -}); +const duration = process.env.duration || 0; +const url = process.env.test_url; + +const start = process.hrtime(); +let throughput = 0; + +function request(res) { + res.on('data', () => {}); + res.on('error', () => {}); + res.on('end', () => { + throughput++; + const diff = process.hrtime(start); + if (duration > 0 && diff[0] < duration) { + run(); + } else { + console.log(JSON.stringify({ throughput })); + } + }); +} + +function run() { + http.get(url, request); +} + +run(); diff --git a/test/sequential/test-benchmark-http.js b/test/sequential/test-benchmark-http.js index edb61a0601..e23a4a1753 100644 --- a/test/sequential/test-benchmark-http.js +++ b/test/sequential/test-benchmark-http.js @@ -25,4 +25,7 @@ runBenchmark('http', 'res=normal', 'type=asc' ], - { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 }); + { + NODEJS_BENCHMARK_ZERO_ALLOWED: 1, + duration: 0 + });