Skip to content

Commit

Permalink
benchmark: fix benchmark for url
Browse files Browse the repository at this point in the history
Rename different parameters with the same names to make it possible
to run tests for url benchmarks.

PR-URL: #19084
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Khaidi Chu <i@2333.moe>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
daynin authored and MylesBorins committed Mar 20, 2018
1 parent e4c320e commit 530b8a4
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 35 deletions.
22 changes: 11 additions & 11 deletions benchmark/url/legacy-vs-whatwg-url-searchparams-parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
});
Expand All @@ -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}`);
Expand Down
20 changes: 10 additions & 10 deletions benchmark/url/legacy-vs-whatwg-url-searchparams-serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
});
Expand All @@ -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) {
Expand All @@ -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}`);
Expand Down
8 changes: 4 additions & 4 deletions benchmark/url/url-searchparams-iteration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
});

Expand Down Expand Up @@ -44,15 +44,15 @@ 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;
case 'iterator':
iterator(n);
break;
default:
throw new Error(`Unknown method ${method}`);
throw new Error(`Unknown method ${loopMethod}`);
}
}
11 changes: 5 additions & 6 deletions benchmark/url/url-searchparams-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
8 changes: 4 additions & 4 deletions benchmark/url/whatwg-url-idna.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const common = require('../common.js');
const { domainToASCII, domainToUnicode } = require('url');

const inputs = {
const domains = {
empty: {
ascii: '',
unicode: ''
Expand All @@ -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();
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-benchmark-url.js
Original file line number Diff line number Diff line change
@@ -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 });

0 comments on commit 530b8a4

Please sign in to comment.