From 99e9e53dfc9e57e3018f09235d449d55c9901d1e Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Wed, 4 Dec 2024 08:52:02 +0100 Subject: [PATCH] switch between ws and http to connect to browser --- README.md | 4 ++-- playwright/cdp.js | 2 +- puppeteer/cdp.js | 13 +++++++++---- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 69127be..74b52a4 100644 --- a/README.md +++ b/README.md @@ -169,7 +169,7 @@ Lightpanda browser, but the code is not publicly available yet. ### Running the benchmark The `puppeteer/cdp.js` benchmark accepts multiple env vars to be configured. -* `BROWSER_ADDRESS` is the address of the running browser listening the CDP protocol, by default `http://127.0.0.1:9222`. +* `BROWSER_ADDRESS` is the address of the running browser listening the CDP protocol, by default `ws://127.0.0.1:9222`. * `BASE_URL` is the base url of the running web reser to request, by default `http://127.0.0.1:1234`. * `RUNS` is the number of pages loaded by the benchmark, default is `100`. @@ -193,7 +193,7 @@ $ /usr/bin/time -v google-chrome --headless=new --remote-debugging-port=9222 Then you can run the benchmark. ```console -$ npm run bench-puppeteer-cdp +$ BROWSER_ADDRESS=http://127.0.0.1:9222 npm run bench-puppeteer-cdp > demo@1.0.0 bench-puppeteer-cdp > node puppeteer/cdp.js diff --git a/playwright/cdp.js b/playwright/cdp.js index fff31cd..39ae6dc 100644 --- a/playwright/cdp.js +++ b/playwright/cdp.js @@ -16,7 +16,7 @@ import { chromium } from 'playwright'; // browserAddress -const browserAddress = process.env.BROWSER_ADDRESS ? process.env.BROWSER_ADDRESS : 'http://127.0.0.1:9222'; +const browserAddress = process.env.BROWSER_ADDRESS ? process.env.BROWSER_ADDRESS : 'ws://127.0.0.1:9222'; // web serveur url const baseURL = process.env.BASE_URL ? process.env.BASE_URL : 'http://127.0.0.1:1234'; diff --git a/puppeteer/cdp.js b/puppeteer/cdp.js index e5cef34..9955c4c 100644 --- a/puppeteer/cdp.js +++ b/puppeteer/cdp.js @@ -16,7 +16,7 @@ import puppeteer from 'puppeteer-core'; // ws address -const browserAddress = process.env.BROWSER_ADDRESS ? process.env.BROWSER_ADDRESS : 'http://127.0.0.1:9222'; +const browserAddress = process.env.BROWSER_ADDRESS ? process.env.BROWSER_ADDRESS : 'ws://127.0.0.1:9222'; // web serveur url const baseURL = process.env.BASE_URL ? process.env.BASE_URL : 'http://127.0.0.1:1234'; @@ -31,9 +31,14 @@ let metrics = []; (async () => { // Connect to the browser and open a new blank page - const browser = await puppeteer.connect({ - browserURL: browserAddress, - }); + let opts = {}; + if (browserAddress.substring(0, 5) == 'ws://') { + opts.browserWSEndpoint = browserAddress; + } else { + opts.browserURL = browserAddress; + } + + const browser = await puppeteer.connect(opts); for (var run = 0; run