Skip to content

Commit 4506bbb

Browse files
francisbouvierkrichprollsch
authored andcommitted
Playwright: connect over CDP
Signed-off-by: Francis Bouvier <[email protected]>
1 parent d16f832 commit 4506bbb

File tree

3 files changed

+88
-20
lines changed

3 files changed

+88
-20
lines changed

README.md

Lines changed: 34 additions & 7 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,28 @@ 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
166-
167-
We use Google Chrome version 123.0.6312.105.
165+
### Running the benchmark
168166

169167
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`,
168+
* `BROWSER_PATH` is the path to your browser implementing the CDP protocol. It can be either the path to a local binary or an URL (host:port) of a running browser. Default value is empty, which will launch the Google Chrome installed through Playwright.
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

174172
`npm run bench-chrome` starts a playwright process, load a Google Chrome
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+
$ BROWSER_PATH=127.0.0.1:9222 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+
```console
186+
$ npm run bench-cdp
179187

180188
> [email protected] bench-chrome
181189
> node playwright/chrome.js
@@ -190,3 +198,22 @@ max run duration (ms) 323
190198
```
191199

192200
![aws.m5 Playwright with Google Chrome](./img/aws_m5_playwright_chrome.png)
201+
202+
**Lightpanda***
203+
204+
Current version (commit X).
205+
206+
```console
207+
$ npm run bench-cdp
208+
209+
> [email protected] bench-chrome
210+
> node playwright/chrome.js
211+
212+
................................................................................
213+
....................
214+
total runs 100
215+
total duration (ms) 18792
216+
avg run duration (ms) 184
217+
min run duration (ms) 168
218+
max run duration (ms) 323
219+
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"scripts": {
88
"install-chrome": "npx playwright install chrome",
99
"ws": "go run ws/main.go",
10-
"bench-chrome": "node playwright/chrome.js"
10+
"bench-cdp": "node playwright/cdp.js"
1111
},
1212
"repository": {
1313
"type": "git",

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

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,39 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14+
import fs from 'fs';
1415

1516
// Import the Chromium browser into our scraper.
1617
import { chromium } from 'playwright';
1718

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;
19+
// check if browser path if a local path or an URL
20+
let browserPath = process.env.BROWSER_PATH;
21+
let networkPath;
22+
if (browserPath) {
23+
24+
// not local path
25+
if (!fs.existsSync(browserPath)) {
26+
if (!browserPath.startsWith("http://")) {
27+
browserPath = "http://" + browserPath
28+
}
29+
const url = new URL(browserPath);
30+
networkPath = url.host;
31+
}
2432
}
2533

26-
// headless
27-
if (process.env.HEADLESS) {
28-
browser_options.headless = process.env.HEADLESS === 'true';
34+
// options passed to the browser
35+
let browserOptions = {};
36+
if (!networkPath) {
37+
38+
// chrome browser path
39+
if (browserPath) {
40+
browserOptions.executablePath = browserPath;
41+
}
42+
43+
// headless
44+
if (process.env.HEADLESS) {
45+
browserOptions.headless = process.env.HEADLESS === 'true';
46+
}
2947
}
3048

3149
// web serveur url
@@ -39,9 +57,32 @@ const gstart = process.hrtime.bigint();
3957
// store all run durations
4058
let metrics = [];
4159

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);
60+
let browser;
61+
if (networkPath) {
62+
63+
// Connect to an existing browser
64+
console.log("Connection to browser on " + networkPath + "...");
65+
66+
const resp = await fetch("http://" + networkPath + "/json/version");
67+
const version = await resp.json()
68+
const wsURL = version.webSocketDebuggerUrl;
69+
70+
browser = await chromium.connectOverCDP(wsURL);
71+
72+
} else {
73+
74+
// Launching a new browser
75+
if (browserPath) {
76+
console.log("Launching browser " + browserPath);
77+
} else {
78+
console.log("Launching browser");
79+
}
80+
81+
// We use headless: false
82+
// to be able to watch the browser window.
83+
browser = await chromium.launch(browserOptions);
84+
85+
}
4586

4687
for (var run = 1; run<=runs; run++) {
4788

0 commit comments

Comments
 (0)