From 7e98c6bf5b140da8af8eb2438abe12115e1c4a84 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 54b7481afaa817..55ebcc96ba21ed 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 8c2f744fbf6e9f..e2a0eb13126ed5 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 edb61a0601c70b..e23a4a1753b00d 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 + });