Skip to content

Commit 0146edb

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

File tree

1 file changed

+196
-7
lines changed

1 file changed

+196
-7
lines changed

.github/workflows/benchmark.yaml

Lines changed: 196 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,203 @@ 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: Debug Artifact Info
88+
run: |
89+
echo "Run ID: 13239200374"
90+
echo "Artifact Name: prometheus-snapshot"
91+
92+
- name: Download Prometheus Snapshot
93+
uses: actions/download-artifact@v4
94+
with:
95+
name: prometheus-snapshot
96+
path: ./
97+
run-id: 13239200374
98+
99+
- name: Download Benchmark Metrics
100+
uses: actions/download-artifact@v4
101+
with:
102+
name: benchmark-metrics
103+
path: ./
104+
105+
- name: Set Up Prometheus Config
106+
run: |
107+
cat << 'EOF' > prometheus.yml
108+
global:
109+
scrape_interval: 5s
110+
scrape_configs:
111+
- job_name: 'benchmark_metrics'
112+
static_configs:
113+
- targets: ['localhost:9000']
114+
EOF
115+
mkdir -p ${{ github.workspace }}/prometheus-data
116+
sudo chown -R 65534:65534 ${{ github.workspace }}/prometheus-data
117+
sudo chmod -R 777 ${{ github.workspace }}/prometheus-data
118+
119+
- name: Extract and Restore Prometheus Snapshot
120+
run: |
121+
SNAPSHOT_FILE="${{ github.workspace }}/prometheus_snapshot.tar.gz"
122+
SNAPSHOT_DIR="${{ github.workspace }}/prometheus-data/snapshots"
123+
124+
if [[ -f "$SNAPSHOT_FILE" ]]; then
125+
mkdir -p "$SNAPSHOT_DIR"
126+
tar -xzf "$SNAPSHOT_FILE" -C "$SNAPSHOT_DIR"
127+
echo "✅ Successfully extracted snapshot to: $SNAPSHOT_DIR"
128+
else
129+
echo "⚠️ WARNING: No snapshot file found. Skipping extraction."
130+
fi
131+
132+
- name: Run Prometheus
133+
run: |
134+
docker run -d --name prometheus -p 9090:9090 \
135+
-v ${{ github.workspace }}/prometheus.yml:/etc/prometheus/prometheus.yml \
136+
-v ${{ github.workspace }}/prometheus-data:/prometheus \
137+
prom/prometheus --config.file=/etc/prometheus/prometheus.yml \
138+
--storage.tsdb.path=/prometheus \
139+
--storage.tsdb.retention.time=1h \
140+
--web.enable-admin-api \
141+
--storage.tsdb.load-wal
142+
143+
- name: Wait for Prometheus to start
144+
run: sleep 10
145+
146+
- name: Check Prometheus is running
147+
run: curl -s http://localhost:9090/-/ready || (docker logs prometheus && exit 1)
46148

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

0 commit comments

Comments
 (0)