Using Puppeteer, how to get Chrome DevTools' “Network” tab's timing information?

耗尽温柔 提交于 2020-03-25 04:06:49

问题


Below is a screenshot of me accessing https://www.ted.com and inspecting the Google Chrome DevTools' "Network" tab, and viewing the Timing data for the root & child requests.

How can I get all of the above timing data programatically using Puppeteer? Ideally, it would look like this JSON structure:

{
    name: "www.ted.com",
    queueTime: 0,
    startedTime: 1.93,
    stalledTime: 4.59,
    dnsLookupTime: 10.67,
    initialConnectionTime: <the number of milliseconds>,
    ...
},
{
    name: <the next child request>,
    ...
}

回答1:


You want to check out HAR (HTTP Archive) files, which is what you would create by saving the data via Chrome.

Quotation what a HAR file is (source):

The HAR file format is an evolving standard and the information contained within it is both flexible and extensible. You can expect a HAR file to include a breakdown of timings including:

  1. How long it takes to fetch DNS information
  2. How long each object takes to be requested
  3. How long it takes to connect to the server
  4. How long it takes to transfer assets from the server to the browser of each object

The data is stored as a JSON document and extracting meaning from the low-level data is not always easy. But with practice a HAR file can quickly help you identify the key performance problems with a web page, letting you efficiently target your development efforts at areas of your site that will deliver the greatest results.

There are libraries like puppeteer-har and chrome-har which can create HAR files by using puppeteer.

Code sample (for puppeteer-har, quote from the page)

const har = new PuppeteerHar(page);
await har.start({ path: 'results.har' });

await page.goto('http://example.com');

await har.stop();


来源:https://stackoverflow.com/questions/57526202/using-puppeteer-how-to-get-chrome-devtools-network-tabs-timing-information

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!