From f4955fde603c2b876f4115b4b412f1d8d095d3e9 Mon Sep 17 00:00:00 2001 From: Refael Ackermann Date: Thu, 14 Feb 2019 19:50:49 -0500 Subject: [PATCH] benchmark,test: refactoring PR-URL: https://github.com/nodejs/node/pull/26119 Refs: https://github.com/nodejs/node/pull/26101 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell --- benchmark/http/create-clientrequest.js | 61 +++++++++++++++----------- benchmark/http/headers.js | 13 +++--- benchmark/http/incoming_headers.js | 6 +-- test/benchmark/test-benchmark-http.js | 8 ++-- 4 files changed, 47 insertions(+), 41 deletions(-) diff --git a/benchmark/http/create-clientrequest.js b/benchmark/http/create-clientrequest.js index 76468a49aa0835..24d6e020c63bc0 100644 --- a/benchmark/http/create-clientrequest.js +++ b/benchmark/http/create-clientrequest.js @@ -1,7 +1,9 @@ 'use strict'; const common = require('../common.js'); -const ClientRequest = require('http').ClientRequest; +const { ClientRequest } = require('http'); +const assert = require('assert'); + const types = Object.keys(common.urls) .filter((i) => common.urls[i] .startsWith('http://')); @@ -15,36 +17,43 @@ const bench = common.createBenchmark(main, { function noop() {} function main({ url: type, arg, e }) { - e = +e; + e = Number(e); const data = common.bakeUrlData(type, e, false, false) .filter((i) => i.startsWith('http://')); const len = data.length; - var result; - var i; - if (arg === 'options') { - const options = data.map((i) => ({ - path: new URL(i).path, createConnection: noop - })); - bench.start(); - for (i = 0; i < len; i++) { - result = new ClientRequest(options[i]); + let result; + switch (arg) { + case 'options': { + const options = data.map((i) => ({ + path: new URL(i).path, createConnection: noop + })); + bench.start(); + for (let i = 0; i < len; i++) { + result = new ClientRequest(options[i]); + } + bench.end(len); + break; + } + case 'URL': { + const options = data.map((i) => new URL(i)); + bench.start(); + for (let i = 0; i < len; i++) { + result = new ClientRequest(options[i], { createConnection: noop }); + } + bench.end(len); + break; } - bench.end(len); - } else if (arg === 'URL') { - const options = data.map((i) => new URL(i)); - bench.start(); - for (i = 0; i < len; i++) { - result = new ClientRequest(options[i], { createConnection: noop }); + case 'string': { + bench.start(); + for (let i = 0; i < len; i++) { + result = new ClientRequest(data[i], { createConnection: noop }); + } + bench.end(len); + break; } - bench.end(len); - } else if (arg === 'string') { - bench.start(); - for (i = 0; i < len; i++) { - result = new ClientRequest(data[i], { createConnection: noop }); + default: { + throw new Error(`Unknown arg type ${arg}`); } - bench.end(len); - } else { - throw new Error(`Unknown arg type ${arg}`); } - require('assert').ok(result); + assert.ok(result); } diff --git a/benchmark/http/headers.js b/benchmark/http/headers.js index 60d800c20cf04f..8f611ae4743c93 100644 --- a/benchmark/http/headers.js +++ b/benchmark/http/headers.js @@ -4,21 +4,20 @@ const common = require('../common.js'); const http = require('http'); const bench = common.createBenchmark(main, { - duplicates: [1, 100], n: [10, 1000], + len: [1, 100], }); -function main({ duplicates, n }) { +function main({ len, n }) { const headers = { 'Connection': 'keep-alive', 'Transfer-Encoding': 'chunked', }; - for (var i = 0; i < n / duplicates; i++) { - headers[`foo${i}`] = []; - for (var j = 0; j < duplicates; j++) { - headers[`foo${i}`].push(`some header value ${i}`); - } + const Is = [ ...Array(n / len).keys() ]; + const Js = [ ...Array(len).keys() ]; + for (const i of Is) { + headers[`foo${i}`] = Js.map(() => `some header value ${i}`); } const server = http.createServer((req, res) => { diff --git a/benchmark/http/incoming_headers.js b/benchmark/http/incoming_headers.js index a1ab57e23876bf..7d9955766c7882 100644 --- a/benchmark/http/incoming_headers.js +++ b/benchmark/http/incoming_headers.js @@ -5,10 +5,10 @@ const http = require('http'); const bench = common.createBenchmark(main, { // unicode confuses ab on os x. c: [50, 500], - headerDuplicates: [0, 5, 20] + n: [0, 5, 20] }); -function main({ c, headerDuplicates }) { +function main({ c, n }) { const server = http.createServer((req, res) => { res.end(); }); @@ -21,7 +21,7 @@ function main({ c, headerDuplicates }) { 'Date': new Date().toString(), 'Cache-Control': 'no-cache' }; - for (let i = 0; i < headerDuplicates; i++) { + for (let i = 0; i < n; i++) { headers[`foo${i}`] = `some header value ${i}`; } bench.http({ diff --git a/test/benchmark/test-benchmark-http.js b/test/benchmark/test-benchmark-http.js index d0c9ef175530bc..ed8423ba4c10cd 100644 --- a/test/benchmark/test-benchmark-http.js +++ b/test/benchmark/test-benchmark-http.js @@ -14,14 +14,12 @@ const runBenchmark = require('../common/benchmark'); runBenchmark('http', [ 'benchmarker=test-double-http', - 'c=1', - 'e=0', - 'url=long', 'arg=string', + 'c=1', 'chunkedEnc=true', 'chunks=0', 'dur=0.1', - 'duplicates=1', + 'e=0', 'input=keep-alive', 'key=""', 'len=1', @@ -29,8 +27,8 @@ runBenchmark('http', 'n=1', 'res=normal', 'type=asc', + 'url=long', 'value=X-Powered-By', - 'headerDuplicates=1', ], { NODEJS_BENCHMARK_ZERO_ALLOWED: 1,