Skip to content

Instantly share code, notes, and snippets.

@martinschierle
Created October 15, 2019 10:29
Show Gist options
  • Save martinschierle/d0a2d7e333bc8cb4d0bacc55dc5103f0 to your computer and use it in GitHub Desktop.
Save martinschierle/d0a2d7e333bc8cb4d0bacc55dc5103f0 to your computer and use it in GitHub Desktop.
const puppeteer = require('puppeteer');
const devices = require('puppeteer/DeviceDescriptors');
const Good3G = {
'offline': false,
'downloadThroughput': 1.5 * 1024 * 1024 / 8,
'uploadThroughput': 750 * 1024 / 8,
'latency': 40
};
const phone = devices['Nexus 5X'];
async function run(url) {
// I can't get the experimental flag working on chromium, so let's start system chrome
const browser = await puppeteer.launch({headless: true});
let page = await browser.newPage();
const client = await page.target().createCDPSession();
await client.send('Network.enable');
await client.send('Network.emulateNetworkConditions', Good3G);
await client.send('Emulation.setCPUThrottlingRate', { rate: 4 });
await page.emulate(phone);
// register observer
await page.evaluateOnNewDocument(async function() {
window.longtasks = [];
const observer = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
// `entry` is a PerformanceEntry instance.
longtasks.push([entry.attribution[0].containerSrc, entry.attribution[0].containerType, entry.attribution[0].name]);
/*console.log(entry.entryType);
console.log(entry.startTime); // DOMHighResTimeStamp
console.log(entry.duration); // DOMHighResTimeStamp*/
}
});
// Start observing the entry types you care about.
observer.observe({entryTypes: ['longtask']});
});
// load page
await page.goto(url, {"timeout": "120000"});
// get results
let results = await page.evaluate(function() {return window.longtasks});
console.log(JSON.stringify(results, null, 2));
await page.close();
await browser.close();
}
run("https://www.stern.de");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment