Skip to content

Instantly share code, notes, and snippets.

@pokutuna
Created June 25, 2018 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pokutuna/6686e32b836ece1d5ce34e468d0cb1f4 to your computer and use it in GitHub Desktop.
Save pokutuna/6686e32b836ece1d5ce34e468d0cb1f4 to your computer and use it in GitHub Desktop.
{
"dependencies": {
"puppeteer": "^1.5.0",
"speedline": "^1.3.3"
}
}
const puppeteer = require('puppeteer');
const devices = require('puppeteer/DeviceDescriptors');
const speedline = require('speedline');
/**
* Usage
* $ node index.js [URL] [withCache] [isMobile]
*/
const pageUrl = process.argv[2];
const withCache = process.argv[3] ? true : false;
const isMobile = process.argv[4] ? true : false;
(async (pageUrl, withCache, isMobile) => {
const browser = await puppeteer.launch({ ignoreHTTPSErrors: true });
const page = await browser.newPage();
if (!isMobile) {
// analytics 見て PC で多い解像度
await page.setViewport({
width: 1366,
height: 768
});
} else {
// iPhone 7
await page.setViewport({
width: 375,
height: 667,
deviceScaleFactor: 2,
isMobile: true,
hasTouch: true,
isLandscape: false
});
}
if (withCache) {
await page.setCacheEnabled(true);
await page.goto(pageUrl);
}
await page.tracing.start({
screenshots: true,
path: 'trace.json'
});
await page.goto(pageUrl);
await page.tracing.stop();
await browser.close();
const results = await speedline('trace.json');
const metrics = ['speedIndex', 'perceptualSpeedIndex', 'first', 'complete', 'duration'];
console.log(metrics.join('\t'));
console.log(metrics.map((m) => results[m]).join('\t'));
})(pageUrl, withCache, isMobile);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment