Skip to content

Commit

Permalink
benchmark: pre-generate data set for URL benchmarks
Browse files Browse the repository at this point in the history
This patch:

- Introduces `common.bakeUrlData` which can be used to pre-generate
  the data set for the URL benchmarks to loop through instead of
  looping over a constant.
- Add the option to use WPT data in benchmarks for better diversity
  in the input
- Add the option to benchmark URL parsing with base URLs (whatwg only)
- Moves the data in `benchmark/fixtures/url-inputs.js` to
  `benchmark/common.js`

PR-URL: #24302
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
joyeecheung authored and rvagg committed Nov 28, 2018
1 parent 957ecbe commit f5d4db1
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 138 deletions.
92 changes: 92 additions & 0 deletions benchmark/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,95 @@ exports.binding = function(bindingName) {
return process.binding(bindingName);
}
};

const urls = {
long: 'http://nodejs.org:89/docs/latest/api/foo/bar/qua/13949281/0f28b/' +
'/5d49/b3020/url.html#test?payload1=true&payload2=false&test=1' +
'&benchmark=3&foo=38.38.011.293&bar=1234834910480&test=19299&3992&' +
'key=f5c65e1e98fe07e648249ad41e1cfdb0',
short: 'https://nodejs.org/en/blog/',
idn: 'http://你好你好.在线',
auth: 'https://user:pass@example.com/path?search=1',
file: 'file:///foo/bar/test/node.js',
ws: 'ws://localhost:9229/f46db715-70df-43ad-a359-7f9949f39868',
javascript: 'javascript:alert("node is awesome");',
percent: 'https://%E4%BD%A0/foo',
dot: 'https://example.org/./a/../b/./c'
};
exports.urls = urls;

const searchParams = {
noencode: 'foo=bar&baz=quux&xyzzy=thud',
multicharsep: 'foo=bar&&&&&&&&&&baz=quux&&&&&&&&&&xyzzy=thud',
encodefake: 'foo=%©ar&baz=%A©uux&xyzzy=%©ud',
encodemany: '%66%6F%6F=bar&%62%61%7A=quux&xyzzy=%74h%75d',
encodelast: 'foo=bar&baz=quux&xyzzy=thu%64',
multivalue: 'foo=bar&foo=baz&foo=quux&quuy=quuz',
multivaluemany: 'foo=bar&foo=baz&foo=quux&quuy=quuz&foo=abc&foo=def&' +
'foo=ghi&foo=jkl&foo=mno&foo=pqr&foo=stu&foo=vwxyz',
manypairs: 'a&b&c&d&e&f&g&h&i&j&k&l&m&n&o&p&q&r&s&t&u&v&w&x&y&z',
manyblankpairs: '&&&&&&&&&&&&&&&&&&&&&&&&',
altspaces: 'foo+bar=baz+quux&xyzzy+thud=quuy+quuz&abc=def+ghi'
};
exports.searchParams = searchParams;

function getUrlData(withBase) {
const data = require('../test/fixtures/wpt/url/resources/urltestdata.json');
const result = [];
for (const item of data) {
if (item.failure || !item.input) continue;
if (withBase) {
result.push([item.input, item.base]);
} else if (item.base !== 'about:blank') {
result.push(item.base);
}
}
return result;
}

exports.urlDataTypes = Object.keys(urls).concat(['wpt']);

/**
* Generate an array of data for URL benchmarks to use.
* The size of the resulting data set is the original data size * 2 ** `e`.
* The 'wpt' type contains about 400 data points when `withBase` is true,
* and 200 data points when `withBase` is false.
* Other types contain 200 data points with or without base.
*
* @param {string} type Type of the data, 'wpt' or a key of `urls`
* @param {number} e The repetition of the data, as exponent of 2
* @param {boolean} withBase Whether to include a base URL
* @param {boolean} asUrl Whether to return the results as URL objects
* @return {string[] | string[][] | URL[]}
*/
function bakeUrlData(type, e = 0, withBase = false, asUrl = false) {
let result = [];
if (type === 'wpt') {
result = getUrlData(withBase);
} else if (urls[type]) {
const input = urls[type];
const item = withBase ? [input, 'about:blank'] : input;
// Roughly the size of WPT URL test data
result = new Array(200).fill(item);
} else {
throw new Error(`Unknown url data type ${type}`);
}

if (typeof e !== 'number') {
throw new Error(`e must be a number, received ${e}`);
}

for (let i = 0; i < e; ++i) {
result = result.concat(result);
}

if (asUrl) {
if (withBase) {
result = result.map(([input, base]) => new URL(input, base));
} else {
result = result.map((input) => new URL(input));
}
}
return result;
}
exports.bakeUrlData = bakeUrlData;
30 changes: 0 additions & 30 deletions benchmark/fixtures/url-inputs.js

This file was deleted.

2 changes: 1 addition & 1 deletion benchmark/querystring/querystring-parse.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const common = require('../common.js');
const querystring = require('querystring');
const inputs = require('../fixtures/url-inputs.js').searchParams;
const inputs = common.searchParams;

const bench = common.createBenchmark(main, {
type: Object.keys(inputs),
Expand Down
44 changes: 21 additions & 23 deletions benchmark/url/legacy-vs-whatwg-url-get-prop.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,15 @@ const common = require('../common.js');
const url = require('url');
const URL = url.URL;
const assert = require('assert');
const inputs = require('../fixtures/url-inputs.js').urls;

const bench = common.createBenchmark(main, {
type: Object.keys(inputs),
type: common.urlDataTypes,
method: ['legacy', 'whatwg'],
n: [1e5]
e: [1]
});

// At the time of writing, when using a passed property name to index
// the object, Crankshaft would generate a LoadKeyedGeneric even when it
// remains a constant in the function, so here we must use the literal
// instead to get a LoadNamedField.
function useLegacy(n, input) {
const obj = url.parse(input);
function useLegacy(data) {
const obj = url.parse(data[0]);
const noDead = {
protocol: obj.protocol,
auth: obj.auth,
Expand All @@ -27,10 +22,12 @@ function useLegacy(n, input) {
search: obj.search,
hash: obj.hash
};
const len = data.length;
// It's necessary to assign the values to an object
// to avoid loop invariant code motion.
bench.start();
for (var i = 0; i < n; i += 1) {
for (var i = 0; i < len; i++) {
const obj = data[i];
noDead.protocol = obj.protocol;
noDead.auth = obj.auth;
noDead.host = obj.host;
Expand All @@ -40,12 +37,12 @@ function useLegacy(n, input) {
noDead.search = obj.search;
noDead.hash = obj.hash;
}
bench.end(n);
bench.end(len);
return noDead;
}

function useWHATWG(n, input) {
const obj = new URL(input);
function useWHATWG(data) {
const obj = new URL(data[0]);
const noDead = {
protocol: obj.protocol,
auth: `${obj.username}:${obj.password}`,
Expand All @@ -56,8 +53,10 @@ function useWHATWG(n, input) {
search: obj.search,
hash: obj.hash
};
const len = data.length;
bench.start();
for (var i = 0; i < n; i += 1) {
for (var i = 0; i < len; i++) {
const obj = data[i];
noDead.protocol = obj.protocol;
noDead.auth = `${obj.username}:${obj.password}`;
noDead.host = obj.host;
Expand All @@ -67,23 +66,22 @@ function useWHATWG(n, input) {
noDead.search = obj.search;
noDead.hash = obj.hash;
}
bench.end(n);
bench.end(len);
return noDead;
}

function main({ type, n, method }) {
const input = inputs[type];
if (!input) {
throw new Error(`Unknown input type "${type}"`);
}

function main({ type, method, e }) {
e = +e;
var data;
var noDead; // Avoid dead code elimination.
switch (method) {
case 'legacy':
noDead = useLegacy(n, input);
data = common.bakeUrlData(type, e, false, false);
noDead = useLegacy(data.map((i) => url.parse(i)));
break;
case 'whatwg':
noDead = useWHATWG(n, input);
data = common.bakeUrlData(type, e, false, true);
noDead = useWHATWG(data);
break;
default:
throw new Error(`Unknown method "${method}"`);
Expand Down
58 changes: 36 additions & 22 deletions benchmark/url/legacy-vs-whatwg-url-parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,61 @@ const common = require('../common.js');
const url = require('url');
const URL = url.URL;
const assert = require('assert');
const inputs = require('../fixtures/url-inputs.js').urls;

const bench = common.createBenchmark(main, {
type: Object.keys(inputs),
method: ['legacy', 'whatwg'],
n: [1e5]
withBase: ['true', 'false'],
type: common.urlDataTypes,
e: [1],
method: ['legacy', 'whatwg']
});

function useLegacy(n, input) {
var noDead = url.parse(input);
function useLegacy(data) {
const len = data.length;
var result = url.parse(data[0]); // avoid dead code elimination
bench.start();
for (var i = 0; i < n; i += 1) {
noDead = url.parse(input);
for (var i = 0; i < len; ++i) {
result = url.parse(data[i]);
}
bench.end(n);
return noDead;
bench.end(len);
return result;
}

function useWHATWG(n, input) {
var noDead = new URL(input);
function useWHATWGWithBase(data) {
const len = data.length;
var result = new URL(data[0][0], data[0][1]); // avoid dead code elimination
bench.start();
for (var i = 0; i < n; i += 1) {
noDead = new URL(input);
for (var i = 0; i < len; ++i) {
const item = data[i];
result = new URL(item[0], item[1]);
}
bench.end(n);
return noDead;
bench.end(len);
return result;
}

function main({ type, n, method }) {
const input = inputs[type];
if (!input) {
throw new Error(`Unknown input type "${type}"`);
function useWHATWGWithoutBase(data) {
const len = data.length;
var result = new URL(data[0]); // avoid dead code elimination
bench.start();
for (var i = 0; i < len; ++i) {
result = new URL(data[i]);
}
bench.end(len);
return result;
}

function main({ e, method, type, withBase }) {
e = +e;
withBase = withBase === 'true';
var noDead; // Avoid dead code elimination.
var data;
switch (method) {
case 'legacy':
noDead = useLegacy(n, input);
data = common.bakeUrlData(type, e, false, false);
noDead = useLegacy(data);
break;
case 'whatwg':
noDead = useWHATWG(n, input);
data = common.bakeUrlData(type, e, withBase, false);
noDead = withBase ? useWHATWGWithBase(data) : useWHATWGWithoutBase(data);
break;
default:
throw new Error(`Unknown method ${method}`);
Expand Down
2 changes: 1 addition & 1 deletion benchmark/url/legacy-vs-whatwg-url-searchparams-parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const common = require('../common.js');
const { URLSearchParams } = require('url');
const querystring = require('querystring');
const searchParams = require('../fixtures/url-inputs.js').searchParams;
const searchParams = common.searchParams;

const bench = common.createBenchmark(main, {
searchParam: Object.keys(searchParams),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const common = require('../common.js');
const { URLSearchParams } = require('url');
const querystring = require('querystring');
const searchParams = require('../fixtures/url-inputs.js').searchParams;
const searchParams = common.searchParams;

const bench = common.createBenchmark(main, {
searchParam: Object.keys(searchParams),
Expand Down
Loading

0 comments on commit f5d4db1

Please sign in to comment.