Skip to content

Commit cdcfe5f

Browse files
committed
use prometheus instead of benchstat
1 parent 001bc32 commit cdcfe5f

File tree

1 file changed

+200
-7
lines changed

1 file changed

+200
-7
lines changed

.github/workflows/benchmark.yaml

Lines changed: 200 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66
- main
77

88
jobs:
9-
benchmark:
9+
run-benchmark:
1010
runs-on: ubuntu-latest
1111
steps:
1212
- name: Checkout code
@@ -33,14 +33,207 @@ jobs:
3333
mkdir -p /tmp/artifacts/
3434
ARTIFACT_PATH=/tmp/artifacts make test-benchmark
3535
36-
- name: Compare with baseline
36+
- name: Convert Benchmark Output to Prometheus Metrics
3737
run: |
38-
go install golang.org/x/perf/cmd/benchstat@latest
39-
benchstat benchmarks/baseline.txt /tmp/artifacts/new.txt | tee /tmp/artifacts/output
38+
mkdir -p /tmp/artifacts/prometheus/
39+
cat << 'EOF' > benchmark_to_prometheus.py
40+
import sys
41+
import re
4042
41-
- name: Upload benchmark results
43+
def parse_benchmark_output(benchmark_output):
44+
metrics = []
45+
for line in benchmark_output.split("\n"):
46+
match = re.match(r"Benchmark([\w\d]+)-\d+\s+\d+\s+([\d]+)\s+ns/op\s+([\d]+)\s+B/op\s+([\d]+)\s+allocs/op", line)
47+
if match:
48+
benchmark_name = match.group(1).lower()
49+
time_ns = match.group(2)
50+
memory_bytes = match.group(3)
51+
allocs = match.group(4)
52+
53+
metrics.append(f"benchmark_{benchmark_name}_ns {time_ns}")
54+
metrics.append(f"benchmark_{benchmark_name}_allocs {allocs}")
55+
metrics.append(f"benchmark_{benchmark_name}_mem_bytes {memory_bytes}")
56+
57+
return "\n".join(metrics)
58+
59+
if __name__ == "__main__":
60+
benchmark_output = sys.stdin.read()
61+
metrics = parse_benchmark_output(benchmark_output)
62+
print(metrics)
63+
EOF
64+
65+
cat /tmp/artifacts/new.txt | python3 benchmark_to_prometheus.py > /tmp/artifacts/prometheus/metrics.txt
66+
67+
# - name: Compare with baseline
68+
# run: |
69+
# go install golang.org/x/perf/cmd/benchstat@latest
70+
# benchstat benchmarks/baseline.txt /tmp/artifacts/new.txt | tee /tmp/artifacts/output
71+
72+
- name: Upload Benchmark Metrics
4273
uses: actions/upload-artifact@v4
4374
with:
44-
name: benchmark-artifacts
45-
path: /tmp/artifacts/
75+
name: benchmark-metrics
76+
path: /tmp/artifacts/prometheus/
77+
78+
run-prometheus:
79+
needs: run-benchmark
80+
runs-on: ubuntu-latest
81+
steps:
82+
- name: Checkout code
83+
uses: actions/checkout@v4
84+
with:
85+
fetch-depth: 0
86+
87+
- name: Download Prometheus Snapshot
88+
run: |
89+
echo "Available Artifacts in this run:"
90+
gh run list --repo operator-framework/operator-controller --limit 5
91+
gh run download --repo operator-framework/operator-controller --name prometheus-snapshot --dir .
92+
env:
93+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
94+
95+
- name: Download Prometheus Snapshot2
96+
uses: actions/download-artifact@v4
97+
with:
98+
name: prometheus-snapshot
99+
path: ./
100+
env:
101+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
102+
103+
- name: Download Benchmark Metrics
104+
uses: actions/download-artifact@v4
105+
with:
106+
name: benchmark-metrics
107+
path: ./
108+
109+
- name: Set Up Prometheus Config
110+
run: |
111+
cat << 'EOF' > prometheus.yml
112+
global:
113+
scrape_interval: 5s
114+
scrape_configs:
115+
- job_name: 'benchmark_metrics'
116+
static_configs:
117+
- targets: ['localhost:9000']
118+
EOF
119+
mkdir -p ${{ github.workspace }}/prometheus-data
120+
sudo chown -R 65534:65534 ${{ github.workspace }}/prometheus-data
121+
sudo chmod -R 777 ${{ github.workspace }}/prometheus-data
122+
123+
- name: Extract and Restore Prometheus Snapshot
124+
run: |
125+
SNAPSHOT_FILE="${{ github.workspace }}/prometheus_snapshot.tar.gz"
126+
SNAPSHOT_DIR="${{ github.workspace }}/prometheus-data/snapshots"
127+
128+
if [[ -f "$SNAPSHOT_FILE" ]]; then
129+
mkdir -p "$SNAPSHOT_DIR"
130+
tar -xzf "$SNAPSHOT_FILE" -C "$SNAPSHOT_DIR"
131+
echo "✅ Successfully extracted snapshot to: $SNAPSHOT_DIR"
132+
else
133+
echo "⚠️ WARNING: No snapshot file found. Skipping extraction."
134+
fi
135+
136+
- name: Run Prometheus
137+
run: |
138+
docker run -d --name prometheus -p 9090:9090 \
139+
-v ${{ github.workspace }}/prometheus.yml:/etc/prometheus/prometheus.yml \
140+
-v ${{ github.workspace }}/prometheus-data:/prometheus \
141+
prom/prometheus --config.file=/etc/prometheus/prometheus.yml \
142+
--storage.tsdb.path=/prometheus \
143+
--storage.tsdb.retention.time=1h \
144+
--web.enable-admin-api \
145+
--storage.tsdb.load-wal
146+
147+
- name: Wait for Prometheus to start
148+
run: sleep 10
149+
150+
- name: Check Prometheus is running
151+
run: curl -s http://localhost:9090/-/ready || (docker logs prometheus && exit 1)
46152

153+
- name: Start HTTP Server to Expose Metrics
154+
run: |
155+
cat << 'EOF' > server.py
156+
from http.server import SimpleHTTPRequestHandler, HTTPServer
157+
158+
class MetricsHandler(SimpleHTTPRequestHandler):
159+
def do_GET(self):
160+
if self.path == "/metrics":
161+
self.send_response(200)
162+
self.send_header("Content-type", "text/plain")
163+
self.end_headers()
164+
with open("metrics.txt", "r") as f:
165+
self.wfile.write(f.read().encode())
166+
else:
167+
self.send_response(404)
168+
self.end_headers()
169+
170+
if __name__ == "__main__":
171+
server = HTTPServer(('0.0.0.0', 9000), MetricsHandler)
172+
print("Serving on port 9000...")
173+
server.serve_forever()
174+
EOF
175+
176+
nohup python3 server.py &
177+
178+
- name: Wait for Prometheus to Collect Data
179+
run: sleep 30
180+
181+
- name: Check Benchmark Metrics Against Threshold
182+
run: |
183+
MAX_TIME_NS=1200000000 # 1.2s
184+
MAX_ALLOCS=4000
185+
MAX_MEM_BYTES=450000
186+
187+
# Query Prometheus Metrics
188+
time_ns=$(curl -s "http://localhost:9090/api/v1/query?query=benchmark_create_cluster_catalog_ns" | jq -r '.data.result[0].value[1]')
189+
allocs=$(curl -s "http://localhost:9090/api/v1/query?query=benchmark_create_cluster_catalog_allocs" | jq -r '.data.result[0].value[1]')
190+
mem_bytes=$(curl -s "http://localhost:9090/api/v1/query?query=benchmark_create_cluster_catalog_mem_bytes" | jq -r '.data.result[0].value[1]')
191+
192+
echo "⏳ Benchmark Execution Time: $time_ns ns"
193+
echo "🛠️ Memory Allocations: $allocs"
194+
echo "💾 Memory Usage: $mem_bytes bytes"
195+
196+
# threshold checking
197+
if (( $(echo "$time_ns > $MAX_TIME_NS" | bc -l) )); then
198+
echo "❌ ERROR: Execution time exceeds threshold!"
199+
exit 1
200+
fi
201+
202+
if (( $(echo "$allocs > $MAX_ALLOCS" | bc -l) )); then
203+
echo "❌ ERROR: Too many memory allocations!"
204+
exit 1
205+
fi
206+
207+
if (( $(echo "$mem_bytes > $MAX_MEM_BYTES" | bc -l) )); then
208+
echo "❌ ERROR: Memory usage exceeds threshold!"
209+
exit 1
210+
fi
211+
212+
echo "✅ All benchmarks passed within threshold!"
213+
214+
- name: Trigger Prometheus Snapshot
215+
run: |
216+
curl -X POST http://localhost:9090/api/v1/admin/tsdb/snapshot || (docker logs prometheus && exit 1)
217+
218+
- name: Find and Upload Prometheus Snapshot
219+
run: |
220+
SNAPSHOT_PATH=$(ls -td ${{ github.workspace }}/prometheus-data/snapshots/* 2>/dev/null | head -1 || echo "")
221+
if [[ -z "$SNAPSHOT_PATH" ]]; then
222+
echo "❌ No Prometheus snapshot found!"
223+
docker logs prometheus
224+
exit 1
225+
fi
226+
227+
echo "✅ Prometheus snapshot stored in: $SNAPSHOT_PATH"
228+
tar -czf $GITHUB_WORKSPACE/prometheus_snapshot.tar.gz -C "$SNAPSHOT_PATH" .
229+
230+
231+
- name: Stop Prometheus
232+
run: docker stop prometheus
233+
234+
- name: Upload Prometheus Snapshot
235+
uses: actions/upload-artifact@v4
236+
with:
237+
name: prometheus-snapshot
238+
path: prometheus_snapshot.tar.gz
239+

0 commit comments

Comments
 (0)