1
1
"use strict" ;
2
2
3
3
import { bench , run } from "mitata" ;
4
- import { existsSync , createWriteStream , readFileSync } from "node:fs" ;
4
+ import { existsSync , createWriteStream , readFileSync , mkdirSync } from "node:fs" ;
5
5
import axios from "axios" ;
6
6
7
- var urls = [
7
+ const fixturesFolderPath = new URL ( 'fixtures' , import . meta. url ) . pathname ;
8
+ const urls = [
8
9
"https://raw.githubusercontent.com/ada-url/url-various-datasets/main/files/linux_files.txt" ,
9
10
"https://raw.githubusercontent.com/ada-url/url-various-datasets/main/others/kasztp.txt" ,
10
11
"https://raw.githubusercontent.com/ada-url/url-various-datasets/main/others/userbait.txt" ,
@@ -13,10 +14,10 @@ var urls = [
13
14
] ;
14
15
15
16
function get_filename ( url ) {
16
- return url . substring ( url . lastIndexOf ( "/" ) + 1 ) ;
17
+ return `fixtures/ ${ url . substring ( url . lastIndexOf ( "/" ) + 1 ) } ` ;
17
18
}
18
19
19
- const downloadFile = async ( url ) => {
20
+ async function downloadFile ( url ) {
20
21
const response = await axios ( {
21
22
method : "GET" ,
22
23
url : url ,
@@ -34,67 +35,41 @@ const downloadFile = async (url) => {
34
35
reject ( err ) ;
35
36
} ) ;
36
37
} ) ;
37
- } ;
38
-
39
- async function download ( urls ) {
40
- const urls_for_download = urls . filter ( ( url ) => {
41
- return ! existsSync ( get_filename ( url ) ) ;
42
- } ) ;
43
- var all_promises = urls_for_download . map ( ( url ) => downloadFile ( url ) ) ;
44
- return axios . all ( all_promises ) ;
45
38
}
46
39
47
- await download ( urls ) ;
48
-
49
- async function download_and_run ( filename , url ) {
50
- if ( existsSync ( filename ) ) {
51
- run_bench ( filename ) ;
52
- } else {
53
- get ( url , ( res ) => {
54
- const file = createWriteStream ( filename ) ;
55
- res . pipe ( file ) ;
56
- file . on ( "error" , ( err ) => {
57
- console . error ( err ) ;
58
- } ) ;
59
- file . on ( "finish" , async ( ) => {
60
- file . close ( ) ;
61
- run_bench ( filename ) ;
62
- } ) ;
63
- } ) ;
64
- }
40
+ if ( ! existsSync ( fixturesFolderPath ) ) {
41
+ mkdirSync ( fixturesFolderPath )
65
42
}
66
-
67
- var length = 0 ;
68
- var bad_url = 0 ;
69
- var good_url = 0 ;
70
-
71
- async function run_bench ( urls ) {
72
- for ( let url of urls ) {
73
- const filename = get_filename ( url ) ;
74
- const file_content = readFileSync ( filename , "utf-8" ) ;
75
- const lines = file_content . split ( "\n" ) ;
76
-
77
- bench ( filename , ( ) => {
78
- for ( var i = 0 ; i < lines . length ; i ++ ) {
79
- try {
80
- length += new URL ( lines [ i ] ) . href . length ;
81
- good_url ++ ;
82
- } catch ( e ) {
83
- bad_url ++ ;
84
- }
43
+ const urls_for_download = urls . filter ( url => ! existsSync ( get_filename ( url ) ) )
44
+ const all_promises = urls_for_download . map ( ( url ) => downloadFile ( url ) ) ;
45
+ await axios . all ( all_promises ) ;
46
+
47
+ let length = 0 ;
48
+ let bad_url = 0 ;
49
+ let good_url = 0 ;
50
+
51
+ for ( let url of urls ) {
52
+ const filename = get_filename ( url ) ;
53
+ const file_content = readFileSync ( filename , "utf-8" ) ;
54
+ const lines = file_content . split ( "\n" ) ;
55
+
56
+ bench ( filename , ( ) => {
57
+ for ( let i = 0 ; i < lines . length ; i ++ ) {
58
+ try {
59
+ length += new URL ( lines [ i ] ) . href . length ;
60
+ good_url ++ ;
61
+ } catch ( e ) {
62
+ bad_url ++ ;
85
63
}
86
- return length ;
87
- } ) ;
88
- }
89
-
90
- await run ( ) ;
64
+ }
65
+ return length ;
66
+ } ) ;
91
67
}
92
68
93
- await run_bench ( urls ) ;
69
+ await run ( ) ;
94
70
95
- console . log ( "Average URL size: " + Math . round ( length / good_url ) + " bytes" ) ;
96
- console . log (
97
- "Ratio of bad URLs: " +
98
- Math . round ( ( bad_url / ( good_url + bad_url ) ) * 10000 ) / 100.0 +
99
- "%"
71
+ console . info ( "Average URL size: " + Math . round ( length / good_url ) + " bytes" ) ;
72
+ console . info (
73
+ "Ratio of bad URLs:" ,
74
+ `${ Math . round ( ( bad_url / ( good_url + bad_url ) ) * 10000 ) / 100 } %` ,
100
75
) ;
0 commit comments