[Puppeteer] Get a Page's Load Time with Puppeteer (window.profermence.timing)

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Puppeteer] Get a Page's Load Time with Puppeteer (window.profermence.timing)相关的知识,希望对你有一定的参考价值。

In this lesson we are going to use Google‘s Puppeteer to gather metrics about a page‘s load time. We‘ll use a high level date subtraction method as well as gather data from the window performance timing. Then see how throttling the network to 3G affects the page‘s load time.

 

const getPageMetrics = async ()  => {
    const browser = await puppeteer.launch({headless: false});
  const page = await browser.newPage();
 await page.waitFor(1000); //delay 1 s

  // 3G metwork
  await page._client.send(‘Network.emulateNetworkConditions‘, {
  offline: false,
  latency: 200,
  downloadThroughput: 780*1024 / 8,
  uploadThroughput: 300*1024/8
})
  await page.goto(‘https://developers.google.com/web/‘);

const pref = await page.evaluate( _ => {
  const {loadEventEnd, navigationStart} = window.performance.timing;
  return ({
    loadTime: loadEventEnd - navigationStart
  })
})

console.log(`It took: ${pref.loadTime}ms`)
}

 

About ‘winidow.profermence.timing‘, please check link.

About Chrom devtool protcol, please check link.

 

以上是关于[Puppeteer] Get a Page's Load Time with Puppeteer (window.profermence.timing)的主要内容,如果未能解决你的问题,请参考以下文章

Puppeteer - 协议错误(Page.navigate):目标已关闭

从 Puppeteer 中的 page.evaluate 获取元素?

使用Puppeteer进行数据抓取——Page对象

Puppeteer:使用page.querySelectorAll()不是一个函数

Puppeteer page.evaluate querySelectorAll 返回空对象

使用CucumberJS / Puppeteer,如何使用多个场景扩展Page对象?