Last active
March 24, 2021 17:02
-
-
Save dwsmart/218f78d6e3e305f5c39cae006a11fd49 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const puppeteer = require('puppeteer'); | |
// throttle network and cpu? | |
const throttle = true; | |
// viewport sizes | |
const viewportw = 412; | |
const viewporth = 732; | |
// url to test | |
const theurl = 'https://tamethebots.com'; | |
(async() => { | |
const browser = await puppeteer.launch(); | |
const page = await browser.newPage(); | |
const client = await page.target().createCDPSession() | |
if (throttle) { | |
await client.send('Network.emulateNetworkConditions', { | |
'offline': false, | |
'downloadThroughput': 1.5 * 1024 * 1024 / 8, | |
'uploadThroughput': 750 * 1024 / 8, | |
'latency': 40 | |
}) | |
await client.send('Emulation.setCPUThrottlingRate', { rate: 4 }); | |
} | |
await page.setViewport({ | |
width: parseInt(viewportw), | |
height: parseInt(viewporth) | |
}); | |
try { | |
await page.goto(theurl, { | |
waitUntil: 'networkidle0' | |
}); | |
await page.evaluate(() => { | |
const elem = document.createElement('a'); | |
elem.id = 'ttbClickTarget'; | |
elem.href = '#'; | |
elem.style.width = '1px'; | |
elem.style.height = '1px'; | |
elem.innerHTML = "here" | |
document.body.prepend(elem); | |
}); | |
await page.waitForSelector('#ttbClickTarget') | |
await page.click('#ttbClickTarget') | |
const metricsArray = await Promise.race([page.evaluate(() => { | |
return new Promise(resolve => { | |
function sendFid(verdict) { | |
resolve(verdict); | |
} | |
new PerformanceObserver(list => { | |
list.getEntries().forEach(entry => { | |
FID = parseFloat(entry.processingStart - entry.startTime); | |
let ver = 'good'; | |
if (FID > 100 && FID <= 300) { | |
ver = 'needs improvement'; | |
} | |
if (FID > 300) { | |
ver = 'poor'; | |
} | |
sendFid({ | |
fid: `${FID.toFixed(4)} Ms`, | |
verdict: ver | |
}); | |
}); | |
}).observe({ | |
type: 'first-input', | |
buffered: true | |
}); | |
}); | |
}), | |
page.waitForTimeout(5000) | |
]); | |
console.log(metricsArray); | |
await browser.close(); | |
} catch (err) { | |
console.log('Error loading page:', err); | |
await browser.close(); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment