Created
October 1, 2024 20:09
-
-
Save DonRichards/5087bde3d8cdac4a63d82745bc77154e to your computer and use it in GitHub Desktop.
How long does a video page take to load
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
// npm install puppeteer | |
// node measure_video_load.js https://digital.library.jhu.edu/islandora/dumb-show | |
const puppeteer = require('puppeteer'); | |
(async () => { | |
// Get the URL from command-line arguments | |
const url = process.argv[2]; // The URL is the third argument in the command line (0 = node, 1 = script name, 2 = URL) | |
if (!url) { | |
console.error('Please provide a URL as an argument.'); | |
process.exit(1); // Exit the script if no URL is provided | |
} | |
const browser = await puppeteer.launch({ headless: false }); // Keep headless as false for debugging | |
const page = await browser.newPage(); | |
try { | |
// Navigate to the provided URL and wait until the network is idle | |
await page.goto(url, { waitUntil: 'networkidle2', timeout: 120000 }); | |
// Wait for the video element to appear | |
await page.waitForSelector('video', { timeout: 60000 }); | |
// Measure the time it takes for the video to load fully | |
const videoLoadTime = await page.evaluate(() => { | |
return new Promise((resolve) => { | |
const video = document.querySelector('video'); | |
const loadStartTime = performance.now(); | |
// Listen for the 'canplaythrough' event to ensure video is fully loaded | |
video.addEventListener('canplaythrough', () => { | |
const loadEndTime = performance.now(); | |
const loadDuration = loadEndTime - loadStartTime; | |
resolve(loadDuration); | |
}); | |
// Set the video source and trigger the loading | |
video.load(); | |
}); | |
}); | |
console.log(`Video Load Time for ${url}: ${videoLoadTime}ms`); | |
} catch (error) { | |
console.error('Error occurred while waiting for video to load:', error); | |
} | |
await browser.close(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment