-
Notifications
You must be signed in to change notification settings - Fork 123
185 lines (164 loc) · 6.91 KB
/
Copy pathgas-benchmarks.yml
File metadata and controls
185 lines (164 loc) · 6.91 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
name: Gas Benchmark Suite
on:
push:
branches: ["main", "dev"]
paths:
- "stellar-lend/contracts/**"
- "stellar-lend/benchmarks/**"
- ".github/workflows/gas-benchmarks.yml"
pull_request:
branches: ["main", "dev"]
paths:
- "stellar-lend/contracts/**"
- "stellar-lend/benchmarks/**"
- ".github/workflows/gas-benchmarks.yml"
# Allow manual trigger for on-demand profiling
workflow_dispatch:
inputs:
compare_baseline:
description: "Compare against baseline (true/false)"
required: false
default: "true"
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
# ─────────────────────────────────────────────────────────────────────────────
# Gas Benchmark Job
# ─────────────────────────────────────────────────────────────────────────────
gas-benchmarks:
name: Gas Benchmarks — All Contracts
runs-on: ubuntu-latest
env:
CARGO_TERM_COLOR: always
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
- name: Cache cargo registry & build
uses: actions/cache@v4
with:
path: |
~/.cargo/registry
~/.cargo/git
stellar-lend/target
key: ${{ runner.os }}-cargo-bench-${{ hashFiles('stellar-lend/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-bench-
${{ runner.os }}-cargo-
- name: Build benchmark suite
run: |
cd stellar-lend
cargo build --bin run_benchmarks --release 2>&1 | tee benchmark-build.log
echo "Build exit code: $?"
- name: Run gas benchmarks
id: run_benchmarks
run: |
cd stellar-lend
cargo run --bin run_benchmarks -- \
--output benchmark-results.json \
2>&1 | tee benchmark-output.log
echo "Benchmark exit code: $?"
- name: Run benchmarks with baseline comparison
id: baseline_check
if: ${{ github.event.inputs.compare_baseline != 'false' }}
run: |
cd stellar-lend
# Use baseline from repo if it exists and has results
BASELINE="benchmarks/baseline.json"
RESULTS=$(python3 -c "import json; d=json.load(open('$BASELINE')); print(len(d.get('results', [])))" 2>/dev/null || echo "0")
if [ "$RESULTS" -gt "0" ]; then
echo "Comparing against baseline ($RESULTS recorded operations)..."
cargo run --bin run_benchmarks -- \
--compare "$BASELINE" \
--output benchmark-results.json \
2>&1 | tee benchmark-comparison.log
EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ]; then
echo "::error::Gas regression detected! See benchmark-comparison.log for details."
exit $EXIT_CODE
fi
echo "All operations within gas budgets."
else
echo "No baseline results found — skipping regression check."
echo "Run benchmarks locally and commit baseline.json to enable regression detection."
fi
- name: Generate benchmark summary
if: always()
run: |
cd stellar-lend
if [ -f benchmark-results.json ]; then
echo "## Gas Benchmark Results" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "| Contract | Operations | Max Instructions | Avg Instructions | Over Budget |" >> $GITHUB_STEP_SUMMARY
echo "|----------|-----------|-----------------|-----------------|-------------|" >> $GITHUB_STEP_SUMMARY
python3 - <<'EOF'
import json, os
with open("benchmark-results.json") as f:
report = json.load(f)
summary = report.get("summary_by_contract", {})
with open(os.environ["GITHUB_STEP_SUMMARY"], "a") as out:
for contract, s in sorted(summary.items()):
over = s.get("over_budget_count", 0)
status = "✗" if over > 0 else "✓"
out.write(
f"| {contract} | {s['total_operations']} | "
f"{s['max_instructions']:,} | {s['avg_instructions']:,} | "
f"{status} {over} |\n"
)
total = report["total_benchmarks"]
passed = report["passed"]
failed = report["failed"]
out.write(f"\n**Total:** {total} | **Passed:** {passed} | **Failed:** {failed}\n")
EOF
# Also append markdown report to summary if available
if [ -f benchmark-results.md ]; then
echo "" >> $GITHUB_STEP_SUMMARY
echo "---" >> $GITHUB_STEP_SUMMARY
cat benchmark-results.md >> $GITHUB_STEP_SUMMARY
fi
fi
- name: Upload benchmark results
if: always()
uses: actions/upload-artifact@v4
with:
name: gas-benchmark-results-${{ github.sha }}
path: |
stellar-lend/benchmark-results.json
stellar-lend/benchmark-results.md
stellar-lend/benchmarks/history.json
stellar-lend/benchmark-output.log
stellar-lend/benchmark-comparison.log
retention-days: 90
- name: Upload build log
if: failure()
uses: actions/upload-artifact@v4
with:
name: benchmark-build-log
path: stellar-lend/benchmark-build.log
# ── Budget Alert Gate ──────────────────────────────────────────────────
- name: Enforce gas budget gate
if: always()
run: |
cd stellar-lend
if [ ! -f benchmark-results.json ]; then
echo "::error::benchmark-results.json not found — benchmarks may have failed to run."
exit 1
fi
FAILED=$(python3 -c "
import json
with open('benchmark-results.json') as f:
r = json.load(f)
over = [x for x in r['results'] if not x['within_budget'] and x['budget'] > 0]
for o in over:
print(f\" {o['operation']}: {o['instructions']:,} instructions (budget: {o['budget']:,})\")
print(len(over))
" | tail -1)
if [ "$FAILED" -gt "0" ]; then
echo "::error::$FAILED operation(s) exceeded gas budget. See benchmark-results.json for details."
exit 1
fi
echo "All operations within gas budgets."