Created
September 7, 2017 18:20
-
-
Save davisshaver/7d8148ad48d8ca63811896e6b3c49426 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 lighthouse = require('lighthouse'); | |
const perfConfig = require('lighthouse/lighthouse-core/config/perf.json'); | |
const chromeLauncher = require('lighthouse/chrome-launcher'); | |
const debug = require('debug')('speed'); | |
function launchChromeAndRunLighthouse(url, flags = {}, config = null) { | |
return chromeLauncher.launch().then(chrome => { | |
flags.chromeFlags = [ | |
'--headless', | |
'--disable-gpu', | |
'--no-sandbox', | |
] | |
flags.port = chrome.port; | |
return lighthouse(url, flags, config).then(results => | |
chrome.kill().then(() => results)); | |
}); | |
} | |
const flags = {}; | |
// Usage: | |
launchChromeAndRunLighthouse('https://www.google.com', flags, perfConfig).then(results => { | |
debug('User-agent: %s\n', results.userAgent); | |
debug('Here are some metrics we care about...'); | |
debug( | |
'• First meaningful paint: %d (%d)', | |
results.audits['first-meaningful-paint'].rawValue, | |
results.audits['first-meaningful-paint'].score | |
); | |
debug( | |
'• Speed Index: %d (%d)', | |
results.audits['speed-index-metric'].rawValue, | |
results.audits['speed-index-metric'].score | |
); | |
debug( | |
'• First interactive: %d (%d)', | |
results.audits['first-interactive'].rawValue, | |
results.audits['first-interactive'].score | |
); | |
debug( | |
'• Total byte weight: %d (%d)', | |
results.audits['total-byte-weight'].rawValue, | |
results.audits['total-byte-weight'].score | |
); | |
if (results.audits['first-meaningful-paint'].score > 20) { | |
process.exit(0); | |
} | |
process.exit(1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment