我正在尝试使用 puppeteer 来测量一组网站在我的环境中加载的速度。我关注的是网络连接的质量和网络速度,所以我很高兴知道当浏览器下载所有图像和 html 时,页面加载(对于加载的外行定义)所需的时间。
通过使用 puppeteer,我可以重复运行测试并精确测量加载时间的差异。
我可以看到
64.0.3240.0 (r508693)
page.getMetrics
和 event: 'metrics'
已经落地,这应该可以帮助我找到我想要的东西。
但是作为 Node 和 js 的新手,我不确定如何阅读
page.getMetrics
以及哪些不同的键/值对在我的上下文中提供了有用的信息。
我目前读取指标的可悲尝试如下:
const puppeteer = require('puppeteer');
async function run() {
const browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']});
const page = await browser.newPage();
page.on('load', () => console.log("Loaded: " + page.url()));
await page.goto('https://google.com');
const metrics = page.getMetrics();
console.log(metrics.Documents, metrics.Frames, metrics.JSEventListeners);
await page.goto('https://yahoo.com');
await page.goto('https://bing.com');
await page.goto('https://github.com/login');
browser.close();
}
run();
非常感谢任何帮助使此代码变得更受人尊敬的东西:)
在最近的版本中,您有
page.metrics()
可用:
它将返回一个包含一堆数字的对象,包括:
查看文档以获取完整列表
你可以这样使用它:
await page.goto('https://github.com/login');
const gitMetrics = await page.metrics();
console.log(gitMetrics.Timestamp)
console.log(gitMetrics.TaskDuration)