Skip to content

Instantly share code, notes, and snippets.

@trungpv1601
Last active October 24, 2022 14:59
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save trungpv1601/2ccd3cc998149a84ba80ed7a4c9ef562 to your computer and use it in GitHub Desktop.
Save trungpv1601/2ccd3cc998149a84ba80ed7a4c9ef562 to your computer and use it in GitHub Desktop.
Network throttling in Puppeteer. Read more: https://fdalvi.github.io/blog/2018-02-05-puppeteer-network-throttle/
let NETWORK_PRESETS = {
'GPRS': {
'offline': false,
'downloadThroughput': 50 * 1024 / 8,
'uploadThroughput': 20 * 1024 / 8,
'latency': 500
},
'Regular2G': {
'offline': false,
'downloadThroughput': 250 * 1024 / 8,
'uploadThroughput': 50 * 1024 / 8,
'latency': 300
},
'Good2G': {
'offline': false,
'downloadThroughput': 450 * 1024 / 8,
'uploadThroughput': 150 * 1024 / 8,
'latency': 150
},
'Regular3G': {
'offline': false,
'downloadThroughput': 750 * 1024 / 8,
'uploadThroughput': 250 * 1024 / 8,
'latency': 100
},
'Good3G': {
'offline': false,
'downloadThroughput': 1.5 * 1024 * 1024 / 8,
'uploadThroughput': 750 * 1024 / 8,
'latency': 40
},
'Regular4G': {
'offline': false,
'downloadThroughput': 4 * 1024 * 1024 / 8,
'uploadThroughput': 3 * 1024 * 1024 / 8,
'latency': 20
},
'DSL': {
'offline': false,
'downloadThroughput': 2 * 1024 * 1024 / 8,
'uploadThroughput': 1 * 1024 * 1024 / 8,
'latency': 5
},
'WiFi': {
'offline': false,
'downloadThroughput': 30 * 1024 * 1024 / 8,
'uploadThroughput': 15 * 1024 * 1024 / 8,
'latency': 2
}
}
const puppeteer = require('puppeteer')
puppeteer.launch().then(async browser => {
// Create a new tab
const page = await browser.newPage()
// Connect to Chrome DevTools
const client = await page.target().createCDPSession()
// Set throttling property
await client.send('Network.emulateNetworkConditions', {
'offline': false,
'downloadThroughput': 200 * 1024 / 8,
'uploadThroughput': 200 * 1024 / 8,
'latency': 20
})
// Navigate and take a screenshot
await page.goto('https://fdalvi.github.io')
await page.screenshot({path: 'screenshot.png'})
await browser.close()
})
@danielcaldas
Copy link

This is rad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment