Skip to content

Commit f5d4db1

Browse files
joyeecheungrvagg
authored andcommittedNov 28, 2018
benchmark: pre-generate data set for URL benchmarks
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 <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 957ecbe commit f5d4db1

12 files changed

+201
-138
lines changed
 

‎benchmark/common.js

+92
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,95 @@ exports.binding = function(bindingName) {
254254
return process.binding(bindingName);
255255
}
256256
};
257+
258+
const urls = {
259+
long: 'http://nodejs.org:89/docs/latest/api/foo/bar/qua/13949281/0f28b/' +
260+
'/5d49/b3020/url.html#test?payload1=true&payload2=false&test=1' +
261+
'&benchmark=3&foo=38.38.011.293&bar=1234834910480&test=19299&3992&' +
262+
'key=f5c65e1e98fe07e648249ad41e1cfdb0',
263+
short: 'https://nodejs.org/en/blog/',
264+
idn: 'http://你好你好.在线',
265+
auth: 'https://user:pass@example.com/path?search=1',
266+
file: 'file:///foo/bar/test/node.js',
267+
ws: 'ws://localhost:9229/f46db715-70df-43ad-a359-7f9949f39868',
268+
javascript: 'javascript:alert("node is awesome");',
269+
percent: 'https://%E4%BD%A0/foo',
270+
dot: 'https://example.org/./a/../b/./c'
271+
};
272+
exports.urls = urls;
273+
274+
const searchParams = {
275+
noencode: 'foo=bar&baz=quux&xyzzy=thud',
276+
multicharsep: 'foo=bar&&&&&&&&&&baz=quux&&&&&&&&&&xyzzy=thud',
277+
encodefake: 'foo=%©ar&baz=%A©uux&xyzzy=%©ud',
278+
encodemany: '%66%6F%6F=bar&%62%61%7A=quux&xyzzy=%74h%75d',
279+
encodelast: 'foo=bar&baz=quux&xyzzy=thu%64',
280+
multivalue: 'foo=bar&foo=baz&foo=quux&quuy=quuz',
281+
multivaluemany: 'foo=bar&foo=baz&foo=quux&quuy=quuz&foo=abc&foo=def&' +
282+
'foo=ghi&foo=jkl&foo=mno&foo=pqr&foo=stu&foo=vwxyz',
283+
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',
284+
manyblankpairs: '&&&&&&&&&&&&&&&&&&&&&&&&',
285+
altspaces: 'foo+bar=baz+quux&xyzzy+thud=quuy+quuz&abc=def+ghi'
286+
};
287+
exports.searchParams = searchParams;
288+
289+
function getUrlData(withBase) {
290+
const data = require('../test/fixtures/wpt/url/resources/urltestdata.json');
291+
const result = [];
292+
for (const item of data) {
293+
if (item.failure || !item.input) continue;
294+
if (withBase) {
295+
result.push([item.input, item.base]);
296+
} else if (item.base !== 'about:blank') {
297+
result.push(item.base);
298+
}
299+
}
300+
return result;
301+
}
302+
303+
exports.urlDataTypes = Object.keys(urls).concat(['wpt']);
304+
305+
/**
306+
* Generate an array of data for URL benchmarks to use.
307+
* The size of the resulting data set is the original data size * 2 ** `e`.
308+
* The 'wpt' type contains about 400 data points when `withBase` is true,
309+
* and 200 data points when `withBase` is false.
310+
* Other types contain 200 data points with or without base.
311+
*
312+
* @param {string} type Type of the data, 'wpt' or a key of `urls`
313+
* @param {number} e The repetition of the data, as exponent of 2
314+
* @param {boolean} withBase Whether to include a base URL
315+
* @param {boolean} asUrl Whether to return the results as URL objects
316+
* @return {string[] | string[][] | URL[]}
317+
*/
318+
function bakeUrlData(type, e = 0, withBase = false, asUrl = false) {
319+
let result = [];
320+
if (type === 'wpt') {
321+
result = getUrlData(withBase);
322+
} else if (urls[type]) {
323+
const input = urls[type];
324+
const item = withBase ? [input, 'about:blank'] : input;
325+
// Roughly the size of WPT URL test data
326+
result = new Array(200).fill(item);
327+
} else {
328+
throw new Error(`Unknown url data type ${type}`);
329+
}
330+
331+
if (typeof e !== 'number') {
332+
throw new Error(`e must be a number, received ${e}`);
333+
}
334+
335+
for (let i = 0; i < e; ++i) {
336+
result = result.concat(result);
337+
}
338+
339+
if (asUrl) {
340+
if (withBase) {
341+
result = result.map(([input, base]) => new URL(input, base));
342+
} else {
343+
result = result.map((input) => new URL(input));
344+
}
345+
}
346+
return result;
347+
}
348+
exports.bakeUrlData = bakeUrlData;

‎benchmark/fixtures/url-inputs.js

-30
This file was deleted.

‎benchmark/querystring/querystring-parse.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
const common = require('../common.js');
33
const querystring = require('querystring');
4-
const inputs = require('../fixtures/url-inputs.js').searchParams;
4+
const inputs = common.searchParams;
55

66
const bench = common.createBenchmark(main, {
77
type: Object.keys(inputs),

‎benchmark/url/legacy-vs-whatwg-url-get-prop.js

+21-23
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,15 @@ const common = require('../common.js');
33
const url = require('url');
44
const URL = url.URL;
55
const assert = require('assert');
6-
const inputs = require('../fixtures/url-inputs.js').urls;
76

87
const bench = common.createBenchmark(main, {
9-
type: Object.keys(inputs),
8+
type: common.urlDataTypes,
109
method: ['legacy', 'whatwg'],
11-
n: [1e5]
10+
e: [1]
1211
});
1312

14-
// At the time of writing, when using a passed property name to index
15-
// the object, Crankshaft would generate a LoadKeyedGeneric even when it
16-
// remains a constant in the function, so here we must use the literal
17-
// instead to get a LoadNamedField.
18-
function useLegacy(n, input) {
19-
const obj = url.parse(input);
13+
function useLegacy(data) {
14+
const obj = url.parse(data[0]);
2015
const noDead = {
2116
protocol: obj.protocol,
2217
auth: obj.auth,
@@ -27,10 +22,12 @@ function useLegacy(n, input) {
2722
search: obj.search,
2823
hash: obj.hash
2924
};
25+
const len = data.length;
3026
// It's necessary to assign the values to an object
3127
// to avoid loop invariant code motion.
3228
bench.start();
33-
for (var i = 0; i < n; i += 1) {
29+
for (var i = 0; i < len; i++) {
30+
const obj = data[i];
3431
noDead.protocol = obj.protocol;
3532
noDead.auth = obj.auth;
3633
noDead.host = obj.host;
@@ -40,12 +37,12 @@ function useLegacy(n, input) {
4037
noDead.search = obj.search;
4138
noDead.hash = obj.hash;
4239
}
43-
bench.end(n);
40+
bench.end(len);
4441
return noDead;
4542
}
4643

47-
function useWHATWG(n, input) {
48-
const obj = new URL(input);
44+
function useWHATWG(data) {
45+
const obj = new URL(data[0]);
4946
const noDead = {
5047
protocol: obj.protocol,
5148
auth: `${obj.username}:${obj.password}`,
@@ -56,8 +53,10 @@ function useWHATWG(n, input) {
5653
search: obj.search,
5754
hash: obj.hash
5855
};
56+
const len = data.length;
5957
bench.start();
60-
for (var i = 0; i < n; i += 1) {
58+
for (var i = 0; i < len; i++) {
59+
const obj = data[i];
6160
noDead.protocol = obj.protocol;
6261
noDead.auth = `${obj.username}:${obj.password}`;
6362
noDead.host = obj.host;
@@ -67,23 +66,22 @@ function useWHATWG(n, input) {
6766
noDead.search = obj.search;
6867
noDead.hash = obj.hash;
6968
}
70-
bench.end(n);
69+
bench.end(len);
7170
return noDead;
7271
}
7372

74-
function main({ type, n, method }) {
75-
const input = inputs[type];
76-
if (!input) {
77-
throw new Error(`Unknown input type "${type}"`);
78-
}
79-
73+
function main({ type, method, e }) {
74+
e = +e;
75+
var data;
8076
var noDead; // Avoid dead code elimination.
8177
switch (method) {
8278
case 'legacy':
83-
noDead = useLegacy(n, input);
79+
data = common.bakeUrlData(type, e, false, false);
80+
noDead = useLegacy(data.map((i) => url.parse(i)));
8481
break;
8582
case 'whatwg':
86-
noDead = useWHATWG(n, input);
83+
data = common.bakeUrlData(type, e, false, true);
84+
noDead = useWHATWG(data);
8785
break;
8886
default:
8987
throw new Error(`Unknown method "${method}"`);

‎benchmark/url/legacy-vs-whatwg-url-parse.js

+36-22
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,61 @@ const common = require('../common.js');
33
const url = require('url');
44
const URL = url.URL;
55
const assert = require('assert');
6-
const inputs = require('../fixtures/url-inputs.js').urls;
76

87
const bench = common.createBenchmark(main, {
9-
type: Object.keys(inputs),
10-
method: ['legacy', 'whatwg'],
11-
n: [1e5]
8+
withBase: ['true', 'false'],
9+
type: common.urlDataTypes,
10+
e: [1],
11+
method: ['legacy', 'whatwg']
1212
});
1313

14-
function useLegacy(n, input) {
15-
var noDead = url.parse(input);
14+
function useLegacy(data) {
15+
const len = data.length;
16+
var result = url.parse(data[0]); // avoid dead code elimination
1617
bench.start();
17-
for (var i = 0; i < n; i += 1) {
18-
noDead = url.parse(input);
18+
for (var i = 0; i < len; ++i) {
19+
result = url.parse(data[i]);
1920
}
20-
bench.end(n);
21-
return noDead;
21+
bench.end(len);
22+
return result;
2223
}
2324

24-
function useWHATWG(n, input) {
25-
var noDead = new URL(input);
25+
function useWHATWGWithBase(data) {
26+
const len = data.length;
27+
var result = new URL(data[0][0], data[0][1]); // avoid dead code elimination
2628
bench.start();
27-
for (var i = 0; i < n; i += 1) {
28-
noDead = new URL(input);
29+
for (var i = 0; i < len; ++i) {
30+
const item = data[i];
31+
result = new URL(item[0], item[1]);
2932
}
30-
bench.end(n);
31-
return noDead;
33+
bench.end(len);
34+
return result;
3235
}
3336

34-
function main({ type, n, method }) {
35-
const input = inputs[type];
36-
if (!input) {
37-
throw new Error(`Unknown input type "${type}"`);
37+
function useWHATWGWithoutBase(data) {
38+
const len = data.length;
39+
var result = new URL(data[0]); // avoid dead code elimination
40+
bench.start();
41+
for (var i = 0; i < len; ++i) {
42+
result = new URL(data[i]);
3843
}
44+
bench.end(len);
45+
return result;
46+
}
3947

48+
function main({ e, method, type, withBase }) {
49+
e = +e;
50+
withBase = withBase === 'true';
4051
var noDead; // Avoid dead code elimination.
52+
var data;
4153
switch (method) {
4254
case 'legacy':
43-
noDead = useLegacy(n, input);
55+
data = common.bakeUrlData(type, e, false, false);
56+
noDead = useLegacy(data);
4457
break;
4558
case 'whatwg':
46-
noDead = useWHATWG(n, input);
59+
data = common.bakeUrlData(type, e, withBase, false);
60+
noDead = withBase ? useWHATWGWithBase(data) : useWHATWGWithoutBase(data);
4761
break;
4862
default:
4963
throw new Error(`Unknown method ${method}`);

‎benchmark/url/legacy-vs-whatwg-url-searchparams-parse.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const common = require('../common.js');
33
const { URLSearchParams } = require('url');
44
const querystring = require('querystring');
5-
const searchParams = require('../fixtures/url-inputs.js').searchParams;
5+
const searchParams = common.searchParams;
66

77
const bench = common.createBenchmark(main, {
88
searchParam: Object.keys(searchParams),

‎benchmark/url/legacy-vs-whatwg-url-searchparams-serialize.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const common = require('../common.js');
33
const { URLSearchParams } = require('url');
44
const querystring = require('querystring');
5-
const searchParams = require('../fixtures/url-inputs.js').searchParams;
5+
const searchParams = common.searchParams;
66

77
const bench = common.createBenchmark(main, {
88
searchParam: Object.keys(searchParams),

‎benchmark/url/legacy-vs-whatwg-url-serialize.js

+19-20
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,48 @@ const common = require('../common.js');
33
const url = require('url');
44
const URL = url.URL;
55
const assert = require('assert');
6-
const inputs = require('../fixtures/url-inputs.js').urls;
76

87
const bench = common.createBenchmark(main, {
9-
type: Object.keys(inputs),
8+
type: common.urlDataTypes,
109
method: ['legacy', 'whatwg'],
11-
n: [1e5]
10+
e: [1]
1211
});
1312

14-
function useLegacy(n, input, prop) {
15-
const obj = url.parse(input);
13+
function useLegacy(data) {
14+
const obj = url.parse(data[0]);
15+
const len = data.length;
1616
var noDead = url.format(obj);
1717
bench.start();
18-
for (var i = 0; i < n; i += 1) {
19-
noDead = url.format(obj);
18+
for (var i = 0; i < len; i++) {
19+
noDead = data[i].toString();
2020
}
21-
bench.end(n);
21+
bench.end(len);
2222
return noDead;
2323
}
2424

25-
function useWHATWG(n, input, prop) {
26-
const obj = new URL(input);
25+
function useWHATWG(data) {
26+
const obj = new URL(data[0]);
27+
const len = data.length;
2728
var noDead = obj.toString();
2829
bench.start();
29-
for (var i = 0; i < n; i += 1) {
30-
noDead = obj.toString();
30+
for (var i = 0; i < len; i++) {
31+
noDead = data[i].toString();
3132
}
32-
bench.end(n);
33+
bench.end(len);
3334
return noDead;
3435
}
3536

36-
function main({ type, n, method }) {
37-
const input = inputs[type];
38-
if (!input) {
39-
throw new Error(`Unknown input type "${type}"`);
40-
}
37+
function main({ type, e, method }) {
38+
e = +e;
39+
const data = common.bakeUrlData(type, e, false, false);
4140

4241
var noDead; // Avoid dead code elimination.
4342
switch (method) {
4443
case 'legacy':
45-
noDead = useLegacy(n, input);
44+
noDead = useLegacy(data);
4645
break;
4746
case 'whatwg':
48-
noDead = useWHATWG(n, input);
47+
noDead = useWHATWG(data);
4948
break;
5049
default:
5150
throw new Error(`Unknown method ${method}`);

‎benchmark/url/url-resolve.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
const common = require('../common.js');
33
const url = require('url');
4-
const hrefs = require('../fixtures/url-inputs.js').urls;
4+
const hrefs = common.urls;
55
hrefs.noscheme = 'some.ran/dom/url.thing?oh=yes#whoo';
66

77
const paths = {

‎benchmark/url/url-searchparams-sort.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const common = require('../common.js');
33
const URLSearchParams = require('url').URLSearchParams;
44

55
const inputs = {
6+
wpt: 'wpt', // to work around tests
67
empty: '',
78
sorted: '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',
89
almostsorted: 'a&b&c&d&e&f&g&i&h&j&k&l&m&n&o&p&q&r&s&t&u&w&v&x&y&z',

‎benchmark/url/whatwg-url-properties.js

+25-38
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,42 @@
11
'use strict';
22
const common = require('../common.js');
3-
const URL = require('url').URL;
4-
const inputs = require('../fixtures/url-inputs.js').urls;
53

64
const bench = common.createBenchmark(main, {
7-
input: Object.keys(inputs),
5+
withBase: ['true', 'false'],
6+
type: ['wpt'], // Too many combinations - just use WPT by default
7+
e: [1],
88
prop: ['href', 'origin', 'protocol',
99
'username', 'password', 'host', 'hostname', 'port',
10-
'pathname', 'search', 'searchParams', 'hash'],
11-
n: [3e5]
10+
'pathname', 'search', 'searchParams', 'hash']
1211
});
1312

14-
function setAndGet(n, url, prop, alternative) {
15-
const old = url[prop];
13+
function setAndGet(data, prop) {
14+
const len = data.length;
15+
var result = data[0][prop];
1616
bench.start();
17-
for (var i = 0; i < n; i += 1) {
18-
url[prop] = n % 2 === 0 ? alternative : old; // set
19-
url[prop]; // get
17+
for (var i = 0; i < len; ++i) {
18+
result = data[i][prop];
19+
data[i][prop] = result;
2020
}
21-
bench.end(n);
21+
bench.end(len);
22+
return result;
2223
}
2324

24-
function get(n, url, prop) {
25+
function get(data, prop) {
26+
const len = data.length;
27+
var result = data[0][prop];
2528
bench.start();
26-
for (var i = 0; i < n; i += 1) {
27-
url[prop]; // get
29+
for (var i = 0; i < len; ++i) {
30+
result = data[i][prop]; // get
2831
}
29-
bench.end(n);
32+
bench.end(len);
33+
return result;
3034
}
3135

32-
const alternatives = {
33-
href: 'http://user:pass@foo.bar.com:21/aaa/zzz?l=25#test',
34-
protocol: 'https:',
35-
username: 'user2',
36-
password: 'pass2',
37-
host: 'foo.bar.net:22',
38-
hostname: 'foo.bar.org',
39-
port: '23',
40-
pathname: '/aaa/bbb',
41-
search: '?k=99',
42-
hash: '#abcd'
43-
};
44-
45-
function getAlternative(prop) {
46-
return alternatives[prop];
47-
}
48-
49-
function main({ n, input, prop }) {
50-
const value = inputs[input];
51-
const url = new URL(value);
52-
36+
function main({ e, type, prop, withBase }) {
37+
e = +e;
38+
withBase = withBase === 'true';
39+
const data = common.bakeUrlData(type, e, withBase, true);
5340
switch (prop) {
5441
case 'protocol':
5542
case 'username':
@@ -61,11 +48,11 @@ function main({ n, input, prop }) {
6148
case 'search':
6249
case 'hash':
6350
case 'href':
64-
setAndGet(n, url, prop, getAlternative(prop));
51+
setAndGet(data, prop);
6552
break;
6653
case 'origin':
6754
case 'searchParams':
68-
get(n, url, prop);
55+
get(data, prop);
6956
break;
7057
default:
7158
throw new Error('Unknown prop');

‎test/benchmark/test-benchmark-url.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const runBenchmark = require('../common/benchmark');
77
runBenchmark('url',
88
[
99
'method=legacy',
10+
'e=0',
1011
'loopMethod=forEach',
1112
'accessMethod=get',
1213
'type=short',
@@ -18,6 +19,7 @@ runBenchmark('url',
1819
'to=ascii',
1920
'prop=href',
2021
'n=1',
21-
'param=one'
22+
'param=one',
23+
'withBase=false'
2224
],
2325
{ NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });

0 commit comments

Comments
 (0)
Please sign in to comment.