1
+ /**
2
+ * Inspired by https://github.com/mizdra/eslint-interactive/blob/a5ab787c4ccc780a2999b88d59d719cd6c1e651d/e2e-test/global-installation/index.test.ts
3
+ */
1
4
"use strict" ;
2
5
3
6
const { spawn } = require ( "child_process" ) ;
4
7
const { rimraf } = require ( "rimraf" ) ;
5
8
const { mkdirp } = require ( "mkdirp" ) ;
6
9
7
10
const FILE_NAME = "for-benchmark" ;
11
+ const MAX = 10 ;
8
12
9
13
const LF = String . fromCharCode ( 0x0a ) ; // \n
10
14
const DOWN = String . fromCharCode ( 0x1b , 0x5b , 0x42 ) ; // ↓
@@ -14,22 +18,12 @@ async function wait(ms) {
14
18
return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
15
19
}
16
20
17
- async function readStream ( stream ) {
18
- let result = "" ;
19
- for await ( const line of stream ) {
20
- result += line ;
21
- }
22
- return result ;
23
- }
24
-
25
21
async function clear ( ) {
26
22
await rimraf ( "./src" ) ;
27
23
await mkdirp ( "./src" ) ;
28
24
}
29
25
30
- ( async ( ) => {
31
- const type = process . argv [ 2 ] ;
32
-
26
+ async function bench ( type ) {
33
27
if ( type === "plop" ) {
34
28
const plop = spawn ( "./node_modules/.bin/plop" ) ;
35
29
await wait ( 1000 ) ;
@@ -38,7 +32,7 @@ async function clear() {
38
32
const plopMeasureStart = performance . now ( ) ;
39
33
plop . stdin . write ( LF ) ;
40
34
const plopMeasureEnd = performance . now ( ) ;
41
- console . log ( `plop: ${ plopMeasureEnd - plopMeasureStart } ms` ) ;
35
+ return plopMeasureEnd - plopMeasureStart ;
42
36
}
43
37
44
38
if ( type === "scaffdog" ) {
@@ -55,7 +49,7 @@ async function clear() {
55
49
const scaffdogMeasureStart = performance . now ( ) ;
56
50
scaffdog . stdin . write ( LF ) ;
57
51
const scaffdogMeasureEnd = performance . now ( ) ;
58
- console . log ( `scaffdog: ${ scaffdogMeasureEnd - scaffdogMeasureStart } ms` ) ;
52
+ return scaffdogMeasureEnd - scaffdogMeasureStart ;
59
53
}
60
54
61
55
if ( type === "moldable" ) {
@@ -68,8 +62,26 @@ async function clear() {
68
62
const moldableMeasureStart = performance . now ( ) ;
69
63
moldable . stdin . write ( LF ) ;
70
64
const moldableMeasureEnd = performance . now ( ) ;
71
- console . log ( `moldable: ${ moldableMeasureEnd - moldableMeasureStart } ms` ) ;
65
+ return moldableMeasureEnd - moldableMeasureStart ;
66
+ }
67
+
68
+ return 0 ;
69
+ }
70
+
71
+ ( async ( ) => {
72
+ const type = process . argv [ 2 ] ;
73
+
74
+ const results = [ ] ;
75
+ for ( let i = 0 ; i < MAX ; i ++ ) {
76
+ const result = await bench ( type ) ;
77
+ console . log ( `[${ type } ] ${ i + 1 } time: ${ result } ms` ) ;
78
+ results . push ( result ) ;
79
+ await clear ( ) ;
72
80
}
73
81
74
- await clear ( ) ;
82
+ console . log (
83
+ `[${ type } ] average time of 10 times: ${ results . reduce ( ( acc , cur ) => acc + cur , 0 ) / MAX } ms`
84
+ ) ;
85
+
86
+ process . exit ( 0 ) ;
75
87
} ) ( ) ;
0 commit comments