NVIDIA GPU加速Chrome
最近有个项目需要在无头模式启动Chrome渲染较复杂的网页,一开始没有使用GPU机型,渲染速度比较慢。后来尝试使用NVIDIA的GPU为Chrome加速,加速效果比较明显,速度提升了将近3倍
最近有个项目需要在无头模式启动Chrome渲染较复杂的网页,一开始没有使用GPU机型,渲染速度比较慢。后来尝试使用NVIDIA的GPU为Chrome加速,加速效果比较明显,速度提升了将近3倍。本文作为总结,记录下如何使用NVIDIA GPU加速Chrome。事后总结感觉比较简单,但是因为一开始对GPU机型不了解,和一些错误的认知,整个过程并不顺利。
我使用的机型是:g4dn.2xlarge
拿到服务器已经装好了驱动、CUDA等等,这里就不详细说明了。可以使用nvidia-smi查看下显卡状态信息。
→ ~ nvidia-smi
Mon Sep 19 23:31:43 2022
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 510.85.02 Driver Version: 510.85.02 CUDA Version: 11.6 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 Tesla T4 Off | 00000000:00:1E.0 Off | 0 |
| N/A 42C P0 25W / 70W | 0MiB / 15360MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| No running processes found |
+-----------------------------------------------------------------------------+
安装浏览器
下载Chrome安装包:
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
安装Chrome和依赖
sudo yum install ./google-chrome-stable_current_x86_64.rpm
安装后Chrome地址: /opt/google/chrome/chrome
GPU Test
运行下面命令,可以将chrome://gpu所有信息生成为pdf格式:
/opt/google/chrome/chrome --no-sandbox --headless --use-gl=egl --print-to-pdf=out.pdf 'chrome://gpu'
注意:除了生成的PDF文件,这里执行过程输出的信息,对排查为什么没有GPU加速有重要作用
如果能看到下面的信息,说明GPU加速是正常的:
Puppeteer启动Chrome
Puppeteer启动参数如下:
const browser = await puppeteer.launch({
args: [
'--autoplay-policy=user-gesture-required',
'--disable-background-networking',
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
'--disable-breakpad',
'--disable-client-side-phishing-detection',
'--disable-component-update',
'--disable-default-apps',
'--disable-dev-shm-usage',
'--disable-domain-reliability',
'--disable-extensions',
'--disable-features=AudioServiceOutOfProcess,IsolateOrigins,site-per-process',
'--disable-hang-monitor',
'--disable-ipc-flooding-protection',
'--disable-offer-store-unmasked-wallet-cards',
'--disable-popup-blocking',
'--disable-print-preview',
'--disable-prompt-on-repost',
'--disable-renderer-backgrounding',
'--disable-setuid-sandbox',
'--disable-speech-api',
'--disable-sync',
'--disable-web-security',
'--disk-cache-size=33554432',
'--hide-scrollbars',
'--ignore-gpu-blocklist',
'--metrics-recording-only',
'--disable-audio-output',
'--no-default-browser-check',
'--no-first-run',
'--no-pings',
'--no-sandbox',
'--no-zygote',
'--password-store=basic',
'--enable-webgl',
'--use-mock-keychain',
'--window-size=1920,1080',
'--use-gl=egl'
],
executablePath: '/opt/google/chrome/chrome',
defaultViewport: puppeteer.defaultViewport,
headless: true,
ignoreHTTPSErrors: true,
userDataDir: '/tmp',
})
最后修改于 2022-09-19
此篇文章的评论功能已经停用。