Member-only story
Use Headless Chrome and Docker to run GPU Computations
Learn how to interact with a website using headless Chrome, simple JavaScript, and Docker containers.
You might need to visit a website at some point programmatically. It can be helpful for different reasons. Here are a few examples:
- 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.
- Collect server-side renderings.
- Generate website screenshots.
- Test chrome extensions.
- Integrate this process into your testing pipeline. You might get away with using testing frameworks such as Selenium and Cypress.
- Get more refined control over the browser programmatically; for example, force the GPU usage for graphics computations, enable different renderers, draft extensions, etc.
- 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…