Skip to content

Commit 638ffd1

Browse files
committed
misc: refactor artifact size metrics
1 parent 232a6b0 commit 638ffd1

File tree

9 files changed

+220
-16
lines changed

9 files changed

+220
-16
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Calculate Artifact Size
2+
description: Calculates size for JVM artifacts
3+
4+
inputs:
5+
artifact-dirs:
6+
description: List of directories for which artifact sizes will be calculated
7+
required: true
8+
# TODO: Additional inputs
9+
10+
runs:
11+
using: composite
12+
steps:
13+
- name: Checkout repository
14+
uses: actions/checkout@v4
15+
16+
- name: Calculate artifact sizes
17+
shell: bash
18+
run: |
19+
chmod +x ./src/main.sh
20+
chmod +x ./src/metrics.sh
21+
chmod +x ./src/s3.sh
22+
chmod +x ./src/cloudwatch.sh
23+
24+
./src/main.sh

.github/actions/artifact-size-metrics/show-results/action.yml renamed to .github/actions/artifact-size-metrics/download-and-process/action.yml

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
1-
name: Artifact Size Metrics - Show Results
2-
description: Posts a new artifact analysis comment on the PR
1+
name: Download and process artifact size metrics
2+
description: TODO
3+
34
inputs:
4-
working-directory:
5-
required: false
6-
description: The working directory to use for reading the previously-generated report.
7-
default: ''
5+
download:
6+
description: TODO
7+
default: "false"
8+
# TODO: Additional inputs
9+
810
runs:
9-
using: "composite"
11+
using: composite
1012
steps:
11-
- name: Post Artifact Analysis Comment
13+
- name: Download and process artifact size metrics
14+
shell: bash
15+
run: |
16+
chmod +x ./src/main.sh
17+
chmod +x ./src/s3.sh
18+
chmod +x ./src/compare.sh
19+
chmod +x ./src/comment.sh
20+
21+
./src/main.sh
22+
23+
- name: Post artifact analysis comment # TODO: MOVE TO SCRIPT IF POSSIBLE
1224
uses: actions/github-script@v7
1325
with:
1426
script: |
15-
const workingDirectory = '${{ inputs.working-directory }}'
16-
if (workingDirectory) {
17-
process.chdir(workingDirectory)
18-
}
19-
2027
const fs = require('node:fs')
21-
const prNumber = context.issue.number ?? process.env.SDK_PR
22-
28+
const prNumber = context.issue.number ?? process.env.SDK_PR # TOSO - CHANGE - THIS WON'T WORK FOR EXTERNAL CONTRIBUTORS BTW - IF EXTERNAL CONTRIBUTOR JUST FAIL
2329
const prInfo = await github.rest.pulls.get({
2430
owner: context.repo.owner,
2531
repo: context.repo.repo,
@@ -83,4 +89,14 @@ runs:
8389
clientMutationId
8490
}
8591
}`)
86-
}
92+
}
93+
94+
- name: Fail if large size increase
95+
shell: bash
96+
if: ${{ !contains(github.event.pull_request.labels.*.name, 'acknowledge-artifact-size-increase') }}
97+
run: |
98+
if [ "$LARGE_DIFF" == "true" ]; then
99+
echo An artifact increased in size by more than allowed or a new artifact was created.
100+
echo If this is expected please add the 'acknowledge-artifact-size-increase' label to this pull request.
101+
exit 1
102+
fi
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Upload the artifact size metrics to cloudwatch
2+
uploadToCloudwatch() {
3+
set -o pipefail
4+
5+
METRICS_FILE="metrics.csv" # path to your CSV file
6+
PROJECT_REPO_NAME=$1
7+
NAMESPACE="Artifact Size Metrics"
8+
9+
# Read CSV, skipping header
10+
tail -n +2 "$METRICS_FILE" | while IFS=',' read -r artifactName artifactSize; do
11+
artifactName=$(echo "$artifactName" | xargs) # trim spaces
12+
artifactSize=$(echo "$artifactSize" | xargs)
13+
14+
# Build JSON for CloudWatch
15+
metric_json=$(jq -n \
16+
--arg name "$PROJECT_REPO_NAME-$artifactName" \
17+
--arg value "$artifactSize" \
18+
--arg project "$PROJECT_REPO_NAME" \
19+
'{
20+
MetricName: $name,
21+
Timestamp: (now | todate),
22+
Unit: "Bytes",
23+
Value: ($value | tonumber),
24+
Dimensions: [
25+
{ Name: "Project", Value: $project }
26+
]
27+
}'
28+
)
29+
30+
METRICS+=("$metric_json")
31+
done
32+
33+
# Send metrics in chunks of 1000
34+
chunk_size=1000
35+
for ((i=0; i<${#METRICS[@]}; i+=chunk_size)); do
36+
chunk=("${METRICS[@]:i:chunk_size}")
37+
aws cloudwatch put-metric-data \
38+
--namespace "$NAMESPACE" \
39+
--metric-data "$(printf '%s\n' "${chunk[@]}" | jq -s '.')"
40+
done
41+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/bash
2+
3+
source ./metrics.sh
4+
source ./cloudwatch.sh
5+
source ../s3.sh
6+
source ../setup.sh
7+
8+
setup
9+
10+
# Build
11+
if [ "$GITHUB_REPOSITORY" = "aws-sdk-kotlin" ]; then
12+
# FIXME: Enable K/N builds
13+
./gradlew build -Paws.kotlin.native=false build --parallel --max-workers 16
14+
else
15+
./gradlew build
16+
fi
17+
18+
# Move artifacts that will be published to staging dir (build/m2)
19+
./gradlew publish
20+
21+
# Calculate size for artifacts in staging dir
22+
getArtifactSizes
23+
24+
# Upload size metrics
25+
if [ "$UPLOAD" == "true" ]; then
26+
if [ "$RELEASE_METRICS" == "true" ]; then
27+
# For record-keeping
28+
uploadToS3 "$GITHUB_REPOSITORY"-v"$IDENTIFIER".csv
29+
uploadToS3 "$GITHUB_REPOSITORY"-latest.csv
30+
31+
# For display in our OPS dashboard
32+
uploadToCloudwatch "$GITHUB_REPOSITORY"
33+
else
34+
# For downstream consumption in pull requests
35+
uploadToS3 [TEMP]"$GITHUB_REPOSITORY"-pull-request-"$IDENTIFIER".csv
36+
fi
37+
fi
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Gets artifact size metrics from staging dir
2+
getArtifactSizes() {
3+
output="build/reports/metrics/artifact-size-metrics.csv"
4+
5+
# Write CSV header
6+
echo "Jar File,Size (Bytes)" > "$output"
7+
8+
# Find all JARs (exclude sources and javadoc)
9+
# TODO: Calculate KN artifacts sizes
10+
find build/m2 -type f -name "*.jar" ! -name "*-sources.jar" ! -name "*-javadoc.jar" | while read -r jar; do
11+
size=$(stat -c%s "$jar")
12+
echo "\"$jar\",$size" >> "$output"
13+
done
14+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Compares two metrics files, saves results into a file, returns "true" if large diff was found
2+
compareMetrics() {
3+
local pr_file="$1"
4+
local release_file="$2"
5+
local output_file="$3"
6+
7+
# Create header for the Markdown table
8+
echo "| Artifact | Pull Request (bytes) | Latest Release (bytes) | Delta (bytes) | Delta (percentage) |" > "$output_file"
9+
echo "|----------|--------------------|----------------------|---------------|------------------|" >> "$output_file"
10+
11+
local flag=false
12+
13+
# Read PR file line by line (skip header)
14+
tail -n +2 "$pr_file" | while IFS=',' read -r artifact pr_size; do
15+
# Trim whitespace
16+
artifact=$(echo "$artifact" | xargs)
17+
pr_size=$(echo "$pr_size" | xargs)
18+
19+
# Find corresponding artifact in release file
20+
release_size=$(awk -F',' -v art="$artifact" 'NR>1 && $1 ~ art {gsub(/ /,"",$2); print $2}' "$release_file")
21+
22+
# Skip if artifact not found in release file
23+
[ -z "$release_size" ] && continue
24+
25+
# Compute delta and percentage
26+
delta=$((pr_size - release_size))
27+
abs_delta=${delta#-}
28+
percent=$((100 * abs_delta / release_size))
29+
30+
# Add row to Markdown table
31+
echo "| $artifact | $pr_size | $release_size | $delta | $percent% |" >> "$output_file"
32+
33+
# Check if delta > 5%
34+
if [ "$percent" -gt 5 ]; then
35+
flag=true
36+
fi
37+
done
38+
39+
$flag && echo "true"
40+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
3+
source ./compare.sh
4+
source ../s3.sh
5+
source ../setup.sh
6+
setup
7+
8+
# Get metrics from pull request and from latest release
9+
if [ "$DOWNLOAD" == "true" ]; then
10+
downloadFromS3 [TEMP]"$GITHUB_REPOSITORY"-pull-request-"$IDENTIFIER".csv ./current.csv
11+
fi
12+
downloadFromS3 "$GITHUB_REPOSITORY"-latest.csv ./latest.csv
13+
14+
# Compare metrics and save if large diff was found
15+
export LARGE_DIFF=$(compareMetrics current.csv latest.csv comparisson.md)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Owned by: aws-kotlin-sdk+ci
2+
S3_ARTIFACT_SIZE_METRICS_BUCKET="artifact-size-metrics"
3+
4+
# Uploads metrics to the metrics bucket under the specified file name
5+
uploadToS3() {
6+
aws s3 cp "$1" s3://"$S3_ARTIFACT_SIZE_METRICS_BUCKET"/"$2"
7+
}
8+
9+
# Downloads metrics from the metrics bucket to the specified local file
10+
downloadFromS3() {
11+
aws s3 cp s3://"$S3_ARTIFACT_SIZE_METRICS_BUCKET"/"$1" "$2"
12+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
setup() {
2+
# Exit if non zero exit code or if env var is missing
3+
set -u
4+
set -e
5+
}

0 commit comments

Comments
 (0)