In a playwright + typescript project, we use the following config line:
workers: process.env.CI ? 1 : undefined,
The undefined
value allows local users to execute faster with X parallel workers on computers having 2*X CPU cores (see https://learn.microsoft.com/en-us/azure/playwright-testing/concept-determine-optimal-configuration ).
This is great, we want to keep this flexibility, but at some point in the code, we also need to know how many max workers the current execution can support.
I see ways to get a worker index like process.env.TEST_WORKER_INDEX
, but is there a way to know how many max workers my machine is capable of? playwrightConfig.workers
returns undefined
of course :)
By default, it's half of the number of logical CPU cores - os.cpus().length / 2
. You can get it from the test, even if you set undefined in playwrightConfig.workers.
import test from "@playwright/test";
test("Example", async ({}, testInfo) => {
console.log(testInfo.config.workers);
});
You can also tune your execution by setting the percentage of cores - workers: "75%"
. (again, 50% by default)