1
- load ( "shell-config.js" )
1
+ load ( "shell-config.js" ) ;
2
+ load ( "startup-helper/StartupBenchmark.js" ) ;
2
3
load ( "JetStreamDriver.js" ) ;
3
4
4
5
function assertTrue ( condition , message ) {
@@ -19,17 +20,25 @@ function assertEquals(actual, expected, message) {
19
20
}
20
21
}
21
22
23
+ function assertThrows ( message , func ) {
24
+ let didThrow = false ;
25
+ try {
26
+ func ( ) ;
27
+ } catch ( e ) {
28
+ didThrow = true ;
29
+ }
30
+ assertTrue ( didThrow , message ) ;
31
+ }
32
+
22
33
( function testTagsAreLowerCaseStrings ( ) {
23
34
for ( const benchmark of BENCHMARKS ) {
24
- benchmark . tags . forEach ( tag => {
25
- assertTrue ( typeof ( tag ) == "string" ) ;
26
- assertTrue ( tag == tag . toLowerCase ( ) ) ;
27
- } )
35
+ benchmark . tags . forEach ( ( tag ) => {
36
+ assertTrue ( typeof tag == "string" ) ;
37
+ assertTrue ( tag == tag . toLowerCase ( ) ) ;
38
+ } ) ;
28
39
}
29
40
} ) ( ) ;
30
41
31
-
32
-
33
42
( function testTagsAll ( ) {
34
43
for ( const benchmark of BENCHMARKS ) {
35
44
const tags = benchmark . tags ;
@@ -41,53 +50,56 @@ function assertEquals(actual, expected, message) {
41
50
}
42
51
} ) ( ) ;
43
52
44
-
45
53
( function testDriverBenchmarksOrder ( ) {
46
54
const benchmarks = findBenchmarksByTag ( "all" ) ;
47
55
const driver = new Driver ( benchmarks ) ;
48
56
assertEquals ( driver . benchmarks . length , BENCHMARKS . length ) ;
49
- const names = driver . benchmarks . map ( b => b . name . toLowerCase ( ) ) . sort ( ) . reverse ( ) ;
57
+ const names = driver . benchmarks
58
+ . map ( ( b ) => b . name . toLowerCase ( ) )
59
+ . sort ( )
60
+ . reverse ( ) ;
50
61
for ( let i = 0 ; i < names . length ; i ++ ) {
51
62
assertEquals ( driver . benchmarks [ i ] . name . toLowerCase ( ) , names [ i ] ) ;
52
63
}
53
64
} ) ( ) ;
54
65
55
-
56
66
( function testEnableByTag ( ) {
57
67
const driverA = new Driver ( findBenchmarksByTag ( "Default" ) ) ;
58
68
const driverB = new Driver ( findBenchmarksByTag ( "default" ) ) ;
59
69
assertTrue ( driverA . benchmarks . length > 0 ) ;
60
70
assertEquals ( driverA . benchmarks . length , driverB . benchmarks . length ) ;
61
71
const enabledBenchmarkNames = new Set (
62
- Array . from ( driverA . benchmarks ) . map ( b => b . name ) ) ;
72
+ Array . from ( driverA . benchmarks ) . map ( ( b ) => b . name )
73
+ ) ;
63
74
for ( const benchmark of BENCHMARKS ) {
64
75
if ( benchmark . tags . has ( "default" ) )
65
76
assertTrue ( enabledBenchmarkNames . has ( benchmark . name ) ) ;
66
77
}
67
78
} ) ( ) ;
68
79
69
-
70
80
( function testDriverEnableDuplicateAndSort ( ) {
71
- const benchmarks = [ ...findBenchmarksByTag ( "wasm" ) , ...findBenchmarksByTag ( "wasm" ) ] ;
72
- assertTrue ( benchmarks . length > 0 ) ;
73
- const uniqueBenchmarks = new Set ( benchmarks ) ;
74
- assertFalse ( uniqueBenchmarks . size == benchmarks . length ) ;
75
- const driver = new Driver ( benchmarks ) ;
76
- assertEquals ( driver . benchmarks . length , uniqueBenchmarks . size ) ;
81
+ const benchmarks = [
82
+ ...findBenchmarksByTag ( "wasm" ) ,
83
+ ...findBenchmarksByTag ( "wasm" ) ,
84
+ ] ;
85
+ assertTrue ( benchmarks . length > 0 ) ;
86
+ const uniqueBenchmarks = new Set ( benchmarks ) ;
87
+ assertFalse ( uniqueBenchmarks . size == benchmarks . length ) ;
88
+ const driver = new Driver ( benchmarks ) ;
89
+ assertEquals ( driver . benchmarks . length , uniqueBenchmarks . size ) ;
77
90
} ) ( ) ;
78
91
79
-
80
92
( function testBenchmarkSubScores ( ) {
81
93
for ( const benchmark of BENCHMARKS ) {
82
94
const subScores = benchmark . subScores ( ) ;
83
95
assertTrue ( subScores instanceof Object ) ;
84
96
assertTrue ( Object . keys ( subScores ) . length > 0 ) ;
85
97
for ( const [ name , value ] of Object . entries ( subScores ) ) {
86
- assertTrue ( typeof ( name ) == "string" ) ;
98
+ assertTrue ( typeof name == "string" ) ;
87
99
// "Score" can only be part of allScores().
88
100
assertFalse ( name == "Score" ) ;
89
101
// Without running values should be either null (or 0 for GroupedBenchmark)
90
- assertFalse ( value )
102
+ assertFalse ( value ) ;
91
103
}
92
104
}
93
105
} ) ( ) ;
@@ -98,7 +110,112 @@ function assertEquals(actual, expected, message) {
98
110
const allScores = benchmark . allScores ( ) ;
99
111
assertTrue ( "Score" in allScores ) ;
100
112
// All subScore items are part of allScores.
101
- for ( const name of Object . keys ( subScores ) )
102
- assertTrue ( name in allScores ) ;
113
+ for ( const name of Object . keys ( subScores ) ) assertTrue ( name in allScores ) ;
103
114
}
104
115
} ) ( ) ;
116
+
117
+ function validateIterationSources ( sources ) {
118
+ for ( const source of sources ) {
119
+ assertTrue ( typeof source == "string" ) ;
120
+ assertFalse ( source . includes ( CACHE_BUST_COMMENT ) ) ;
121
+ }
122
+ }
123
+
124
+ ( async function testStartupBenchmark ( ) {
125
+ try {
126
+ JetStream . preload = { BUNDLE : "test-bundle.js" } ;
127
+ JetStream . getString = ( file ) => {
128
+ assertEquals ( file , "test-bundle.js" ) ;
129
+ return `function test() {
130
+ ${ CACHE_BUST_COMMENT }
131
+ return 1;
132
+ }` ;
133
+ } ;
134
+ await testStartupBenchmarkInnerTests ( ) ;
135
+ } finally {
136
+ JetStream . preload = undefined ;
137
+ JetStream . getString = undefined ;
138
+ }
139
+ } ) ( ) ;
140
+
141
+ async function testStartupBenchmarkInnerTests ( ) {
142
+ const benchmark = new StartupBenchmark ( {
143
+ iterationCount : 12 ,
144
+ expectedCacheCommentCount : 1 ,
145
+ } ) ;
146
+ assertEquals ( benchmark . iterationCount , 12 ) ;
147
+ assertEquals ( benchmark . expectedCacheCommentCount , 1 ) ;
148
+ assertEquals ( benchmark . iterationSourceCodes . length , 0 ) ;
149
+ assertEquals ( benchmark . sourceCode , undefined ) ;
150
+ assertEquals ( benchmark . sourceHash , 0 ) ;
151
+ await benchmark . init ( ) ;
152
+ assertEquals ( benchmark . sourceHash , 177573 ) ;
153
+ assertEquals ( benchmark . sourceCode . length , 68 ) ;
154
+ assertEquals ( benchmark . iterationSourceCodes . length , 12 ) ;
155
+ assertEquals ( new Set ( benchmark . iterationSourceCodes ) . size , 12 ) ;
156
+ validateIterationSources ( benchmark . iterationSourceCodes ) ;
157
+
158
+ const noReuseBenchmark = new StartupBenchmark ( {
159
+ iterationCount : 12 ,
160
+ expectedCacheCommentCount : 1 ,
161
+ sourceCodeReuseCount : 0 ,
162
+ } ) ;
163
+ assertEquals ( noReuseBenchmark . iterationSourceCodes . length , 0 ) ;
164
+ await noReuseBenchmark . init ( ) ;
165
+ assertEquals ( noReuseBenchmark . iterationSourceCodes . length , 12 ) ;
166
+ assertEquals ( new Set ( noReuseBenchmark . iterationSourceCodes ) . size , 1 ) ;
167
+ validateIterationSources ( noReuseBenchmark . iterationSourceCodes ) ;
168
+
169
+ const reuseBenchmark = new StartupBenchmark ( {
170
+ iterationCount : 12 ,
171
+ expectedCacheCommentCount : 1 ,
172
+ sourceCodeReuseCount : 3 ,
173
+ } ) ;
174
+ assertEquals ( reuseBenchmark . iterationSourceCodes . length , 0 ) ;
175
+ await reuseBenchmark . init ( ) ;
176
+ assertEquals ( reuseBenchmark . iterationSourceCodes . length , 12 ) ;
177
+ assertEquals ( new Set ( reuseBenchmark . iterationSourceCodes ) . size , 4 ) ;
178
+ validateIterationSources ( reuseBenchmark . iterationSourceCodes ) ;
179
+
180
+ const reuseBenchmark2 = new StartupBenchmark ( {
181
+ iterationCount : 12 ,
182
+ expectedCacheCommentCount : 1 ,
183
+ sourceCodeReuseCount : 5 ,
184
+ } ) ;
185
+ assertEquals ( reuseBenchmark2 . iterationSourceCodes . length , 0 ) ;
186
+ await reuseBenchmark2 . init ( ) ;
187
+ assertEquals ( reuseBenchmark2 . iterationSourceCodes . length , 12 ) ;
188
+ assertEquals ( new Set ( reuseBenchmark2 . iterationSourceCodes ) . size , 3 ) ;
189
+ validateIterationSources ( reuseBenchmark2 . iterationSourceCodes ) ;
190
+ }
191
+
192
+ ( function testStartupBenchmarkThrow ( ) {
193
+ assertThrows (
194
+ "StartupBenchmark constructor should throw with no arguments." ,
195
+ ( ) => new StartupBenchmark ( )
196
+ ) ;
197
+
198
+ assertThrows (
199
+ "StartupBenchmark constructor should throw with missing expectedCacheCommentCount." ,
200
+ ( ) => new StartupBenchmark ( { iterationCount : 1 } )
201
+ ) ;
202
+
203
+ assertThrows (
204
+ "StartupBenchmark constructor should throw with missing iterationCount." ,
205
+ ( ) => new StartupBenchmark ( { expectedCacheCommentCount : 1 } )
206
+ ) ;
207
+
208
+ assertThrows (
209
+ "StartupBenchmark constructor should throw with iterationCount=0." ,
210
+ ( ) => {
211
+ new StartupBenchmark ( { iterationCount : 0 , expectedCacheCommentCount : 1 } ) ;
212
+ }
213
+ ) ;
214
+
215
+ assertThrows (
216
+ "StartupBenchmark constructor should throw with expectedCacheCommentCount=0." ,
217
+ ( ) => {
218
+ new StartupBenchmark ( { iterationCount : 1 , expectedCacheCommentCount : 0 } ) ;
219
+ }
220
+ ) ;
221
+ } ) ( ) ;
0 commit comments