Skip to content

Commit 669b3b0

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

File tree

1 file changed

+122
-6
lines changed

1 file changed

+122
-6
lines changed

.github/workflows/benchmark.yaml

Lines changed: 122 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,130 @@ 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
73+
uses: actions/upload-artifact@v4
74+
with:
75+
name: benchmark-metrics
76+
path: /tmp/artifacts/prometheus/
77+
78+
start-prometheus:
79+
needs: 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 Benchmark Metrics
88+
uses: actions/download-artifact@v4
89+
with:
90+
name: benchmark-metrics
91+
path: ./
92+
93+
- name: List Downloaded Metrics files
94+
run: ls -lh
95+
96+
- name: Set Up Prometheus Config
97+
run: |
98+
cat << 'EOF' > prometheus.yml
99+
global:
100+
scrape_interval: 5s
101+
scrape_configs:
102+
- job_name: 'benchmark_metrics'
103+
static_configs:
104+
- targets: ['localhost:9000']
105+
EOF
106+
107+
- name: Run Prometheus
108+
run: |
109+
docker run -d --name prometheus \
110+
-v ${{ github.workspace }}/prometheus.yml:/etc/prometheus/prometheus.yml \
111+
-v ${{ github.workspace }}/prometheus-data:/prometheus \
112+
prom/prometheus --config.file=/etc/prometheus/prometheus.yml \
113+
--storage.tsdb.path=/prometheus --storage.tsdb.retention.time=1h
114+
115+
- name: Start HTTP Server to Expose Metrics
116+
run: |
117+
cat << 'EOF' > server.py
118+
from http.server import SimpleHTTPRequestHandler, HTTPServer
119+
120+
class MetricsHandler(SimpleHTTPRequestHandler):
121+
def do_GET(self):
122+
if self.path == "/metrics":
123+
self.send_response(200)
124+
self.send_header("Content-type", "text/plain")
125+
self.end_headers()
126+
with open("prometheus_metrics.txt", "r") as f:
127+
self.wfile.write(f.read().encode())
128+
else:
129+
self.send_response(404)
130+
self.end_headers()
131+
132+
if __name__ == "__main__":
133+
server = HTTPServer(('0.0.0.0', 9000), MetricsHandler)
134+
print("Serving on port 9000...")
135+
server.serve_forever()
136+
EOF
137+
138+
nohup python3 server.py &
139+
140+
- name: Wait for Prometheus to Collect Data
141+
run: sleep 30
142+
143+
- name: Trigger Prometheus Snapshot
144+
run: |
145+
curl -X POST http://localhost:9090/api/v1/admin/tsdb/snapshot
146+
147+
- name: Stop Prometheus
148+
run: docker stop prometheus
149+
150+
- name: Find and Upload Prometheus Snapshot
151+
run: |
152+
SNAPSHOT_PATH=$(ls -td /prometheus/snapshots/* | head -1)
153+
echo "Prometheus snapshot stored in: $SNAPSHOT_PATH"
154+
tar -czf prometheus_snapshot.tar.gz -C "$SNAPSHOT_PATH" .
155+
mv prometheus_snapshot.tar.gz $GITHUB_WORKSPACE/
156+
157+
- name: Upload Prometheus Snapshot
42158
uses: actions/upload-artifact@v4
43159
with:
44-
name: benchmark-artifacts
45-
path: /tmp/artifacts/
160+
name: prometheus-snapshot
161+
path: prometheus_snapshot.tar.gz
46162

0 commit comments

Comments
 (0)