Skip to content

Commit f5d4db1

Browse files
joyeecheungrvagg
authored andcommitted
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:[email protected]/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),

0 commit comments

Comments
 (0)