Skip to content

Commit dba035d

Browse files
committed
use prometheus instead of benchstat
1 parent 08a24d1 commit dba035d

File tree

1 file changed

+190
-7
lines changed

1 file changed

+190
-7
lines changed

.github/workflows/benchmark.yaml

+190-7
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,197 @@ 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+
uses: actions/download-artifact@v4
89+
with:
90+
name: prometheus-snapshot
91+
path: ${{ github.workspace }}/
92+
93+
- name: Download Benchmark Metrics
94+
uses: actions/download-artifact@v4
95+
with:
96+
name: benchmark-metrics
97+
path: ./
98+
99+
- name: Set Up Prometheus Config
100+
run: |
101+
cat << 'EOF' > prometheus.yml
102+
global:
103+
scrape_interval: 5s
104+
scrape_configs:
105+
- job_name: 'benchmark_metrics'
106+
static_configs:
107+
- targets: ['localhost:9000']
108+
EOF
109+
mkdir -p ${{ github.workspace }}/prometheus-data
110+
sudo chown -R 65534:65534 ${{ github.workspace }}/prometheus-data
111+
sudo chmod -R 777 ${{ github.workspace }}/prometheus-data
112+
113+
- name: Extract and Restore Prometheus Snapshot
114+
run: |
115+
SNAPSHOT_FILE="${{ github.workspace }}/prometheus_snapshot.tar.gz"
116+
SNAPSHOT_DIR="${{ github.workspace }}/prometheus-data/snapshots"
117+
118+
if [[ -f "$SNAPSHOT_FILE" ]]; then
119+
mkdir -p "$SNAPSHOT_DIR"
120+
tar -xzf "$SNAPSHOT_FILE" -C "$SNAPSHOT_DIR"
121+
echo "✅ Successfully extracted snapshot to: $SNAPSHOT_DIR"
122+
else
123+
echo "⚠️ WARNING: No snapshot file found. Skipping extraction."
124+
fi
125+
126+
- name: Run Prometheus
127+
run: |
128+
docker run -d --name prometheus -p 9090:9090 \
129+
-v ${{ github.workspace }}/prometheus.yml:/etc/prometheus/prometheus.yml \
130+
-v ${{ github.workspace }}/prometheus-data:/prometheus \
131+
prom/prometheus --config.file=/etc/prometheus/prometheus.yml \
132+
--storage.tsdb.path=/prometheus \
133+
--storage.tsdb.retention.time=1h \
134+
--web.enable-admin-api \
135+
--storage.tsdb.load-wal
136+
137+
- name: Wait for Prometheus to start
138+
run: sleep 10
139+
140+
- name: Check Prometheus is running
141+
run: curl -s http://localhost:9090/-/ready || (docker logs prometheus && exit 1)
142+
143+
- name: Start HTTP Server to Expose Metrics
144+
run: |
145+
cat << 'EOF' > server.py
146+
from http.server import SimpleHTTPRequestHandler, HTTPServer
147+
148+
class MetricsHandler(SimpleHTTPRequestHandler):
149+
def do_GET(self):
150+
if self.path == "/metrics":
151+
self.send_response(200)
152+
self.send_header("Content-type", "text/plain")
153+
self.end_headers()
154+
with open("metrics.txt", "r") as f:
155+
self.wfile.write(f.read().encode())
156+
else:
157+
self.send_response(404)
158+
self.end_headers()
159+
160+
if __name__ == "__main__":
161+
server = HTTPServer(('0.0.0.0', 9000), MetricsHandler)
162+
print("Serving on port 9000...")
163+
server.serve_forever()
164+
EOF
46165
166+
nohup python3 server.py &
167+
168+
- name: Wait for Prometheus to Collect Data
169+
run: sleep 30
170+
171+
- name: Check Benchmark Metrics Against Threshold
172+
run: |
173+
MAX_TIME_NS=1200000000 # 1.2s
174+
MAX_ALLOCS=4000
175+
MAX_MEM_BYTES=450000
176+
177+
# Query Prometheus Metrics
178+
time_ns=$(curl -s "http://localhost:9090/api/v1/query?query=benchmark_create_cluster_catalog_ns" | jq -r '.data.result[0].value[1]')
179+
allocs=$(curl -s "http://localhost:9090/api/v1/query?query=benchmark_create_cluster_catalog_allocs" | jq -r '.data.result[0].value[1]')
180+
mem_bytes=$(curl -s "http://localhost:9090/api/v1/query?query=benchmark_create_cluster_catalog_mem_bytes" | jq -r '.data.result[0].value[1]')
181+
182+
echo "⏳ Benchmark Execution Time: $time_ns ns"
183+
echo "🛠️ Memory Allocations: $allocs"
184+
echo "💾 Memory Usage: $mem_bytes bytes"
185+
186+
# threshold checking
187+
if (( $(echo "$time_ns > $MAX_TIME_NS" | bc -l) )); then
188+
echo "❌ ERROR: Execution time exceeds threshold!"
189+
exit 1
190+
fi
191+
192+
if (( $(echo "$allocs > $MAX_ALLOCS" | bc -l) )); then
193+
echo "❌ ERROR: Too many memory allocations!"
194+
exit 1
195+
fi
196+
197+
if (( $(echo "$mem_bytes > $MAX_MEM_BYTES" | bc -l) )); then
198+
echo "❌ ERROR: Memory usage exceeds threshold!"
199+
exit 1
200+
fi
201+
202+
echo "✅ All benchmarks passed within threshold!"
203+
204+
- name: Trigger Prometheus Snapshot
205+
run: |
206+
curl -X POST http://localhost:9090/api/v1/admin/tsdb/snapshot || (docker logs prometheus && exit 1)
207+
208+
- name: Find and Upload Prometheus Snapshot
209+
run: |
210+
SNAPSHOT_PATH=$(ls -td ${{ github.workspace }}/prometheus-data/snapshots/* 2>/dev/null | head -1 || echo "")
211+
if [[ -z "$SNAPSHOT_PATH" ]]; then
212+
echo "❌ No Prometheus snapshot found!"
213+
docker logs prometheus
214+
exit 1
215+
fi
216+
217+
echo "✅ Prometheus snapshot stored in: $SNAPSHOT_PATH"
218+
tar -czf $GITHUB_WORKSPACE/prometheus_snapshot.tar.gz -C "$SNAPSHOT_PATH" .
219+
220+
221+
- name: Stop Prometheus
222+
run: docker stop prometheus
223+
224+
- name: Upload Prometheus Snapshot
225+
uses: actions/upload-artifact@v4
226+
with:
227+
name: prometheus-snapshot
228+
path: prometheus_snapshot.tar.gz
229+

0 commit comments

Comments
 (0)