Skip to content

Commit 4eb1f91

Browse files
tinayuangaomhevery
authored andcommitted
ci(aio): Refactored payload size script, add script to track payload (angular#18517)
PR Close angular#18517
1 parent f53f724 commit 4eb1f91

5 files changed

Lines changed: 166 additions & 78 deletions

File tree

aio/scripts/_payload-limits.sh

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
set -u -e -o pipefail
44

5-
declare -A limitUncompressed
6-
limitUncompressed=(["inline"]=1600 ["main"]=525000 ["polyfills"]=38000)
7-
declare -A limitGzip7
8-
limitGzip7=(["inline"]=1000 ["main"]=127000 ["polyfills"]=12500)
9-
declare -A limitGzip9
10-
limitGzip9=(["inline"]=1000 ["main"]=127000 ["polyfills"]=12500)
5+
declare -A payloadLimits
6+
payloadLimits["aio", "uncompressed", "inline"]=1600
7+
payloadLimits["aio", "uncompressed", "main"]=525000
8+
payloadLimits["aio", "uncompressed", "polyfills"]=38000
9+
payloadLimits["aio", "gzip7", "inline"]=1000
10+
payloadLimits["aio", "gzip7", "main"]=127000
11+
payloadLimits["aio", "gzip7", "polyfills"]=12500
12+
payloadLimits["aio", "gzip9", "inline"]=1000
13+
payloadLimits["aio", "gzip9", "main"]=127000
14+
payloadLimits["aio", "gzip9", "polyfills"]=12500

aio/scripts/payload.sh

Lines changed: 3 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,79 +4,10 @@ set -eu -o pipefail
44

55
readonly thisDir=$(cd $(dirname $0); pwd)
66
readonly parentDir=$(dirname $thisDir)
7-
readonly PROJECT_NAME="angular-payload-size"
87

8+
# Track payload size functions
9+
source ../scripts/ci/payload-size.sh
910
source ${thisDir}/_payload-limits.sh
1011

11-
failed=false
12-
payloadData=""
13-
for filename in dist/*.bundle.js; do
14-
size=$(stat -c%s "$filename")
15-
label=$(echo "$filename" | sed "s/.*\///" | sed "s/\..*//")
16-
payloadData="$payloadData\"uncompressed/$label\": $size, "
12+
trackPayloadSize "aio" "dist/*.bundle.js" true true
1713

18-
19-
gzip -7 $filename -c >> "${filename}7.gz"
20-
size7=$(stat -c%s "${filename}7.gz")
21-
payloadData="$payloadData\"gzip7/$label\": $size7, "
22-
23-
gzip -9 $filename -c >> "${filename}9.gz"
24-
size9=$(stat -c%s "${filename}9.gz")
25-
payloadData="$payloadData\"gzip9/$label\": $size9, "
26-
27-
if [[ $size -gt ${limitUncompressed[$label]} ]]; then
28-
failed=true
29-
echo "Uncompressed $label size is $size which is greater than ${limitUncompressed[$label]}"
30-
elif [[ $size7 -gt ${limitGzip7[$label]} ]]; then
31-
failed=true
32-
echo "Gzip7 $label size is $size7 which is greater than ${limitGzip7[$label]}"
33-
elif [[ $size9 -gt ${limitGzip9[$label]} ]]; then
34-
failed=true
35-
echo "Gzip9 $label size is $size9 which is greater than ${limitGzip9[$label]}"
36-
fi
37-
done
38-
39-
# Add Timestamp
40-
timestamp=$(date +%s)
41-
payloadData="$payloadData\"timestamp\": $timestamp, "
42-
43-
# Add change source: application, dependencies, or 'application+dependencies'
44-
applicationChanged=false
45-
dependenciesChanged=false
46-
if [[ $(git diff --name-only $TRAVIS_COMMIT_RANGE $parentDir | grep -v aio/yarn.lock | grep -v content) ]]; then
47-
applicationChanged=true
48-
fi
49-
if [[ $(git diff --name-only $TRAVIS_COMMIT_RANGE $parentDir/yarn.lock) ]]; then
50-
dependenciesChanged=true
51-
fi
52-
53-
if $dependenciesChanged && $applicationChanged; then
54-
change='application+dependencies'
55-
elif $dependenciesChanged; then
56-
# only yarn.lock changed
57-
change='dependencies'
58-
elif $applicationChanged; then
59-
change='application'
60-
else
61-
# Nothing changed in aio/
62-
exit 0
63-
fi
64-
message=$(echo $TRAVIS_COMMIT_MESSAGE | sed 's/"/\\"/g' | sed 's/\\/\\\\/g')
65-
payloadData="$payloadData\"change\": \"$change\", \"message\": \"$message\""
66-
67-
payloadData="{${payloadData}}"
68-
69-
echo $payloadData
70-
71-
if [[ "$TRAVIS_PULL_REQUEST" == "false" ]]; then
72-
readonly safeBranchName=$(echo $TRAVIS_BRANCH | sed -e 's/\./_/g')
73-
readonly dbPath=/payload/aio/$safeBranchName/$TRAVIS_COMMIT
74-
75-
# WARNING: FIREBASE_TOKEN should NOT be printed.
76-
set +x
77-
firebase database:update --data "$payloadData" --project $PROJECT_NAME --confirm --token "$ANGULAR_PAYLOAD_FIREBASE_TOKEN" $dbPath
78-
fi
79-
80-
if [[ $failed = true ]]; then
81-
exit 1
82-
fi

integration/_payload-limits.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
set -u -e -o pipefail
4+
5+
declare -A payloadLimits
6+
payloadLimits["hello_world__closure", "uncompressed", "bundle"]=106000
7+
payloadLimits["hello_world__closure", "gzip7", "bundle"]=35000
8+
payloadLimits["hello_world__closure", "gzip9", "bundle"]=35000
9+
10+
payloadLimits["cli-hello-world", "uncompressed", "inline"]=1500
11+
payloadLimits["cli-hello-world", "uncompressed", "main"]=183000
12+
payloadLimits["cli-hello-world", "uncompressed", "polyfills"]=63000
13+
payloadLimits["cli-hello-world", "gzip7", "inline"]=900
14+
payloadLimits["cli-hello-world", "gzip7", "main"]=48000
15+
payloadLimits["cli-hello-world", "gzip7", "polyfills"]=21000
16+
payloadLimits["cli-hello-world", "gzip9", "inline"]=900
17+
payloadLimits["cli-hello-world", "gzip9", "main"]=48000
18+
payloadLimits["cli-hello-world", "gzip9", "polyfills"]=21000
19+

integration/run_tests.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ set -e -o pipefail
44

55
cd `dirname $0`
66

7+
# Track payload size functions
8+
source ../scripts/ci/payload-size.sh
9+
source ./_payload-limits.sh
10+
711
# Workaround https://github.com/yarnpkg/yarn/issues/2165
812
# Yarn will cache file://dist URIs and not update Angular code
913
readonly cache=.yarn_local_cache
@@ -34,7 +38,17 @@ for testDir in $(ls | grep -v node_modules) ; do
3438
cd $testDir
3539
# Workaround for https://github.com/yarnpkg/yarn/issues/2256
3640
rm -f yarn.lock
41+
rm -rf dist
3742
yarn install --cache-folder ../$cache
3843
yarn test || exit 1
44+
# Track payload size for cli-hello-world and hello_world__closure
45+
if [[ $testDir == cli-hello-world ]] || [[ $testDir == hello_world__closure ]]; then
46+
if [[ $testDir == cli-hello-world ]]; then
47+
yarn build
48+
fi
49+
trackPayloadSize "$testDir" "dist/*.js" true false
50+
fi
3951
)
4052
done
53+
54+
trackPayloadSize "umd" "../dist/packages-dist/*/bundles/*.umd.min.js" false false

scripts/ci/payload-size.sh

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#!/usr/bin/env bash
2+
3+
readonly PROJECT_NAME="angular-payload-size"
4+
5+
# Calculate the size of target file uncompressed size, gzip7 size, gzip9 size
6+
# Write to global variable $payloadData, $filename
7+
calculateSize() {
8+
size["uncompressed"]=$(stat -c%s "$filename")
9+
label=$(echo "$filename" | sed "s/.*\///" | sed "s/\..*//")
10+
payloadData="$payloadData\"uncompressed/$label\": ${size["uncompressed"]}, "
11+
12+
gzip -7 $filename -c >> "${filename}7.gz"
13+
size["gzip7"]=$(stat -c%s "${filename}7.gz")
14+
payloadData="$payloadData\"gzip7/$label\": ${size["gzip7"]}, "
15+
16+
gzip -9 $filename -c >> "${filename}9.gz"
17+
size["gzip9"]=$(stat -c%s "${filename}9.gz")
18+
payloadData="$payloadData\"gzip9/$label\": ${size["gzip9"]}, "
19+
}
20+
21+
# Check whether the file size is under limit
22+
# Write to global variable $failed
23+
# Read from global variables $size, $size7, $size9, $label, $limitUncompressed
24+
checkSize() {
25+
for fileType in "uncompressed" "gzip7" "gzip9"; do
26+
if [[ ${size[$fileType]} -gt ${payloadLimits[$name, $fileType, $label]} ]]; then
27+
failed=true
28+
echo "$fileType $label size is ${size[$fileType]} which is greater than ${payloadLimits[$name, $fileType, $label]}"
29+
fi
30+
done
31+
}
32+
33+
# Write timestamp to global variable $payloadData
34+
addTimestamp() {
35+
# Add Timestamp
36+
timestamp=$(date +%s)
37+
payloadData="$payloadData\"timestamp\": $timestamp, "
38+
}
39+
40+
# Write travis commit message to global variable $payloadData
41+
addMessage() {
42+
message=$(git log --oneline $TRAVIS_COMMIT_RANGE)
43+
message=$(echo $message | sed 's/"/\\"/g' | sed 's/\\/\\\\/g')
44+
payloadData="$payloadData\"message\": \"$message\""
45+
}
46+
47+
# Add change source: application, dependencies, or 'application+dependencies'
48+
# Read from global variables $parentDir
49+
# Update the change source to global variable $payloadData
50+
addChange() {
51+
yarnChanged=false
52+
allChangedFiles=$(git diff --name-only $TRAVIS_COMMIT_RANGE $parentDir | wc -l)
53+
allChangedFileNames=$(git diff --name-only $TRAVIS_COMMIT_RANGE $parentDir)
54+
55+
if [[ $allChangedFileNames == *"yarn.lock"* ]]; then
56+
yarnChanged=true
57+
fi
58+
59+
if [[ $allChangedFiles -eq 1 ]] && [[ "$yarnChanged" = true ]]; then
60+
# only yarn.lock changed
61+
change='dependencies'
62+
elif [[ $allChangedFiles -gt 1 ]] && [[ "$yarnChanged" = true ]]; then
63+
change='application+dependencies'
64+
elif [[ $allChangedFiles -gt 0 ]]; then
65+
change='application'
66+
else
67+
# Nothing changed in aio/
68+
exit 0
69+
fi
70+
payloadData="$payloadData\"change\": \"$change\", "
71+
}
72+
73+
# Upload data to firebase database if it's commit, print out data for pull
74+
# requests
75+
uploadData() {
76+
name="$1"
77+
payloadData="{${payloadData}}"
78+
79+
echo The data for $name is:
80+
echo $payloadData
81+
82+
if [[ "$TRAVIS_PULL_REQUEST" == "false" ]]; then
83+
readonly safeBranchName=$(echo $TRAVIS_BRANCH | sed -e 's/\./_/g')
84+
readonly dbPath=/payload/$name/$safeBranchName/$TRAVIS_COMMIT
85+
86+
# WARNING: FIREBASE_TOKEN should NOT be printed.
87+
set +x
88+
firebase database:update --data "$payloadData" --project $PROJECT_NAME --confirm --token "$ANGULAR_PAYLOAD_FIREBASE_TOKEN" $dbPath
89+
fi
90+
}
91+
92+
# Track payload size, $1 is the name in database, $2 is the file path
93+
# $3 is whether we check the payload size and fail the test if the size exceeds
94+
# limit, $4 is whether record the type of changes: true | false
95+
trackPayloadSize() {
96+
name="$1"
97+
path="$2"
98+
checkSize="$3"
99+
trackChange=$4
100+
101+
payloadData=""
102+
failed=false
103+
for filename in $path; do
104+
declare -A size
105+
calculateSize
106+
if [[ $checkSize = true ]]; then
107+
checkSize
108+
fi
109+
done
110+
addTimestamp
111+
if [[ $trackChange = true ]]; then
112+
addChange
113+
fi
114+
addMessage
115+
uploadData $name
116+
if [[ $failed = true ]]; then
117+
echo exit 1
118+
exit 1
119+
fi
120+
}

0 commit comments

Comments
 (0)