Skip to content

Commit 791a85a

Browse files
committed
add puppeteer script
1 parent e29ffdf commit 791a85a

File tree

2 files changed

+121
-1
lines changed

2 files changed

+121
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"main": "index.js",
77
"scripts": {
88
"ws": "go run ws/main.go",
9-
"bench-cdp": "node playwright/cdp.js"
9+
"bench-playwright-cdp": "node playwright/cdp.js",
10+
"bench-puppeteer-cdp": "node puppeteer/cdp.js"
1011
},
1112
"repository": {
1213
"type": "git",

puppeteer/cdp.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright 2023-2024 Lightpanda (Selecy SAS)
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
'use scrict'
15+
16+
import puppeteer from 'puppeteer-core';
17+
18+
// ws address
19+
const browserAddress = process.env.BROWSER_ADDRESS ? process.env.BROWSER_ADDRESS : 'http://127.0.0.1:9222';
20+
21+
// web serveur url
22+
const baseURL = process.env.BASE_URL ? process.env.BASE_URL : 'http://127.0.0.1:1234';
23+
24+
// runs
25+
const runs = process.env.RUNS ? parseInt(process.env.RUNS) : 100;
26+
27+
const executablePath = process.env.CHROME_PATH;
28+
29+
// measure general time.
30+
const gstart = process.hrtime.bigint();
31+
// store all run durations
32+
let metrics = [];
33+
34+
(async () => {
35+
// Launch the browser and open a new blank page
36+
const browser = await puppeteer.launch({
37+
executablePath: executablePath,
38+
browserURL: browserAddress,
39+
});
40+
41+
for (var run = 1; run<=runs; run++) {
42+
// measure run time.
43+
const rstart = process.hrtime.bigint();
44+
45+
const context = await browser.createBrowserContext();
46+
const page = await context.newPage();
47+
48+
// Navigate the page to a URL
49+
await page.goto(baseURL + '/campfire-commerce');
50+
51+
// ensure the price is loaded.
52+
await page.waitForFunction(() => {
53+
const price = document.querySelector('#product-price');
54+
return price.textContent.length > 0;
55+
});
56+
57+
58+
// ensure the reviews are loaded.
59+
await page.waitForFunction(() => {
60+
const reviews = document.querySelectorAll('#product-reviews > div');
61+
return reviews.length > 0;
62+
});
63+
64+
let res = {};
65+
66+
res.name = await page.evaluate(() => { return document.querySelector('#product-name').textContent; });
67+
res.price = parseFloat(await page.evaluate(() => { return document.querySelector('#product-price').textContent.substring(1); }));
68+
res.description = await page.evaluate(() => { return document.querySelector('#product-description').textContent; });
69+
res.features = await page.evaluate(() => { return document.querySelector('#product-features > li').allTextContents; });
70+
res.image = await page.evaluate(() => { return document.querySelector('#product-image').getAttribute('src'); });
71+
72+
const related = await page.evaluate(() => {
73+
return Array.from(document.querySelectorAll('#product-related > div')).map(row => {
74+
return {
75+
name: row.querySelector('h4').textContent,
76+
price: parseFloat((row.querySelector('p').textContent).substring(1)),
77+
image: row.querySelector('img').getAttribute('src'),
78+
};
79+
});
80+
});
81+
res.related = related;
82+
83+
const reviews = await page.evaluate(() => {
84+
return Array.from(document.querySelectorAll('#product-reviews > div')).map(row => {
85+
return {
86+
name: row.querySelector('h4').textContent,
87+
text: row.querySelector('p').textContent,
88+
};
89+
});
90+
});
91+
res.reviews = reviews;
92+
93+
//console.log(res);
94+
95+
process.stderr.write('.');
96+
if(run % 80 == 0) process.stderr.write('\n');
97+
98+
await page.close();
99+
await context.close();
100+
101+
metrics[run] = process.hrtime.bigint() - rstart;
102+
}
103+
104+
await browser.close();
105+
106+
const gduration = process.hrtime.bigint() - gstart;
107+
108+
process.stderr.write('\n');
109+
110+
const avg = metrics.reduce((s, a) => s += a) / BigInt(metrics.length);
111+
const min = metrics.reduce((s, a) => a < s ? a : s);
112+
const max = metrics.reduce((s, a) => a > s ? a : s);
113+
114+
console.log('total runs', runs);
115+
console.log('total duration (ms)', (gduration/1000000n).toString());
116+
console.log('avg run duration (ms)', (avg/1000000n).toString());
117+
console.log('min run duration (ms)', (min/1000000n).toString());
118+
console.log('max run duration (ms)', (max/1000000n).toString());
119+
})();

0 commit comments

Comments
 (0)