-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmeasure-chromium.js
60 lines (54 loc) · 1.82 KB
/
measure-chromium.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const puppeteer = require('puppeteer')
const lighthouse = require('lighthouse')
const delay = require('delay')
const { defaultCacheType, cacheType } = require('../config')
const { isDefaultExt } = require('./extension')
const lhrConfig = {
extends: 'lighthouse:default',
settings: {
throttlingMethod: 'devtools', // Lantern does not support warm/hot caching
emulatedFormFactor: 'desktop', // It's not possible to install extension on mobile
throttling: {
cpuSlowdownMultiplier: 2 // reduce slowdown to emulate desktop
},
output: 'json',
onlyAudits: [
'screenshot-thumbnails',
'max-potential-fid',
'interactive',
'mainthread-work-breakdown',
'bootup-time',
'network-requests',
'main-thread-tasks'
]
}
}
/**
* @typedef {import('./extension').Extension} Extension
* @typedef {import('../index').LhResult} LhResult
*
* @param {string} url
* @param {Extension} ext
* @param {string} [cache]
* @return {Promise<{ lhr: LhResult }>}
*/
exports.measureChromium = async function(url, ext, cache = defaultCacheType) {
const browser = await puppeteer.launch({
headless: isDefaultExt(ext), // headless mode is not possible for extensions
args: isDefaultExt(ext) ? [] : [`--disable-extensions-except=${ext.path}`, `--load-extension=${ext.path}`]
})
const lhOpts = {
port: new URL(browser.wsEndpoint()).port,
disableStorageReset: cache !== defaultCacheType
}
if (!isDefaultExt(ext)) await delay(10000) // await extension to be installed
if (cache === cacheType.warm) {
await lighthouse(url, lhOpts, lhrConfig)
} else if (cache === cacheType.hot) {
await lighthouse(url, lhOpts, lhrConfig)
await lighthouse(url, lhOpts, lhrConfig)
}
const { lhr } = await lighthouse(url, lhOpts, lhrConfig)
await browser.close()
return { lhr }
}