Enable GPU to speed up slow Playwright tests in headless mode
source link: https://michelkraemer.com/enable-gpu-for-slow-playwright-tests-in-headless-mode/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Enable GPU to speed up slow Playwright tests in headless mode
When I wrote end-to-end tests for Sudocle the other day, I noticed that they ran much faster when I started my browser in non-headless mode. After some debugging, I was able to figure out that the browser I was using to run my tests (Chrome/Chromium), automatically employs a software renderer for WebGL when started headless. Since Sudocle heavily relies on WebGL, the tests could benefit a great deal from hardware acceleration.
tl;dr
To force-enable GPU hardware acceleration for headless Chrome/Chromium, you have to run it with --use-gl=desktop
or --use-gl=egl
. You can add this parameter in your playwright.config.js
file:
const config = {
use: {
channel: "chrome",
launchOptions: {
// force GPU hardware acceleration
// (even in headless mode)
args: ["--use-gl=egl"]
}
}
}
module.exports = config
Whether you need to use desktop
or egl
, seems to depend on the environment or operating system you’re on. For me, on macOS, egl
worked well. To check if hardware acceleration is enabled or not, you can use the code below.
Benchmark
With hardware acceleration enabled, I was able to reduce the average runtime of my tests from 1.9s to 1.3s on my MacBook. Depending on how many tests you run and how complex they are, your test suite can take several minutes less than without hardware acceleration.
In my case, at the time of writing, I’m running 823 tests with 6 workers. Without hardware acceleration, the test suite took 4m 19s. Now, it only takes 2m 59s.
You can find the source code of my tests here:
https://github.com/michel-kraemer/sudocle/blob/main/tests/Grid.spec.js
My playwright.config.js
can be found here:
https://github.com/michel-kraemer/sudocle/blob/main/playwright.config.js
Please note that the test fixtures are not checked in to the source code repository of Sudocle.
Checking if hardware acceleration is enabled
If you want to check if your headless browser has GPU hardware acceleration enabled or not, you can use the following test:
test("GPU hardware acceleration", async ({ page }) => {
await page.goto("chrome://gpu")
let featureStatusList = page.locator(".feature-status-list")
await expect(featureStatusList).toContainText("Hardware accelerated")
})
Happy testing!
Next post
New major version 5.0.0 of gradle-download-task
Today, I’m happy to announce the new major version 5.0.0 of the popular gradle-download-task. With this Gradle plugin, you can download files in your build…
Previous post
Efficient scheduling of workflow actions in the cloud
My paper “Efficient Scheduling of Scientific Workflow Actions in the Cloud Based on Required Capabilities” has just been published in Springer’s Communications in…
Related posts
Sudocle: A modern web app for Sudoku
I’m a huge Sudoku fan. Today, I’m releasing the first version of my new web app called Sudocle. The app has been inspired by Cracking the Cryptic, the world’s most…
New major version 5.0.0 of gradle-download-task
Today, I’m happy to announce the new major version 5.0.0 of the popular gradle-download-task. With this Gradle plugin, you can download files in your build…
Steep 5.8.0
I’ve just released the new version 5.8.0 of my scientific workflow management system Steep. One of the highlights of this version is increased fault tolerance: Steep…
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK