@@ -187,6 +187,16 @@ class BrowserPool {
187187 private _promise : DeferPromise < void > | undefined
188188 private _providedContext : string | undefined
189189
190+ private _scaling : Promise < void > | undefined
191+ // EMA of how long a single file takes in this pool; undefined until the
192+ // first file finishes, which means "no signal yet — keep opening sessions"
193+ private _fileCostEma : number | undefined
194+ // refined with the real duration after every session open
195+ private _sessionOpenCost = 250
196+ // a session's first file pays the tester bootstrap on top of the test
197+ // itself, so it would wildly overestimate the steady per-file cost
198+ private _warmedUpSessions = new Set < string > ( )
199+
190200 private readySessions : Set < string >
191201
192202 private _traces : Traces
@@ -250,37 +260,85 @@ class BrowserPool {
250260 return this . _promise
251261 }
252262
253- // open the minimum amount of tabs
254- // if there is only 1 file running, we don't need 8 tabs running
255- const workerCount = Math . min (
256- this . options . maxWorkers - this . orchestrators . size ,
257- files . length ,
258- )
259-
260- const promises : Promise < void > [ ] = [ ]
261- for ( let i = 0 ; i < workerCount ; i ++ ) {
262- const sessionId = crypto . randomUUID ( )
263- this . project . vitest . _browserSessions . sessionIds . add ( sessionId )
264- const project = this . project . name
265- debug ?.( '[%s] creating session for %s' , sessionId , project )
266- const page = this . _traces . $ (
267- `vitest.browser.open` ,
268- {
269- context : this . _otel . context ,
270- attributes : {
271- 'vitest.browser.session_id' : sessionId ,
263+ this . scaleSessions ( method )
264+ return this . _promise
265+ }
266+
267+ // Sessions are opened one by one while the queue justifies another tab
268+ // instead of `maxWorkers` tabs upfront: every tab pays a context + page +
269+ // full module graph bring-up that competes with already-running sessions
270+ // for the same Vite server, so for fast suites fewer tabs finish sooner.
271+ private scaleSessions ( method : 'run' | 'collect' ) : void {
272+ if ( this . _scaling ) {
273+ return
274+ }
275+ this . _scaling = ( async ( ) => {
276+ while (
277+ this . _queue . length
278+ && this . orchestrators . size < this . options . maxWorkers
279+ && this . shouldOpenAnotherSession ( )
280+ ) {
281+ const sessionId = crypto . randomUUID ( )
282+ this . project . vitest . _browserSessions . sessionIds . add ( sessionId )
283+ debug ?.( '[%s] creating session for %s' , sessionId , this . project . name )
284+ const openStart = performance . now ( )
285+ await this . _traces . $ (
286+ `vitest.browser.open` ,
287+ {
288+ context : this . _otel . context ,
289+ attributes : {
290+ 'vitest.browser.session_id' : sessionId ,
291+ } ,
272292 } ,
273- } ,
274- ( ) => this . openPage ( sessionId , { parallel : workerCount > 1 } ) ,
275- ) . then ( ( ) => {
276- // start running tests on the page when it's ready
277- this . runNextTest ( method , sessionId )
293+ ( ) => this . openPage ( sessionId , {
294+ parallel : this . options . maxWorkers > 1
295+ && this . orchestrators . size + this . _queue . length > 1 ,
296+ } ) ,
297+ )
298+ this . _sessionOpenCost = performance . now ( ) - openStart
299+ // start running tests on the page when it's ready; a failure here
300+ // already took a file off the queue, so it can never be swallowed
301+ try {
302+ this . runNextTest ( method , sessionId )
303+ }
304+ catch ( error ) {
305+ this . reject ( error as Error )
306+ return
307+ }
308+ }
309+ debug ?.( 'finished scaling sessions, %s sessions are running' , this . orchestrators . size )
310+ } ) ( )
311+ this . _scaling
312+ . catch ( ( error ) => {
313+ // a failure to open an extra session when the queue is already
314+ // drained should not fail the run: the sessions that are still
315+ // running have their own error and timeout handling
316+ if ( ! this . _queue . length && this . orchestrators . size > 0 ) {
317+ debug ?.( 'failed to open an extra session, ignoring: %s' , error )
318+ return
319+ }
320+ this . reject ( error as Error )
278321 } )
279- promises . push ( page )
322+ . finally ( ( ) => {
323+ this . _scaling = undefined
324+ // completion might have been blocked by the in-flight scaling
325+ this . checkCompletion ( )
326+ } )
327+ }
328+
329+ private shouldOpenAnotherSession ( ) : boolean {
330+ // the first session always opens; without a per-file signal
331+ // keep the old behavior of scaling up to maxWorkers
332+ if ( this . orchestrators . size === 0 || this . _fileCostEma == null ) {
333+ return true
280334 }
281- await Promise . all ( promises )
282- debug ?.( 'all sessions are created' )
283- return this . _promise
335+ // only pay for another tab when the remaining work, split across the
336+ // sessions we already have, still takes considerably longer than opening
337+ // a tab costs — a new tab does not just cost its own bring-up, it also
338+ // competes with the running sessions for the same Vite server
339+ const projectedDrainMs
340+ = ( this . _queue . length * this . _fileCostEma ) / this . orchestrators . size
341+ return projectedDrainMs > Math . max ( this . _sessionOpenCost , 100 ) * 2
284342 }
285343
286344 private async openPage ( sessionId : string , options : { parallel : boolean } ) : Promise < void > {
@@ -328,14 +386,7 @@ class BrowserPool {
328386 private finishSession ( sessionId : string ) : void {
329387 this . readySessions . add ( sessionId )
330388
331- // the last worker finished running tests
332- if ( this . readySessions . size === this . orchestrators . size ) {
333- this . _otel . span . end ( )
334- this . _promise ?. resolve ( )
335- this . _promise = undefined
336- debug ?.( '[%s] all tests finished running' , sessionId )
337- }
338- else {
389+ if ( ! this . checkCompletion ( ) ) {
339390 debug ?.(
340391 `did not finish sessions for ${ sessionId } : |ready - %s| |overall - %s|` ,
341392 [ ...this . readySessions ] . join ( ', ' ) ,
@@ -344,6 +395,23 @@ class BrowserPool {
344395 }
345396 }
346397
398+ private checkCompletion ( ) : boolean {
399+ // the run already finished (or was rejected) — nothing to resolve
400+ if ( ! this . _promise ) {
401+ return false
402+ }
403+ // the last worker finished running tests; a session that is still
404+ // opening (this._scaling) will call this again once it settles
405+ if ( ! this . _scaling && this . readySessions . size === this . orchestrators . size ) {
406+ this . _otel . span . end ( )
407+ this . _promise . resolve ( )
408+ this . _promise = undefined
409+ debug ?.( 'all tests finished running' )
410+ return true
411+ }
412+ return false
413+ }
414+
347415 private runNextTest ( method : 'run' | 'collect' , sessionId : string ) : void {
348416 const file = this . _queue . shift ( )
349417
@@ -373,7 +441,14 @@ class BrowserPool {
373441 const orchestrator = this . getOrchestrator ( sessionId )
374442 debug ?.( '[%s] run test %s' , sessionId , file )
375443
444+ // warm the transform cache while the iframe is booting so the test
445+ // file import doesn't wait for the transform; mirrors the URL the
446+ // tester will request (see `importFile` in the browser runner)
447+ const fileUrl = `/${ / ^ \w : / . test ( file . filepath ) ? '@fs/' : '' } ${ file . filepath } ` . replace ( / \/ + / g, '/' )
448+ void this . project . vite . transformRequest ( fileUrl ) . catch ( ( ) => { } )
449+
376450 this . setBreakpoint ( sessionId , file . filepath ) . then ( ( ) => {
451+ const fileStart = performance . now ( )
377452 // this starts running tests inside the orchestrator
378453 const testersPromise = this . _traces . $ (
379454 `vitest.browser.run` ,
@@ -403,6 +478,15 @@ class BrowserPool {
403478 )
404479 testersPromise
405480 . then ( ( ) => {
481+ if ( this . _warmedUpSessions . has ( sessionId ) ) {
482+ const fileCost = performance . now ( ) - fileStart
483+ this . _fileCostEma = this . _fileCostEma == null
484+ ? fileCost
485+ : this . _fileCostEma * 0.7 + fileCost * 0.3
486+ }
487+ else {
488+ this . _warmedUpSessions . add ( sessionId )
489+ }
406490 debug ?.( '[%s] test %s finished running' , sessionId , file )
407491 this . runNextTest ( method , sessionId )
408492 } )
0 commit comments