-
Notifications
You must be signed in to change notification settings - Fork 0
152 lines (130 loc) · 5.32 KB
/
bloat.yml
File metadata and controls
152 lines (130 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
name: Cargo Bloat Analysis
on:
pull_request:
push:
branches: [master]
permissions:
contents: read
statuses: write
jobs:
bloat-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: rui314/setup-mold@v1
- uses: Swatinem/rust-cache@v2
- uses: taiki-e/install-action@v2
with:
tool: cargo-bloat
- name: Build base branch
id: bloat_base
if: github.event_name == 'pull_request'
run: |
# Build base branch in separate target directory
git checkout ${{ github.event.pull_request.base.sha }}
CARGO_TARGET_DIR=target-base cargo bloat --release > bloat_base.txt 2>&1 || true
git checkout -
{
echo 'output<<EOF'
cat bloat_base.txt
echo 'EOF'
} >> $GITHUB_OUTPUT
- name: Build PR branch
id: bloat_pr
run: |
# Build PR branch in default target directory
cargo bloat --release > bloat_pr.txt 2>&1 || true
{
echo 'output<<EOF'
cat bloat_pr.txt
echo 'EOF'
} >> $GITHUB_OUTPUT
- name: Compare bloat results
id: compare
if: github.event_name == 'pull_request'
run: |
pr_line=$(grep "\.text section size" bloat_pr.txt || echo "")
base_line=$(grep "\.text section size" bloat_base.txt || echo "")
if [ -z "$pr_line" ] || [ -z "$base_line" ]; then
echo "Error: Could not find .text section size in bloat output"
exit 1
fi
pr_size=$(echo "$pr_line" | grep -oP '\d+\.\d+[KMGT]?i?B' | head -1)
base_size=$(echo "$base_line" | grep -oP '\d+\.\d+[KMGT]?i?B' | head -1)
# Convert to bytes for calculation
pr_bytes=$(echo "$pr_size" | awk '{
val = $1
gsub(/[KMGT]?i?B/, "", val)
if ($1 ~ /TiB/) print val * 1024 * 1024 * 1024 * 1024
else if ($1 ~ /GiB/) print val * 1024 * 1024 * 1024
else if ($1 ~ /MiB/) print val * 1024 * 1024
else if ($1 ~ /KiB/) print val * 1024
else print val
}')
base_bytes=$(echo "$base_size" | awk '{
val = $1
gsub(/[KMGT]?i?B/, "", val)
if ($1 ~ /TiB/) print val * 1024 * 1024 * 1024 * 1024
else if ($1 ~ /GiB/) print val * 1024 * 1024 * 1024
else if ($1 ~ /MiB/) print val * 1024 * 1024
else if ($1 ~ /KiB/) print val * 1024
else print val
}')
# Calculate percentage change
pct_change=$(awk "BEGIN {printf \"%.2f\", (($pr_bytes - $base_bytes) / $base_bytes) * 100}")
# Format with + sign if positive
if (( $(awk "BEGIN {print ($pct_change >= 0)}") )); then
pct_display="+${pct_change}%"
else
pct_display="${pct_change}%"
fi
echo "pct_change=${pct_display}" >> $GITHUB_OUTPUT
echo "pr_size=${pr_size}" >> $GITHUB_OUTPUT
echo "base_size=${base_size}" >> $GITHUB_OUTPUT
{
echo 'comparison<<EOF'
echo "### Size Comparison"
echo "- **Base branch**: $base_size"
echo "- **PR branch**: $pr_size ($pct_display)"
echo ""
echo "### PR Branch Results"
cat bloat_pr.txt
echo ""
echo "### Base Branch Results"
cat bloat_base.txt
echo 'EOF'
} >> $GITHUB_OUTPUT
- name: Display results (PR)
if: github.event_name == 'pull_request'
run: |
echo "### 📦 Cargo Bloat Comparison" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Binary size change: ${{ steps.compare.outputs.pct_change }}**" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
echo "${{ steps.compare.outputs.comparison }}" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
echo "::notice title=Binary Size Change::${{ steps.compare.outputs.pct_change }} (.text: ${{ steps.compare.outputs.base_size }} → ${{ steps.compare.outputs.pr_size }})"
- name: Update commit status
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
uses: actions/github-script@v7
with:
script: |
await github.rest.repos.createCommitStatus({
owner: context.repo.owner,
repo: context.repo.repo,
sha: context.payload.pull_request.head.sha,
state: 'success',
context: 'Binary Size Change',
description: `${{ steps.compare.outputs.pct_change }} (.text: ${{ steps.compare.outputs.base_size }} → ${{ steps.compare.outputs.pr_size }})`,
target_url: `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`
});
- name: Display results (Push)
if: github.event_name == 'push'
run: |
echo "### 📦 Cargo Bloat Analysis" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
echo "${{ steps.bloat_pr.outputs.output }}" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY