@@ -181,47 +181,27 @@ function uiFriendlyDuration(time) {
181
181
return `${ time . toFixed ( 3 ) } ms` ;
182
182
}
183
183
184
+ // TODO: Cleanup / remove / merge. This is only used for caching loads in the
185
+ // non-browser setting. In the browser we use exclusively `loadCache`,
186
+ // `loadBlob`, `doLoadBlob`, `prefetchResourcesForBrowser` etc., see below.
184
187
const fileLoader = ( function ( ) {
185
188
class Loader {
186
189
constructor ( ) {
187
190
this . requests = new Map ;
188
191
}
189
192
190
- async _loadInternal ( url ) {
191
- if ( ! isInBrowser )
192
- return Promise . resolve ( readFile ( url ) ) ;
193
+ // Cache / memoize previously read files, because some workloads
194
+ // share common code.
195
+ load ( url ) {
196
+ assert ( ! isInBrowser ) ;
193
197
194
- let response ;
195
- const tries = 3 ;
196
- while ( tries -- ) {
197
- let hasError = false ;
198
- try {
199
- response = await fetch ( url ) ;
200
- } catch ( e ) {
201
- hasError = true ;
202
- }
203
- if ( ! hasError && response . ok )
204
- break ;
205
- if ( tries )
206
- continue ;
207
- globalThis . allIsGood = false ;
208
- throw new Error ( "Fetch failed" ) ;
209
- }
210
- if ( url . indexOf ( ".js" ) !== - 1 )
211
- return response . text ( ) ;
212
- else if ( url . indexOf ( ".wasm" ) !== - 1 )
213
- return response . arrayBuffer ( ) ;
214
-
215
- throw new Error ( "should not be reached!" ) ;
216
- }
217
-
218
- async load ( url ) {
219
- if ( this . requests . has ( url ) )
198
+ if ( this . requests . has ( url ) ) {
220
199
return this . requests . get ( url ) ;
200
+ }
221
201
222
- const promise = this . _loadInternal ( url ) ;
223
- this . requests . set ( url , promise ) ;
224
- return promise ;
202
+ const contents = readFile ( url ) ;
203
+ this . requests . set ( url , contents ) ;
204
+ return contents ;
225
205
}
226
206
}
227
207
return new Loader ;
@@ -233,6 +213,8 @@ class Driver {
233
213
this . isDone = false ;
234
214
this . errors = [ ] ;
235
215
this . benchmarks = [ ] ;
216
+ // TODO: Cleanup / remove / merge `blobDataCache` and `loadCache` vs.
217
+ // the global `fileLoader` cache.
236
218
this . blobDataCache = { } ;
237
219
this . loadCache = { } ;
238
220
this . counter = { } ;
@@ -241,9 +223,10 @@ class Driver {
241
223
this . counter . failedPreloadResources = 0 ;
242
224
}
243
225
226
+ // TODO: Remove, make `this.benchmarks` immutable and set it once in the
227
+ // ctor instead of this and the global `addBenchmarksBy*` functions.
244
228
addBenchmark ( benchmark ) {
245
229
this . benchmarks . push ( benchmark ) ;
246
- benchmark . fetchResources ( ) ;
247
230
}
248
231
249
232
async start ( ) {
@@ -336,8 +319,7 @@ class Driver {
336
319
}
337
320
}
338
321
339
- runCode ( string )
340
- {
322
+ runCode ( string ) {
341
323
if ( ! isInBrowser ) {
342
324
const scripts = string ;
343
325
let globalObject ;
@@ -387,8 +369,7 @@ class Driver {
387
369
return magicFrame ;
388
370
}
389
371
390
- prepareToRun ( )
391
- {
372
+ prepareToRun ( ) {
392
373
this . benchmarks . sort ( ( a , b ) => a . plan . name . toLowerCase ( ) < b . plan . name . toLowerCase ( ) ? 1 : - 1 ) ;
393
374
394
375
let text = "" ;
@@ -433,8 +414,7 @@ class Driver {
433
414
} ) ;
434
415
}
435
416
436
- reportError ( benchmark , error )
437
- {
417
+ reportError ( benchmark , error ) {
438
418
this . pushError ( benchmark . name , error ) ;
439
419
440
420
if ( ! isInBrowser )
@@ -459,8 +439,7 @@ class Driver {
459
439
async initialize ( ) {
460
440
if ( isInBrowser )
461
441
window . addEventListener ( "error" , ( e ) => this . pushError ( "driver startup" , e . error ) ) ;
462
- await this . prefetchResourcesForBrowser ( ) ;
463
- await this . fetchResources ( ) ;
442
+ await this . prefetchResources ( ) ;
464
443
this . prepareToRun ( ) ;
465
444
this . isReady = true ;
466
445
if ( isInBrowser ) {
@@ -471,14 +450,18 @@ class Driver {
471
450
}
472
451
}
473
452
474
- async prefetchResourcesForBrowser ( ) {
475
- if ( ! isInBrowser )
453
+ async prefetchResources ( ) {
454
+ if ( ! isInBrowser ) {
455
+ for ( const benchmark of this . benchmarks )
456
+ benchmark . prefetchResourcesForShell ( ) ;
476
457
return ;
458
+ }
477
459
460
+ // TODO: Cleanup the browser path of the preloading below and in
461
+ // `prefetchResourcesForBrowser` / `retryPrefetchResourcesForBrowser`.
478
462
const promises = [ ] ;
479
463
for ( const benchmark of this . benchmarks )
480
464
promises . push ( benchmark . prefetchResourcesForBrowser ( ) ) ;
481
-
482
465
await Promise . all ( promises ) ;
483
466
484
467
const counter = JetStream . counter ;
@@ -498,16 +481,6 @@ class Driver {
498
481
}
499
482
500
483
JetStream . loadCache = { } ; // Done preloading all the files.
501
- }
502
-
503
- async fetchResources ( ) {
504
- const promises = [ ] ;
505
- for ( const benchmark of this . benchmarks )
506
- promises . push ( benchmark . fetchResources ( ) ) ;
507
- await Promise . all ( promises ) ;
508
-
509
- if ( ! isInBrowser )
510
- return ;
511
484
512
485
const statusElement = document . getElementById ( "status" ) ;
513
486
statusElement . classList . remove ( 'loading' ) ;
@@ -623,7 +596,7 @@ class Benchmark {
623
596
this . isAsync = ! ! plan . isAsync ;
624
597
this . disabledByDefault = ! ! plan . disabledByDefault ;
625
598
this . scripts = null ;
626
- this . _resourcesPromise = null ;
599
+ this . preloads = null ;
627
600
this . _state = BenchmarkState . READY ;
628
601
}
629
602
@@ -880,8 +853,8 @@ class Benchmark {
880
853
}
881
854
882
855
prefetchResourcesForBrowser ( ) {
883
- if ( ! isInBrowser )
884
- return ;
856
+ assert ( isInBrowser ) ;
857
+
885
858
const promises = this . plan . files . map ( ( file ) => this . loadBlob ( "file" , null , file ) . then ( ( blobData ) => {
886
859
if ( ! globalThis . allIsGood )
887
860
return ;
@@ -913,6 +886,8 @@ class Benchmark {
913
886
}
914
887
915
888
async retryPrefetchResource ( type , prop , file ) {
889
+ assert ( isInBrowser ) ;
890
+
916
891
const counter = JetStream . counter ;
917
892
const blobData = JetStream . blobDataCache [ file ] ;
918
893
if ( blobData . blob ) {
@@ -947,8 +922,7 @@ class Benchmark {
947
922
}
948
923
949
924
async retryPrefetchResourcesForBrowser ( ) {
950
- if ( ! isInBrowser )
951
- return ;
925
+ assert ( isInBrowser ) ;
952
926
953
927
const counter = JetStream . counter ;
954
928
for ( const resource of this . plan . files ) {
@@ -968,33 +942,14 @@ class Benchmark {
968
942
return ! counter . failedPreloadResources && counter . loadedResources == counter . totalResources ;
969
943
}
970
944
971
- fetchResources ( ) {
972
- if ( this . _resourcesPromise )
973
- return this . _resourcesPromise ;
945
+ prefetchResourcesForShell ( ) {
946
+ assert ( ! isInBrowser ) ;
974
947
975
- this . preloads = [ ] ;
976
-
977
- if ( isInBrowser ) {
978
- this . _resourcesPromise = Promise . resolve ( ) ;
979
- return this . _resourcesPromise ;
980
- }
981
-
982
- const filePromises = this . plan . files . map ( ( file ) => fileLoader . load ( file ) ) ;
983
- this . _resourcesPromise = Promise . all ( filePromises ) . then ( ( texts ) => {
984
- if ( isInBrowser )
985
- return ;
986
- this . scripts = [ ] ;
987
- assert ( texts . length === this . plan . files . length ) ;
988
- for ( const text of texts )
989
- this . scripts . push ( text ) ;
990
- } ) ;
991
-
992
- if ( this . plan . preload ) {
993
- for ( const prop of Object . getOwnPropertyNames ( this . plan . preload ) )
994
- this . preloads . push ( [ prop , this . plan . preload [ prop ] ] ) ;
995
- }
948
+ assert ( this . scripts === null , "This initialization should be called only once." ) ;
949
+ this . scripts = this . plan . files . map ( file => fileLoader . load ( file ) ) ;
996
950
997
- return this . _resourcesPromise ;
951
+ assert ( this . preloads === null , "This initialization should be called only once." ) ;
952
+ this . preloads = Object . entries ( this . plan . preload ?? { } ) ;
998
953
}
999
954
1000
955
scoreIdentifiers ( ) { throw new Error ( "Must be implemented by subclasses" ) ; }
0 commit comments