Created
March 24, 2021 15:34
-
-
Save dwsmart/70ef7582c8f80bed3627973bf36eab30 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.click('html') | |
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