Search code examples
google-chrome-devtoolspuppeteerchrome-devtools-protocol

Using Puppeteer & Chrome DevTools Protocol, how to get the 4 Performance Timing metrics?


To create the below screenshot, I have opened the Chrome Developer Tools, selected the "Performance" tab, started a recording, opened https://www.ted.com, and then stopped the recording. This is what I see:

enter image description here

How can I programatically using Puppeteer & potentially the Chrome DevTools Protocol, get the 4 metrics circled in the screenshot?


Solution

  • You can try this

     const perfEntries = JSON.parse(
            await page.evaluate(() => JSON.stringify(performance.toJSON()))
        );
        const paints = await page.evaluate(_ => {
            const result = {};
            performance.getEntriesByType('paint').map(entry => {
                result[entry.name] = entry.startTime;
            });
            return result;
        });
    
        for (const [key, val] of Object.entries(paints)) {
            console.log(`${key}: ${Math.round(val)}ms`);
        }
    
        console.log('domContentLoadedEventEnd :' + `${Math.round(perfEntries.timing.domContentLoadedEventEnd) - Math.round(perfEntries.timeOrigin)}ms`);
        console.log('domComplete :' + `${ Math.round(perfEntries.timing.domComplete) - Math.round(perfEntries.timeOrigin)}ms`);
        console.log('loadEventEnd :' + `${Math.round(perfEntries.timing.loadEventEnd) - Math.round(perfEntries.timeOrigin)}ms`);
    

    Output :

    first-paint: 927ms
    first-contentful-paint: 927ms
    domContentLoadedEventEnd :1409ms
    domComplete :4103ms
    loadEventEnd :4183ms