-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlightdiff.js
66 lines (51 loc) · 2.41 KB
/
lightdiff.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
61
62
63
64
65
66
const lighthouse = require('lighthouse'),
chromeLauncher = require('chrome-launcher'),
queue = require('async/queue'),
cTable = require('console.table'),
loadingSpinner = require('loading-spinner'),
dashToTitleCase = require('./helpers/dashToTitleCase');
var allResults = [[],[]];
var runningConfig = {};
function launchChromeAndRunLighthouse(url, config = null) {
console.log(url);
const opts = {chromeFlags: ['--headless']};
return chromeLauncher.launch({chromeFlags: opts.chromeFlags}).then(chrome => {
opts.port = chrome.port;
return lighthouse(url, opts, config).then(results => {
return chrome.kill().then(() => results.lhr)
});
});
}
module.exports = function (config){
loadingSpinner.start(100, { clearChar: true, doNotBlock: true });
runningConfig = config;
const lighthouseQueue = queue((endpoint, callback) => {
launchChromeAndRunLighthouse(endpoint.url).then(function(results) {
allResults[endpoint.version].push(results);
callback()
});
}, config.instances)
lighthouseQueue.drain = function() {
console.log('heheheh');
loadingSpinner.stop();
var table = [], arr = ['first-meaningful-paint', 'first-contentful-paint', 'speed-index', 'estimated-input-latency', 'time-to-first-byte',
'first-cpu-idle', 'interactive', 'mainthread-work-breakdown', 'bootup-time', 'network-requests', 'total-byte-weight', 'unused-css-rules'];
arr.forEach( function(string) {
var before = Math.round(allResults[0].reduce((a,c) => (c['audits'][string]['rawValue'] + a), 0) / allResults[0].length);
var after = Math.round(allResults[1].reduce((a,c) => (c['audits'][string]['rawValue'] + a), 0) / allResults[0].length);
var difference = -((before - after) / before * 100).toFixed(2);
var tableData = {};
tableData["Metric"] = dashToTitleCase(string);
tableData['Url 1'] = before;
tableData['Url 2'] = after;
tableData["Difference"] = difference > 0 ? '+' + difference + '%' : difference + '%';
table.push(tableData);
});
console.log('\n');
console.table(table);
};
for(var x = 0; x < config.iterations; x++){
lighthouseQueue.push({url: config.url1, version: 0})
lighthouseQueue.push({url: config.url2, version: 1})
}
}