-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbench_url.js
76 lines (63 loc) · 2.06 KB
/
bench_url.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"use strict";
import { bench, run } from "mitata";
import { existsSync, createWriteStream, readFileSync, mkdirSync } from "node:fs";
import path from "node:path";
import axios from "axios";
const fixturesFolderPath = new URL('fixtures', import.meta.url).pathname;
const urls = [
"https://raw.githubusercontent.com/ada-url/url-various-datasets/main/files/linux_files.txt",
"https://raw.githubusercontent.com/ada-url/url-various-datasets/main/others/kasztp.txt",
"https://raw.githubusercontent.com/ada-url/url-various-datasets/main/others/userbait.txt",
"https://raw.githubusercontent.com/ada-url/url-various-datasets/main/top100/top100.txt",
"https://raw.githubusercontent.com/ada-url/url-various-datasets/main/wikipedia/wikipedia_100k.txt",
];
function get_filename(url) {
return `fixtures/${path.basename(url)}`;
}
async function downloadFile(url) {
const response = await axios({
method: "GET",
url: url,
responseType: "stream",
});
response.data.pipe(createWriteStream(get_filename(url)));
return new Promise((resolve, reject) => {
response.data.on("end", () => {
resolve();
});
response.data.on("error", (err) => {
reject(err);
});
});
}
if (!existsSync(fixturesFolderPath)) {
mkdirSync(fixturesFolderPath)
}
const urls_for_download = urls.filter(url => !existsSync(get_filename(url)))
const all_promises = urls_for_download.map((url) => downloadFile(url));
await axios.all(all_promises);
let length = 0;
let bad_url = 0;
let good_url = 0;
for (let url of urls) {
const filename = get_filename(url);
const file_content = readFileSync(filename, "utf-8");
const lines = file_content.split("\n");
bench(filename, () => {
for (let i = 0; i < lines.length; i++) {
try {
length += new URL(lines[i]).href.length;
good_url++;
} catch (e) {
bad_url++;
}
}
return length;
});
}
await run();
console.info("Average URL size: " + Math.round(length / good_url) + " bytes");
console.info(
"Ratio of bad URLs:",
`${Math.round((bad_url / (good_url + bad_url)) * 10000) / 100}%`,
);