0

要创建下面的屏幕截图,我打开了 Chrome 开发者工具,选择了“性能”选项卡,开始录制,打开https://www.ted.com,然后停止录制。这就是我所看到的:

在此处输入图像描述

我如何以编程方式使用 Puppeteer 和可能的Chrome DevTools Protocol,获得屏幕截图中圈出的 4 个指标?

4

1 回答 1

1

你可以试试这个

 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`);

输出 :

first-paint: 927ms
first-contentful-paint: 927ms
domContentLoadedEventEnd :1409ms
domComplete :4103ms
loadEventEnd :4183ms
于 2019-08-23T09:10:11.110 回答