JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Follow publication

Member-only story

Use Headless Chrome and Docker to run GPU Computations

Antriani
JavaScript in Plain English
4 min readOct 26, 2021

Photo by Kevin Bhagat on Unsplash

You might need to visit a website at some point programmatically. It can be helpful for different reasons. Here are a few examples:

  1. Test quickly and accurately a website’s behavior without refreshing or checking manually. You can automate interactions with the website such as buttons clicks, form submissions, UI testing, etc.
  2. Collect server-side renderings.
  3. Generate website screenshots.
  4. Test chrome extensions.
  5. Integrate this process into your testing pipeline. You might get away with using testing frameworks such as Selenium and Cypress.
  6. Get more refined control over the browser programmatically; for example, force the GPU usage for graphics computations, enable different renderers, draft extensions, etc.
  7. Craft your tooling based on website UI content.

There are three main pieces to the setup: puppeteer, a docker container that is not mandatory, and the website you want to visit.

I tested the below code on Ubuntu 20.04 with Nvidia Tesla GPU.

Puppeteer

Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. Puppeteer runs headless by default, but can be configured to run full (non-headless) Chrome or Chromium.

The first step is to include puppeteer and define the required args.

Make sure you use the headless option. Note that on Mac currently, even in headless mode, it does launch an instance and hides it quickly after, so performance on that platform is questionable.

--headless

It’s also essential to select which GL the GPU process should use. Options are:

  • desktop: whatever desktop OpenGL the user has installed (Linux and Mac default).
  • egl: whatever EGL / GLES2 the user has installed (Windows default — actually ANGLE).
  • swiftshader: The…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Published in JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Write a response

Hello, first of all, thank you for sharing theses informations.
I followed your article, in a little different way but in same spirit and i m struggling to "enable hardware acceleration" into chrome.
chrome://gpu still show that hardware acceleration…

--

Have you tried the same setup with screen recording?

--

Another option is Selenium. I use the Python flavor and it is simply amazing. Since Python can be operated in interactive mode, it's easy to change things on the fly. Good to know there are other options out there, though. Thanks!

--