From 77c81eee46cb647f89dd41fcbb15fa1535290c5e Mon Sep 17 00:00:00 2001 From: Sergey Golovin Date: Fri, 2 Mar 2018 16:23:16 +0300 Subject: [PATCH] benchmark: fix benchmark for url Rename different parameters with the same names to make it possible to run tests for url benchmarks. PR-URL: https://github.com/nodejs/node/pull/19084 Reviewed-By: Ruben Bridgewater Reviewed-By: Khaidi Chu Reviewed-By: Shingo Inoue Reviewed-By: Anatoli Papirovski Reviewed-By: James M Snell --- ...legacy-vs-whatwg-url-searchparams-parse.js | 22 +++++++++---------- ...cy-vs-whatwg-url-searchparams-serialize.js | 20 ++++++++--------- benchmark/url/url-searchparams-iteration.js | 8 +++---- benchmark/url/url-searchparams-read.js | 11 +++++----- benchmark/url/whatwg-url-idna.js | 8 +++---- test/parallel/test-benchmark-url.js | 22 +++++++++++++++++++ 6 files changed, 56 insertions(+), 35 deletions(-) create mode 100644 test/parallel/test-benchmark-url.js diff --git a/benchmark/url/legacy-vs-whatwg-url-searchparams-parse.js b/benchmark/url/legacy-vs-whatwg-url-searchparams-parse.js index e915ceb54f917f..6b054d0b2a1f1a 100644 --- a/benchmark/url/legacy-vs-whatwg-url-searchparams-parse.js +++ b/benchmark/url/legacy-vs-whatwg-url-searchparams-parse.js @@ -2,10 +2,10 @@ const common = require('../common.js'); const { URLSearchParams } = require('url'); const querystring = require('querystring'); -const inputs = require('../fixtures/url-inputs.js').searchParams; +const searchParams = require('../fixtures/url-inputs.js').searchParams; const bench = common.createBenchmark(main, { - type: Object.keys(inputs), + searchParam: Object.keys(searchParams), method: ['legacy', 'whatwg'], n: [1e6] }); @@ -19,27 +19,27 @@ function useLegacy(n, input) { bench.end(n); } -function useWHATWG(n, input) { - new URLSearchParams(input); +function useWHATWG(n, param) { + new URLSearchParams(param); bench.start(); for (var i = 0; i < n; i += 1) { - new URLSearchParams(input); + new URLSearchParams(param); } bench.end(n); } -function main({ type, n, method }) { - const input = inputs[type]; - if (!input) { - throw new Error(`Unknown input type "${type}"`); +function main({ searchParam, n, method }) { + const param = searchParams[searchParam]; + if (!param) { + throw new Error(`Unknown search parameter type "${searchParam}"`); } switch (method) { case 'legacy': - useLegacy(n, input); + useLegacy(n, param); break; case 'whatwg': - useWHATWG(n, input); + useWHATWG(n, param); break; default: throw new Error(`Unknown method ${method}`); diff --git a/benchmark/url/legacy-vs-whatwg-url-searchparams-serialize.js b/benchmark/url/legacy-vs-whatwg-url-searchparams-serialize.js index 8fe3e546f0780d..54fdd956544886 100644 --- a/benchmark/url/legacy-vs-whatwg-url-searchparams-serialize.js +++ b/benchmark/url/legacy-vs-whatwg-url-searchparams-serialize.js @@ -2,10 +2,10 @@ const common = require('../common.js'); const { URLSearchParams } = require('url'); const querystring = require('querystring'); -const inputs = require('../fixtures/url-inputs.js').searchParams; +const searchParams = require('../fixtures/url-inputs.js').searchParams; const bench = common.createBenchmark(main, { - type: Object.keys(inputs), + searchParam: Object.keys(searchParams), method: ['legacy', 'whatwg'], n: [1e6] }); @@ -20,8 +20,8 @@ function useLegacy(n, input, prop) { bench.end(n); } -function useWHATWG(n, input, prop) { - const obj = new URLSearchParams(input); +function useWHATWG(n, param, prop) { + const obj = new URLSearchParams(param); obj.toString(); bench.start(); for (var i = 0; i < n; i += 1) { @@ -30,18 +30,18 @@ function useWHATWG(n, input, prop) { bench.end(n); } -function main({ type, n, method }) { - const input = inputs[type]; - if (!input) { - throw new Error(`Unknown input type "${type}"`); +function main({ searchParam, n, method }) { + const param = searchParams[searchParam]; + if (!param) { + throw new Error(`Unknown search parameter type "${searchParam}"`); } switch (method) { case 'legacy': - useLegacy(n, input); + useLegacy(n, param); break; case 'whatwg': - useWHATWG(n, input); + useWHATWG(n, param); break; default: throw new Error(`Unknown method ${method}`); diff --git a/benchmark/url/url-searchparams-iteration.js b/benchmark/url/url-searchparams-iteration.js index cae2ef5df61956..244c95502bfffb 100644 --- a/benchmark/url/url-searchparams-iteration.js +++ b/benchmark/url/url-searchparams-iteration.js @@ -4,7 +4,7 @@ const assert = require('assert'); const { URLSearchParams } = require('url'); const bench = common.createBenchmark(main, { - method: ['forEach', 'iterator'], + loopMethod: ['forEach', 'iterator'], n: [1e6] }); @@ -44,8 +44,8 @@ function iterator(n) { assert.strictEqual(noDead[1], '3rd'); } -function main({ method, n }) { - switch (method) { +function main({ loopMethod, n }) { + switch (loopMethod) { case 'forEach': forEach(n); break; @@ -53,6 +53,6 @@ function main({ method, n }) { iterator(n); break; default: - throw new Error(`Unknown method ${method}`); + throw new Error(`Unknown method ${loopMethod}`); } } diff --git a/benchmark/url/url-searchparams-read.js b/benchmark/url/url-searchparams-read.js index 0cf66dabbc36dc..2eb0a4f21350dc 100644 --- a/benchmark/url/url-searchparams-read.js +++ b/benchmark/url/url-searchparams-read.js @@ -3,21 +3,20 @@ const common = require('../common.js'); const { URLSearchParams } = require('url'); const bench = common.createBenchmark(main, { - method: ['get', 'getAll', 'has'], + accessMethod: ['get', 'getAll', 'has'], param: ['one', 'two', 'three', 'nonexistent'], n: [2e7] }); const str = 'one=single&two=first&three=first&two=2nd&three=2nd&three=3rd'; -function main({ method, param, n }) { +function main({ accessMethod, param, n }) { const params = new URLSearchParams(str); - const fn = params[method]; - if (!fn) - throw new Error(`Unknown method ${method}`); + if (!params[accessMethod]) + throw new Error(`Unknown method ${accessMethod}`); bench.start(); for (var i = 0; i < n; i += 1) - fn(param); + params[accessMethod](param); bench.end(n); } diff --git a/benchmark/url/whatwg-url-idna.js b/benchmark/url/whatwg-url-idna.js index c1e3d4a0b85cec..b67a08da7a88e0 100644 --- a/benchmark/url/whatwg-url-idna.js +++ b/benchmark/url/whatwg-url-idna.js @@ -2,7 +2,7 @@ const common = require('../common.js'); const { domainToASCII, domainToUnicode } = require('url'); -const inputs = { +const domains = { empty: { ascii: '', unicode: '' @@ -26,13 +26,13 @@ const inputs = { }; const bench = common.createBenchmark(main, { - input: Object.keys(inputs), + domain: Object.keys(domains), to: ['ascii', 'unicode'], n: [5e6] }); -function main({ n, to, input }) { - const value = inputs[input][to]; +function main({ n, to, domain }) { + const value = domains[domain][to]; const method = to === 'ascii' ? domainToASCII : domainToUnicode; bench.start(); diff --git a/test/parallel/test-benchmark-url.js b/test/parallel/test-benchmark-url.js new file mode 100644 index 00000000000000..e4bcf5017004f4 --- /dev/null +++ b/test/parallel/test-benchmark-url.js @@ -0,0 +1,22 @@ +'use strict'; + +require('../common'); + +const runBenchmark = require('../common/benchmark'); + +runBenchmark('url', + [ + 'method=legacy', + 'loopMethod=forEach', + 'accessMethod=get', + 'type=short', + 'searchParam=noencode', + 'href=short', + 'input=short', + 'domain=empty', + 'path=up', + 'to=ascii', + 'prop=href', + 'n=1', + ], + { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });