Skip to content

Commit aa66357

Browse files
Merge pull request #9 from lightpanda-io/playwright-lightpanda
Playwright: connect over CDP
2 parents d16f832 + e93e147 commit aa66357

File tree

3 files changed

+29
-26
lines changed

3 files changed

+29
-26
lines changed

README.md

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ $ /usr/bin/time -v ./browsercore-get --dump http://127.0.0.1:1234/campfire-comme
150150
## Multiple requests using Playwright
151151

152152
We compare now multiple page loads and js evaluations using
153-
[Playwright](https://playwright.dev).
153+
[Playwright](https://playwright.dev), which connects to the browser using CDP (Chrome Debug Protocol).
154154

155155
### Dependencies
156156

@@ -162,20 +162,34 @@ dependencies, mainly Playwright.
162162
You have also to install [Google Chrome](https://www.google.com/chrome/) and
163163
Lightpanda browser, but the code is not publicly available yet.
164164

165-
### Google Chrome benchmark
165+
### Running the benchmark
166166

167-
We use Google Chrome version 123.0.6312.105.
168-
169-
The `playwright/chrome.js` benchmark accepts multiple env vars to be configured.
170-
* `CHROME_PATH` is the path to your Google Chrome bin,
171-
* `BASE_URL` is the base url of the running web reser to request, by default `http://127.0.0.1:1234`,
167+
The `playwright/cdp.js` benchmark accepts multiple env vars to be configured.
168+
* `BROWSER_ADDRESS` is the address of the running browser listening the CDP protocol, by default `http://127.0.0.1:9222`.
169+
* `BASE_URL` is the base url of the running web reser to request, by default `http://127.0.0.1:1234`.
172170
* `RUNS` is the number of pages loaded by the benchmark, default is `100`.
173171

174-
`npm run bench-chrome` starts a playwright process, load a Google Chrome
172+
`npm run bench-cdp` starts a playwright process
175173
instance and load the page to extract data 100 times.
176174

177175
```console
178-
$ CHROME_PATH=`which google-chrome` npm run bench-chrome
176+
$ npm run bench-cdp
177+
```
178+
179+
### Results
180+
181+
**Google Chrome**
182+
183+
We use Google Chrome version 123.0.6312.105.
184+
185+
You have to start the browser first.
186+
```console
187+
$ google-chrome --headless=new --disable-gpu --remote-debugging-port=9222
188+
```
189+
190+
Then you can run the benchmark.
191+
```console
192+
$ npm run bench-cdp
179193

180194
> [email protected] bench-chrome
181195
> node playwright/chrome.js

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
"description": "Lightpanda browser demo",
66
"main": "index.js",
77
"scripts": {
8-
"install-chrome": "npx playwright install chrome",
98
"ws": "go run ws/main.go",
10-
"bench-chrome": "node playwright/chrome.js"
9+
"bench-cdp": "node playwright/cdp.js"
1110
},
1211
"repository": {
1312
"type": "git",

playwright/chrome.js renamed to playwright/cdp.js

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,8 @@
1515
// Import the Chromium browser into our scraper.
1616
import { chromium } from 'playwright';
1717

18-
// options passed to the browser.
19-
let browser_options = {};
20-
21-
// chrome browser path
22-
if (process.env.CHROME_PATH) {
23-
browser_options.executablePath = process.env.CHROME_PATH;
24-
}
25-
26-
// headless
27-
if (process.env.HEADLESS) {
28-
browser_options.headless = process.env.HEADLESS === 'true';
29-
}
18+
// browserAddress
19+
const browserAddress = process.env.BROWSER_ADDRESS ? process.env.BROWSER_ADDRESS : 'http://127.0.0.1:9222';
3020

3121
// web serveur url
3222
const baseURL = process.env.BASE_URL ? process.env.BASE_URL : 'http://127.0.0.1:1234';
@@ -39,9 +29,9 @@ const gstart = process.hrtime.bigint();
3929
// store all run durations
4030
let metrics = [];
4131

42-
// Open a Chromium browser. We use headless: false
43-
// to be able to watch the browser window.
44-
const browser = await chromium.launch(browser_options);
32+
// Connect to an existing browser
33+
console.log("Connection to browser on " + browserAddress);
34+
const browser = await chromium.connectOverCDP(browserAddress);
4535

4636
for (var run = 1; run<=runs; run++) {
4737

0 commit comments

Comments
 (0)