@@ -181,47 +181,27 @@ function uiFriendlyDuration(time) {
181181 return `${ time . toFixed ( 3 ) } ms` ;
182182}
183183
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.
184187const fileLoader = ( function ( ) {
185188 class Loader {
186189 constructor ( ) {
187190 this . requests = new Map ;
188191 }
189192
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 ) ;
193197
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 ) ) {
220199 return this . requests . get ( url ) ;
200+ }
221201
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 ;
225205 }
226206 }
227207 return new Loader ;
@@ -233,6 +213,8 @@ class Driver {
233213 this . isDone = false ;
234214 this . errors = [ ] ;
235215 this . benchmarks = [ ] ;
216+ // TODO: Cleanup / remove / merge `blobDataCache` and `loadCache` vs.
217+ // the global `fileLoader` cache.
236218 this . blobDataCache = { } ;
237219 this . loadCache = { } ;
238220 this . counter = { } ;
@@ -241,9 +223,10 @@ class Driver {
241223 this . counter . failedPreloadResources = 0 ;
242224 }
243225
226+ // TODO: Remove, make `this.benchmarks` immutable and set it once in the
227+ // ctor instead of this and the global `addBenchmarksBy*` functions.
244228 addBenchmark ( benchmark ) {
245229 this . benchmarks . push ( benchmark ) ;
246- benchmark . fetchResources ( ) ;
247230 }
248231
249232 async start ( ) {
@@ -336,8 +319,7 @@ class Driver {
336319 }
337320 }
338321
339- runCode ( string )
340- {
322+ runCode ( string ) {
341323 if ( ! isInBrowser ) {
342324 const scripts = string ;
343325 let globalObject ;
@@ -387,8 +369,7 @@ class Driver {
387369 return magicFrame ;
388370 }
389371
390- prepareToRun ( )
391- {
372+ prepareToRun ( ) {
392373 this . benchmarks . sort ( ( a , b ) => a . plan . name . toLowerCase ( ) < b . plan . name . toLowerCase ( ) ? 1 : - 1 ) ;
393374
394375 let text = "" ;
@@ -433,8 +414,7 @@ class Driver {
433414 } ) ;
434415 }
435416
436- reportError ( benchmark , error )
437- {
417+ reportError ( benchmark , error ) {
438418 this . pushError ( benchmark . name , error ) ;
439419
440420 if ( ! isInBrowser )
@@ -459,8 +439,7 @@ class Driver {
459439 async initialize ( ) {
460440 if ( isInBrowser )
461441 window . addEventListener ( "error" , ( e ) => this . pushError ( "driver startup" , e . error ) ) ;
462- await this . prefetchResourcesForBrowser ( ) ;
463- await this . fetchResources ( ) ;
442+ await this . prefetchResources ( ) ;
464443 this . prepareToRun ( ) ;
465444 this . isReady = true ;
466445 if ( isInBrowser ) {
@@ -471,14 +450,18 @@ class Driver {
471450 }
472451 }
473452
474- async prefetchResourcesForBrowser ( ) {
475- if ( ! isInBrowser )
453+ async prefetchResources ( ) {
454+ if ( ! isInBrowser ) {
455+ for ( const benchmark of this . benchmarks )
456+ benchmark . prefetchResourcesForShell ( ) ;
476457 return ;
458+ }
477459
460+ // TODO: Cleanup the browser path of the preloading below and in
461+ // `prefetchResourcesForBrowser` / `retryPrefetchResourcesForBrowser`.
478462 const promises = [ ] ;
479463 for ( const benchmark of this . benchmarks )
480464 promises . push ( benchmark . prefetchResourcesForBrowser ( ) ) ;
481-
482465 await Promise . all ( promises ) ;
483466
484467 const counter = JetStream . counter ;
@@ -498,16 +481,6 @@ class Driver {
498481 }
499482
500483 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 ;
511484
512485 const statusElement = document . getElementById ( "status" ) ;
513486 statusElement . classList . remove ( 'loading' ) ;
@@ -623,6 +596,7 @@ class Benchmark {
623596 this . isAsync = ! ! plan . isAsync ;
624597 this . disabledByDefault = ! ! plan . disabledByDefault ;
625598 this . scripts = null ;
599+ this . preloads = null ;
626600 this . _resourcesPromise = null ;
627601 this . _state = BenchmarkState . READY ;
628602 }
@@ -888,8 +862,8 @@ class Benchmark {
888862 }
889863
890864 prefetchResourcesForBrowser ( ) {
891- if ( ! isInBrowser )
892- return ;
865+ assert ( isInBrowser ) ;
866+
893867 const promises = this . plan . files . map ( ( file ) => this . loadBlob ( "file" , null , file ) . then ( ( blobData ) => {
894868 if ( ! globalThis . allIsGood )
895869 return ;
@@ -921,6 +895,8 @@ class Benchmark {
921895 }
922896
923897 async retryPrefetchResource ( type , prop , file ) {
898+ assert ( isInBrowser ) ;
899+
924900 const counter = JetStream . counter ;
925901 const blobData = JetStream . blobDataCache [ file ] ;
926902 if ( blobData . blob ) {
@@ -955,8 +931,7 @@ class Benchmark {
955931 }
956932
957933 async retryPrefetchResourcesForBrowser ( ) {
958- if ( ! isInBrowser )
959- return ;
934+ assert ( isInBrowser ) ;
960935
961936 const counter = JetStream . counter ;
962937 for ( const resource of this . plan . files ) {
@@ -976,33 +951,14 @@ class Benchmark {
976951 return ! counter . failedPreloadResources && counter . loadedResources == counter . totalResources ;
977952 }
978953
979- fetchResources ( ) {
980- if ( this . _resourcesPromise )
981- return this . _resourcesPromise ;
954+ prefetchResourcesForShell ( ) {
955+ assert ( ! isInBrowser ) ;
982956
983- this . preloads = [ ] ;
984-
985- if ( isInBrowser ) {
986- this . _resourcesPromise = Promise . resolve ( ) ;
987- return this . _resourcesPromise ;
988- }
989-
990- const filePromises = this . plan . files . map ( ( file ) => fileLoader . load ( file ) ) ;
991- this . _resourcesPromise = Promise . all ( filePromises ) . then ( ( texts ) => {
992- if ( isInBrowser )
993- return ;
994- this . scripts = [ ] ;
995- assert ( texts . length === this . plan . files . length ) ;
996- for ( const text of texts )
997- this . scripts . push ( text ) ;
998- } ) ;
999-
1000- if ( this . plan . preload ) {
1001- for ( const prop of Object . getOwnPropertyNames ( this . plan . preload ) )
1002- this . preloads . push ( [ prop , this . plan . preload [ prop ] ] ) ;
1003- }
957+ assert ( this . scripts === null , "This initialization should be called only once." ) ;
958+ this . scripts = this . plan . files . map ( file => fileLoader . load ( file ) ) ;
1004959
1005- return this . _resourcesPromise ;
960+ assert ( this . preloads === null , "This initialization should be called only once." ) ;
961+ this . preloads = Object . entries ( this . plan . preload ?? { } ) ;
1006962 }
1007963
1008964 static scoreDescription ( ) { throw new Error ( "Must be implemented by subclasses." ) ; }
0 commit comments