Skip to content

Commit

Permalink
benchmark: rename different parameters
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
- add options in the test for all url benchmarks
  • Loading branch information
daynin committed Mar 4, 2018
1 parent 57c86b6 commit 3cd0d7f
Show file tree
Hide file tree
Showing 6 changed files with 50 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}`);
}
}
10 changes: 5 additions & 5 deletions benchmark/url/url-searchparams-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +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);
if (!params[method])
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)
params[method](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
17 changes: 16 additions & 1 deletion test/parallel/test-benchmark-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,19 @@ require('../common');

const runBenchmark = require('../common/benchmark');

runBenchmark('url', ['n=1'], { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });
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 3cd0d7f

Please sign in to comment.