75

Enable GPU to speed up slow Playwright tests in headless mode

 2 years ago
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 no­ticed that they ran much faster when I star­ted my browser in non-head­less mode. After some de­bug­ging, I was able to fig­ure out that the browser I was us­ing to run my tests (Chrome/​Chro­mium), auto­mat­ic­ally em­ploys a soft­ware ren­derer for WebGL when star­ted head­less. Since Sudocle heav­ily re­lies on WebGL, the tests could be­ne­fit a great deal from hard­ware ac­cel­er­a­tion.

tl;dr

To force-en­able GPU hard­ware ac­cel­er­a­tion for head­less Chrome/​Chro­mium, you have to run it with --use-gl=desktop or --use-gl=egl. You can add this para­meter 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 de­pend on the en­vir­on­ment or op­er­at­ing sys­tem you’re on. For me, on ma­cOS, egl worked well. To check if hard­ware ac­cel­er­a­tion is en­abled or not, you can use the code be­low.

Benchmark

With hard­ware ac­cel­er­a­tion en­abled, I was able to re­duce the av­er­age runtime of my tests from 1.9s to 1.3s on my Mac­Book. De­pend­ing on how many tests you run and how com­plex they are, your test suite can take sev­eral minutes less than without hard­ware ac­cel­er­a­tion.

In my case, at the time of writ­ing, I’m run­ning 823 tests with 6 work­ers. Without hard­ware ac­cel­er­a­tion, 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 fix­tures are not checked in to the source code re­pos­it­ory of Sudocle.

Checking if hardware acceleration is enabled

If you want to check if your head­less browser has GPU hard­ware ac­cel­er­a­tion en­abled or not, you can use the fol­low­ing 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 test­ing!


Posted by Michel Krämer
on January, 2nd 2022.


Next post

New major version 5.0.0 of gradle-download-task

Today, I’m happy to an­nounce the new ma­jor ver­sion 5.0.0 of the pop­u­lar gradle-down­load-task. With this Gradle plug­in, you can down­load files in your build…

Previous post

Efficient scheduling of workflow actions in the cloud

My pa­per “Ef­fi­cient Schedul­ing of Sci­entific Work­flow Ac­tions in the Cloud Based on Re­quired Cap­ab­il­it­ies” has just been pub­lished in Spring­er’s Com­mu­nic­a­tions in…

Related posts

Sudocle: A modern web app for Sudoku

I’m a huge Sudoku fan. Today, I’m re­leas­ing the first ver­sion of my new web app called Sudocle. The app has been in­spired by Crack­ing the Cryptic, the world’s most…

New major version 5.0.0 of gradle-download-task

Today, I’m happy to an­nounce the new ma­jor ver­sion 5.0.0 of the pop­u­lar gradle-down­load-task. With this Gradle plug­in, you can down­load files in your build…

Steep 5.8.0

I’ve just re­leased the new ver­sion 5.8.0 of my sci­entific work­flow man­age­ment sys­tem Steep. One of the high­lights of this ver­sion is in­creased fault tol­er­ance: Steep…


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK