Output the budget result as JSON (#2299)

* Output the budget result as JSON
This commit is contained in:
Peter Hedenskog 2019-02-08 21:03:01 +01:00 committed by GitHub
parent fa10c4a723
commit 67b04d3afd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 33 additions and 13 deletions

View File

@ -612,7 +612,7 @@ module.exports.parseCommandLine = function parseCommandLine() {
group: 'Budget'
})
.option('budget.output', {
choices: ['junit', 'tap'],
choices: ['junit', 'tap', 'json'],
describe: 'The output format of the budget.',
group: 'Budget'
})

View File

@ -4,6 +4,7 @@ const deprecatedVerify = require('./deprecatedVerify').verify;
const verify = require('./verify').verify;
const tap = require('./tap');
const junit = require('./junit');
const json = require('./json');
const log = require('intel').getLogger('sitespeedio.plugin.budget');
module.exports = {
@ -47,7 +48,9 @@ module.exports = {
case 'sitespeedio.render': {
if (this.options.budget) {
if (this.options.budget.output === 'tap') {
if (this.options.budget.output === 'json') {
json.writeJson(this.result, this.storageManager.getBaseDir());
} else if (this.options.budget.output === 'tap') {
tap.writeTap(this.result, this.storageManager.getBaseDir());
} else if (this.options.budget.output === 'junit') {
junit.writeJunit(this.result, this.storageManager.getBaseDir());

View File

@ -0,0 +1,11 @@
'use strict';
const fs = require('fs'),
log = require('intel').getLogger('sitespeedio.plugin.budget'),
path = require('path');
exports.writeJson = function(results, dir) {
const file = path.join(dir, 'budget-result.json');
log.info('Write budget to %s', path.resolve(file));
fs.writeFileSync(file, JSON.stringify(results, null, 2));
};

View File

@ -27,11 +27,11 @@ exports.writeJunit = function(results, dir) {
.failure(
result.metric +
' is ' +
result.value +
result.friendlyValue +
' and limit ' +
result.limitType +
' ' +
result.limit +
result.friedlyLimit +
' ' +
url
);
@ -47,11 +47,11 @@ exports.writeJunit = function(results, dir) {
.standardOutput(
result.metric +
' is ' +
result.value +
result.friendlyValue +
' and limit ' +
result.limitType +
' ' +
result.limit +
result.friendlyLimit +
' ' +
url
);

View File

@ -20,7 +20,8 @@ exports.writeTap = function(results, dir) {
tap(result.type + '.' + result.metric + ' ' + url, function(t) {
let extra = '';
if (resultType === 'failing') {
extra = ' limit ' + result.limitType + ' ' + result.limit + EOL;
extra =
' limit ' + result.limitType + ' ' + result.friendlyLimit + EOL;
}
t.ok(
resultType === 'failing' ? false : true,
@ -28,7 +29,7 @@ exports.writeTap = function(results, dir) {
'.' +
result.metric +
' ' +
result.value +
result.friendlyValue +
' ' +
extra +
' ' +

View File

@ -11,12 +11,15 @@ const size = require('../../support/helpers').size.format;
const log = require('intel').getLogger('sitespeedio.plugin.budget');
const friendlyNames = require('./friendlynames');
function getItem(url, type, metric, value, limit, limitType) {
function getItem(fullPath, type, metric, value, limit, limitType) {
const format = getHelperFunction(fullPath);
return {
metric,
type,
value,
friendlyValue: format(value),
limit,
friendlyLimit: format(limit),
limitType,
status: 'working'
};
@ -57,24 +60,25 @@ module.exports = {
let value = get(message.data, fullPath);
// We got a matching metric
if (value) {
const format = getHelperFunction(fullPath);
const budgetValue = budgetForThisURL[metricType][metric];
const item = getItem(
message.url,
fullPath,
metricType,
metric,
format(value),
format(budgetValue),
value,
budgetValue,
tool === 'coach' ? 'min' : 'max'
);
if (tool === 'coach') {
if (value < budgetValue) {
item.status = 'failing';
failing.push(item);
} else {
working.push(item);
}
} else {
if (value > budgetValue) {
item.status = 'failing';
failing.push(item);
} else {
working.push(item);

View File

@ -9,6 +9,7 @@ module.exports = {
return Number(bytes / KB).toFixed(1);
},
format(bytes) {
if (bytes === 0) return '0 b';
if (!bytes || bytes < 0) return 'N/A';
if (bytes < KB) {