2017-11-29 05:47:04 -05:00
|
|
|
'use strict';
|
2017-11-03 18:20:09 -04:00
|
|
|
|
2017-11-29 05:47:04 -05:00
|
|
|
// Imports
|
2017-11-03 18:20:09 -04:00
|
|
|
const fs = require('fs');
|
2017-11-18 15:26:33 -05:00
|
|
|
|
2017-11-29 05:47:04 -05:00
|
|
|
// Get branch and project name from command line arguments.
|
|
|
|
const [, , limitFile, project, branch, commit] = process.argv;
|
2017-11-03 18:20:09 -04:00
|
|
|
|
2017-11-29 05:47:04 -05:00
|
|
|
// Load sizes.
|
|
|
|
const currentSizes = JSON.parse(fs.readFileSync('/tmp/current.log', 'utf8'));
|
|
|
|
const allLimitSizes = JSON.parse(fs.readFileSync(limitFile, 'utf8'));
|
|
|
|
const limitSizes = allLimitSizes[project][branch] || allLimitSizes[project]['master'];
|
2017-11-18 15:26:33 -05:00
|
|
|
|
2017-11-29 05:47:04 -05:00
|
|
|
// Check current sizes against limits.
|
2017-11-03 18:20:09 -04:00
|
|
|
let failed = false;
|
2019-11-21 14:42:11 -05:00
|
|
|
const successMessages = [];
|
|
|
|
const failureMessages = [];
|
2017-11-29 05:47:04 -05:00
|
|
|
for (const compressionType in limitSizes) {
|
|
|
|
if (typeof limitSizes[compressionType] === 'object') {
|
|
|
|
const limitPerFile = limitSizes[compressionType];
|
|
|
|
|
|
|
|
for (const filename in limitPerFile) {
|
|
|
|
const expectedSize = limitPerFile[filename];
|
|
|
|
const actualSize = currentSizes[`${compressionType}/${filename}`];
|
|
|
|
|
2018-11-23 14:08:23 -05:00
|
|
|
if (actualSize === undefined) {
|
|
|
|
failed = true;
|
|
|
|
// An expected compression type/file combination is missing. Maybe the file was renamed or
|
|
|
|
// removed. Report it as an error, so the user updates the corresponding limit file.
|
|
|
|
console.log(
|
2019-11-21 14:42:11 -05:00
|
|
|
`ERROR: Commit ${commit} ${compressionType} ${filename} measurement is missing. ` +
|
2018-11-23 14:08:23 -05:00
|
|
|
'Maybe the file was renamed or removed.');
|
2019-11-21 14:42:11 -05:00
|
|
|
} else {
|
|
|
|
const absoluteSizeDiff = Math.abs(actualSize - expectedSize);
|
|
|
|
// If size diff is larger than 1% or 500 bytes...
|
|
|
|
if (absoluteSizeDiff > 500 || absoluteSizeDiff > expectedSize / 100) {
|
|
|
|
failed = true;
|
|
|
|
// We must also catch when the size is significantly lower than the payload limit, so
|
|
|
|
// we are forced to update the expected payload number when the payload size reduces.
|
|
|
|
// Otherwise, we won't be able to catch future regressions that happen to be below
|
|
|
|
// the artificially inflated limit.
|
|
|
|
const operator = actualSize > expectedSize ? 'exceeded' : 'fell below';
|
|
|
|
|
|
|
|
failureMessages.push(
|
|
|
|
`FAIL: Commit ${commit} ${compressionType} ${filename} ${operator} expected size by 500 bytes or >1% ` +
|
2017-12-04 19:28:06 -05:00
|
|
|
`(expected: ${expectedSize}, actual: ${actualSize}).`);
|
2019-11-21 14:42:11 -05:00
|
|
|
} else {
|
|
|
|
successMessages.push(`SUCCESS: Commit ${commit} ${compressionType} ${filename} did NOT cross size threshold of 500 bytes or >1% ` +
|
|
|
|
`(expected: ${expectedSize}, actual: ${actualSize}).`);
|
|
|
|
}
|
2017-11-03 18:20:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:42:11 -05:00
|
|
|
// Group failure messages separately from success messages so they are easier to find.
|
|
|
|
successMessages.concat(failureMessages).forEach(message => console.log(message));
|
|
|
|
|
2017-11-03 18:20:09 -04:00
|
|
|
if (failed) {
|
2017-12-04 19:28:06 -05:00
|
|
|
console.log(`If this is a desired change, please update the size limits in file '${limitFile}'.`);
|
2017-11-03 18:20:09 -04:00
|
|
|
process.exit(1);
|
|
|
|
} else {
|
2019-11-21 14:42:11 -05:00
|
|
|
console.log(`Payload size check passed. All diffs are less than 1% or 500 bytes.`);
|
2017-11-03 18:20:09 -04:00
|
|
|
}
|