Skip to content

Commit

Permalink
[CHORE] Improve output and fail test for Asset size Check (emberjs#6676)
Browse files Browse the repository at this point in the history
* [CHORE] Improve output and fail test for Asset size Check

* when using tee we need to ensure failures are propagated
  • Loading branch information
runspired authored and snewcomer committed Nov 9, 2019
1 parent 4ecf03d commit 18a3b29
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 25 deletions.
21 changes: 18 additions & 3 deletions .github/workflows/asset-size-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ jobs:
with:
node-version: 12.x
repo-token: ${{ secrets.GITHUB_TOKEN }}
- name: Check SHA
run: |
sha=$(git rev-parse --short=8 HEAD)
echo "HEAD sha=$sha"
echo "GITHUB_SHA sha=$GITHUB_SHA"
mkdir -p tmp
echo $sha > tmp/sha-for-check.txt
originSha=$(git rev-parse HEAD^2)
echo $originSha > tmp/sha-for-commit.txt
git show --format=short --no-patch $originSha
- name: Checkout master
run: git checkout master
- name: Install dependencies for master
Expand All @@ -25,7 +35,9 @@ jobs:
- name: Checkout ${{github.ref}}
# We checkout the PR Branch before parsing the master vendor file
# So that we are always using the current analysis tooling
run: git checkout --progress --force $GITHUB_SHA
run: |
sha=$(cat tmp/sha-for-check.txt)
git checkout --progress --force $sha
- name: Install dependencies for ${{github.ref}}
run: yarn install
- name: Parse Master Assets
Expand All @@ -45,6 +57,7 @@ jobs:
run: |
mkdir -p tmp
mkdir -p tmp/asset-sizes
set -o pipefail
node ./bin/asset-size-tracking/generate-diff.js | tee tmp/asset-sizes/diff.txt
- name: Prepare Data For Report
if: failure() || success()
Expand All @@ -53,6 +66,7 @@ jobs:
- name: Print Asset Size Report
if: failure() || success()
run: |
set -o pipefail
node ./bin/asset-size-tracking/print-analysis.js | tee tmp/asset-sizes/commit-analysis.txt
- name: Upload ${{github.ref}} Dist Artifacts
uses: actions/upload-artifact@v1
Expand All @@ -69,7 +83,8 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
COMMENT_MARKER="Asset Size Report for "
node ./bin/asset-size-tracking/src/create-comment-text.js $GITHUB_SHA > tmp/asset-sizes/comment.txt
COMMENT_MARKER="Asset Size Report for "=
sha=$(cat tmp/sha-for-commit.txt)
node ./bin/asset-size-tracking/src/create-comment-text.js $sha > tmp/asset-sizes/comment.txt
COMMENT_TEXT="@./tmp/asset-sizes/comment.txt"
source bin/asset-size-tracking/src/post-comment.sh
75 changes: 54 additions & 21 deletions bin/asset-size-tracking/generate-diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function analyzeDiff(diff) {
if (diff.currentSize < diff.newSize) {
let delta = diff.newSize - diff.currentSize;
let compressedDelta = diff.newSizeCompressed - diff.currentSizeCompressed;
if (delta > library_failure_threshold) {
if (delta > library_failure_threshold && compressedDelta > 0) {
failures.push(
`The size of the library ${diff.name} has increased by ${formatBytes(delta)} (${formatBytes(
compressedDelta
Expand All @@ -114,44 +114,65 @@ function analyzeDiff(diff) {
}

function printDiff(diff) {
console.log('\n```\n');
printItem(diff);
diff.packages.forEach(pkg => {
printItem(pkg, 2);
pkg.modules.forEach(m => {
printItem(m, 4);
});
});
console.log('\n```\n');
}

function printItem(item, indent = 0) {
if (item.currentSize !== item.newSize) {
const indentColor = indent >= 4 ? 'grey' : indent >= 2 ? 'yellow' : indent >= 0 ? 'magenta' : 'green';
console.log(
leftPad(
chalk[indentColor](item.name) + ' ' + chalk.white(formatBytes(item.newSizeCompressed)) + formatDelta(item),
indent * 2
)
leftPad(chalk[indentColor](item.name) + ' ' + formatSize(item, false) + ' ' + formatSize(item, true), indent * 2)
);
}
}

function formatDelta(item) {
if (item.currentSize === item.newSize) {
return '';
function formatSize(item, isCompressed = false) {
let size = formatBytes(isCompressed ? item.newSizeCompressed : item.newSize);
let delta = formatDelta(item, isCompressed);

return isCompressed ? chalk.grey(`(${chalk.white(size)} ${delta} compressed)`) : `${chalk.white(size)} ${delta}`;
}

function formatDelta(item, isCompressed = false) {
let delta = isCompressed ? item.newSizeCompressed - item.currentSizeCompressed : item.newSize - item.currentSize;

if (delta === 0) {
return chalk.black('±0 B');
}
if (item.currentSize > item.newSize) {
return chalk.green(` (- ${formatBytes(item.currentSizeCompressed - item.newSizeCompressed)})`);

if (delta < 0) {
return chalk.green(`${formatBytes(delta)}`);
} else {
return chalk.red(` (+ ${formatBytes(item.newSizeCompressed - item.currentSizeCompressed)})`);
return chalk.red(`+${formatBytes(delta)}`);
}
}

function humanizeNumber(n) {
let s = n.toFixed(2);
if (s.charAt(s.length - 1) === '0') {
s = n.toFixed(1);

if (s.charAt(s.length - 2) === '0') {
s = n.toFixed(0);
}
}
return s;
}

function formatBytes(b) {
let str;
if (b > 1024) {
str = (b / 1024).toFixed(2) + ' KB';
if (b > 1024 || b < -1024) {
str = humanizeNumber(b / 1024) + ' KB';
} else {
str = b + ' B';
str = humanizeNumber(b) + ' B';
}

return str;
Expand All @@ -164,7 +185,6 @@ function leftPad(str, len, char = ' ') {
return str;
}

printDiff(diff);
const { failures, warnings } = analyzeDiff(diff);

if (failures.length) {
Expand All @@ -181,6 +201,8 @@ if (failures.length) {
console.log(w);
});
}
console.log('\n**Changeset**\n');
printDiff(diff);
console.log('\n</details>');
process.exit(1);
} else {
Expand All @@ -189,16 +211,25 @@ if (failures.length) {
console.log(`\n<details>\n <summary>${diff.name} has not changed in size</summary>`);
} else if (delta > 0) {
console.log(
`\n<detail>\n <summary>${diff.name} shrank by ${formatBytes(delta)} (${formatBytes(
`\n<details>\n <summary>${diff.name} shrank by ${formatBytes(delta)} (${formatBytes(
diff.currentSizeCompressed - diff.newSizeCompressed
)} compressed)</summary>`
);
} else {
console.log(
`\n${diff.name} increased by ${formatBytes(-1 * delta)} (${formatBytes(
diff.newSizeCompressed - diff.currentSizeCompressed
)} compressed) which is within the allowed tolerance of ${library_failure_threshold} bytes uncompressed`
);
let compressedDelta = diff.newSizeCompressed - diff.currentSizeCompressed;
if (-1 * delta < library_failure_threshold) {
console.log(
`\n<details>\n <summary>${diff.name} increased by ${formatBytes(-1 * delta)} (${formatBytes(
compressedDelta
)} compressed) which is within the allowed tolerance of ${library_failure_threshold} bytes uncompressed</summary>`
);
} else {
console.log(
`\n<details>\n <summary>${diff.name} increased by ${formatBytes(
-1 * delta
)} uncompressed but decreased by ${formatBytes(-1 * compressedDelta)} compressed</summary>`
);
}
}
if (warnings.length) {
console.log('\nWarnings\n-----------------------');
Expand All @@ -208,6 +239,8 @@ if (failures.length) {
} else {
console.log('\nIf any packages had changed sizes they would be listed here.');
}
console.log('\n**Changeset**\n');
printDiff(diff);
console.log('\n</details>');
process.exit(0);
}
2 changes: 1 addition & 1 deletion bin/asset-size-tracking/src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ function formatBytes(b) {
if (b > 1024) {
str = (b / 1024).toFixed(2) + ' KB';
} else {
str = b + ' B';
str = b.toFixed(2) + ' B';
}

return str;
Expand Down

0 comments on commit 18a3b29

Please sign in to comment.