[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.

原文地址:https://www.cnblogs.com/Answer1215/p/8455377.html