Kamal Palei
Kamal Palei

Reputation: 2022

Puppeteer: How to get total bytes sent / received in a page

Is there any puppeteer api/workaround to get total bytes sent / received in a page. For example below code gives me all timing stats.

await page.evaluate(() => JSON.stringify(window.performance.timing))

Similarly is there any way or work around I can get total bytes / sent received in a page. Byte counts should include HTTP, Websocket, XHR request / response headers, body all.

Upvotes: 2

Views: 2552

Answers (1)

Rahul L
Rahul L

Reputation: 4349

By using

const perfEntries = JSON.parse(
  await page.evaluate(() => JSON.stringify(performance.getEntries()))
);

console.log(perfEntries);

You will get entries for all the requests.Add the size for all requests.

transferSize/encodedBodySize/decodedBodySize

{ name: 'https://www.google.com/',
    entryType: 'navigation',
    startTime: 0,
    duration: 7156.92000000854,
    initiatorType: 'navigation',
    nextHopProtocol: 'h2',
    workerStart: 0,
    redirectStart: 0,
    redirectEnd: 0,
    fetchStart: 1.7300000181421638,
    domainLookupStart: 1.7300000181421638,
    domainLookupEnd: 1.7300000181421638,
    connectStart: 1.7300000181421638,
    connectEnd: 1.7300000181421638,
    secureConnectionStart: 0,
    requestStart: 722.3000000230968,
    responseStart: 863.4900000179186,
    responseEnd: 948.6600000236649,
    transferSize: 64008,
    encodedBodySize: 63410,
    decodedBodySize: 216421,
    serverTiming: [],
    unloadEventStart: 0,
    unloadEventEnd: 0,
    domInteractive: 1416.8400000198744,
    domContentLoadedEventStart: 1416.880000004312,
    domContentLoadedEventEnd: 1424.5000000228174,
    domComplete: 7133.590000012191,
    loadEventStart: 7133.6149999988265,
    loadEventEnd: 7156.92000000854,
    type: 'navigate',
    redirectCount: 0 },...............................................]

Upvotes: 3

Related Questions