From c7b6a22f1ac0cec67d7b884a2f7e5cbc566d8c86 Mon Sep 17 00:00:00 2001 From: Refael Ackermann Date: Thu, 14 Feb 2019 19:50:49 -0500 Subject: 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 ++-- 3 files changed, 44 insertions(+), 36 deletions(-) (limited to 'benchmark/http') diff --git a/benchmark/http/create-clientrequest.js b/benchmark/http/create-clientrequest.js index 76468a49aa..24d6e020c6 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 60d800c20c..8f611ae474 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 a1ab57e238..7d9955766c 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({ -- cgit v1.2.3