Skip to content

fix: workerInitializationDelay is not hardcoded - #5676

Open
kobenguyent wants to merge 1 commit into
4.xfrom
fix-delayed-in-initialzation-workers
Open

fix: workerInitializationDelay is not hardcoded#5676
kobenguyent wants to merge 1 commit into
4.xfrom
fix-delayed-in-initialzation-workers

Conversation

@kobenguyent

@kobenguyent kobenguyent commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

Motivation/Description of the PR

The standard solutions to the "Thundering Herd" problem with concurrent browser initialization. The previous implementation had a couple of architectural flaws:

  1. Massive CPU Spikes: By instantly spinning up 25 Node.js workers inside a for loop in the master process, the system had to simultaneously create 25 isolated V8 instances and start parsing files for all of them at the exact
    same millisecond. This causes a massive CPU usage spike which can starve the main process and actually delay everything downstream.
  2. Artificial Delay in Worker: Relying on the workerIndex * delay inside the worker script is generally seen as a band-aid. The V8 instances are already fighting for resources while they evaluate their file imports, only to hit
    an arbitrary setTimeout when they reach the code execution phase.

A Better Implementation: Master-Coordinated Staggering

The standard practice in large-scale concurrent runners (like Playwright test and Jest worker-pools) is to stagger the creation of the worker threads themselves in the master process or use a bounded connection pool.

by removing the artificial delay logic from the worker file altogether and instead moved a 200ms delay directly into the worker-creation loop inside lib/workers.js.
• The CPU spike from spawning V8 instances is drastically smoothed out.

Why this is better:

• The master process now creates the first worker, waits 200ms, then creates the second worker, and so on.
• The browser initialization is naturally staggered because each worker starts its entire lifecycle (including imports and setups) exactly 200ms behind the previous one.
• You no longer have hacky math based on workerIndex inside your test runner script.

Users can now specify workerInitializationDelay: in their codecept.conf.js root configuration. If they omit it, it defaults to 200ms, and setting it to 0 will disable the delay entirely.

Here's an example of how a user would use this in their codecept.conf.js:

    export const config = {                                                                                                                                                                                                           
      tests: './tests/*_test.js',                                                                                                                                                                                                     
      output: './output',                                                                                                                                                                                                             
      // Custom initialization delay between spinning up workers (in milliseconds)                                                                                                                                                    
      workerInitializationDelay: 500,                                                                                                                                                                                                 
                                                                                                                                                                                                                                      
      helpers: {                                                                                                                                                                                                                      
        Playwright: {                                                                                                                                                                                                                 
          url: 'http://localhost',                                                                                                                                                                                                    
          show: false,                                                                                                                                                                                                                
          browser: 'chromium'                                                                                                                                                                                                         
        }                                                                                                                                                                                                                             
      },                                                                                                                                                                                                                              
      // ...                                                                                                                                                                                                                          
    }     

Type of change

  • 🔥 Breaking changes
  • 🚀 New functionality
  • 🐛 Bug fix
  • 🧹 Chore
  • 📋 Documentation changes/updates
  • ♨️ Hot fix
  • 🔨 Markdown files fix - not related to source code
  • 💅 Polish code

Checklist:

  • Tests have been added
  • Documentation has been added (Run npm run docs)
  • Lint checking (Run npm run lint)
  • Local tests are passed (Run npm test)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant