Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 2 #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 7 additions & 77 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,14 @@
#!/usr/bin/env node

const lighthouse = require('lighthouse'),
chromeLauncher = require('chrome-launcher'),
queue = require('async/queue'),
cTable = require('console.table'),
loadingSpinner = require('loading-spinner'),
program = require('commander'),
packageJson = require('./package.json');
const validate = require('./src/validate'),
params = require('./src/params');

var allResults = [[],[]];
var config = {
instances: 5,
iterations: 5
}

function launchChromeAndRunLighthouse(url, config = null) {
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)
});
});
}

function toTitleCase(str) {
return str.replace(/-/g, ' ').replace(/\w\S*/g, function(txt){
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
}

function begin(url1, url2){
loadingSpinner.start(100, { clearChar: true, doNotBlock: true });

config.iterations = program.number || number;
config.instances = program.instances || instances;
var config = params();

const lighthouseQueue = queue((endpoint, callback) => {
launchChromeAndRunLighthouse(endpoint.url).then(function(results) {
allResults[endpoint.version].push(results);
callback()
});
}, config.instances)
if(validate(config)){
const lightdiff = require('./src/lightdiff');

lighthouseQueue.drain = function() {
loadingSpinner.stop();
console.log(config)

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"] = toTitleCase(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: url1, version: 0})
lighthouseQueue.push({url: url2, version: 1})
}
lightdiff(config);
}

program
.version(packageJson.version)
.arguments('<url1> <url2>')
.option('-n, --number <number>', 'The number of times both URL\'s should be run in lighthouse (default: 5)')
.option('-o, --one <one>', 'The title of the first URL tested')
.option('-t, --two <two>', 'The title of the second URL tested')
.option('-i, --instances <instances>', 'The number lighthouse instances used at once (default: 5)')
.action(begin)
.parse(process.argv);
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"version": "1.0.4",
"description": "Command line tool for comparing the average performance of two urls.",
"main": "index.js",
"engines": {
"node": ">=8.9"
},
"bin": {
"lightdiff": "./index.js"
},
Expand All @@ -21,7 +24,8 @@
"commander": "^2.16.0",
"console.table": "^0.10.0",
"lighthouse": "^3.0.2",
"loading-spinner": "^1.2.0"
"loading-spinner": "^1.2.0",
"semver": "^5.5.0"
},
"keywords": [
"compare",
Expand Down
5 changes: 5 additions & 0 deletions src/helpers/dashToTitleCase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = function (str) {
return str.replace(/-/g, ' ').replace(/\w\S*/g, function(txt){
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
}
66 changes: 66 additions & 0 deletions src/lightdiff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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})
}
}
24 changes: 24 additions & 0 deletions src/params.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const program = require('commander'),
packageJson = require('../package.json');

module.exports = function() {
var config = {};

program
.version(packageJson.version)
.arguments('<url1> <url2>')
.option('-n, --number <number>', 'The number of times both URL\'s should be run in lighthouse (default: 5)')
.option('-o, --one <one>', 'The title of the first URL tested')
.option('-t, --two <two>', 'The title of the second URL tested')
.option('-i, --instances <instances>', 'The number lighthouse instances used at once (default: 5)')
.action(function (url1, url2) {
config.url1 = url1;
config.url2 = url2;
})
.parse(process.argv);

config.instances = program.instances || 5;
config.iterations = program.number || 5;

return config;
}
37 changes: 37 additions & 0 deletions src/validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const packageJson = require('../package.json'),
semver = require('semver');

module.exports = function(config){
var safe = true;
const number = config.number,
iterations = config.iterations,
version = packageJson.engines.node;

if (!semver.satisfies(process.version, version)) {
console.log(`Required node version ${version} not satisfied with current version ${process.version}.`);
safe = false;
}

if(!config.url1 || config.url1 == '' && !config.url2 || config.url2 == ''){
console.error('Error: url1 & url2 must be provided!');
safe = false;
}else if(!config.url1 || config.url1 == ''){
console.error('Error: url1 must be provided!');
safe = false;
}else if(!config.url2 || config.url2 == ''){
console.error('Error: url2 must be provided!');
safe = false;
}

if(number && !/^\d+$/.test(number)){
console.error('Error: number value must be a number!');
safe = false;
}

if(iterations && !/^\d+$/.test(iterations)){
console.error('Error: iterations value must be a number!');
safe = false;
}

return safe;
}