Skip to content

Commit 9305638

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

File tree

1 file changed

+131
-6
lines changed

1 file changed

+131
-6
lines changed

.github/workflows/benchmark.yaml

+131-6
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,139 @@ 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+
mkdir -p ${{ github.workspace }}/prometheus-data
107+
sudo chown -R 65534:65534 ${{ github.workspace }}/prometheus-data
108+
sudo chmod -R 777 ${{ github.workspace }}/prometheus-data
109+
110+
- name: Run Prometheus
111+
run: |
112+
docker run -d --name prometheus -p 9090:9090 \
113+
-v ${{ github.workspace }}/prometheus.yml:/etc/prometheus/prometheus.yml \
114+
-v ${{ github.workspace }}/prometheus-data:/prometheus \
115+
prom/prometheus --config.file=/etc/prometheus/prometheus.yml \
116+
--storage.tsdb.path=/prometheus --storage.tsdb.retention.time=1h
117+
118+
- name: Wait for Prometheus to start
119+
run: sleep 10
120+
121+
- name: Check Prometheus is running
122+
run: curl -s http://localhost:9090/-/ready || (docker logs prometheus && exit 1)
123+
124+
- name: Start HTTP Server to Expose Metrics
125+
run: |
126+
cat << 'EOF' > server.py
127+
from http.server import SimpleHTTPRequestHandler, HTTPServer
128+
129+
class MetricsHandler(SimpleHTTPRequestHandler):
130+
def do_GET(self):
131+
if self.path == "/metrics":
132+
self.send_response(200)
133+
self.send_header("Content-type", "text/plain")
134+
self.end_headers()
135+
with open("metrics.txt", "r") as f:
136+
self.wfile.write(f.read().encode())
137+
else:
138+
self.send_response(404)
139+
self.end_headers()
140+
141+
if __name__ == "__main__":
142+
server = HTTPServer(('0.0.0.0', 9000), MetricsHandler)
143+
print("Serving on port 9000...")
144+
server.serve_forever()
145+
EOF
146+
147+
nohup python3 server.py &
148+
149+
- name: Wait for Prometheus to Collect Data
150+
run: sleep 30
151+
152+
- name: Trigger Prometheus Snapshot
153+
run: |
154+
curl -X POST http://localhost:9090/api/v1/admin/tsdb/snapshot
155+
156+
- name: Stop Prometheus
157+
run: docker stop prometheus
158+
159+
- name: Find and Upload Prometheus Snapshot
160+
run: |
161+
SNAPSHOT_PATH=$(ls -td /prometheus/snapshots/* | head -1)
162+
echo "Prometheus snapshot stored in: $SNAPSHOT_PATH"
163+
tar -czf prometheus_snapshot.tar.gz -C "$SNAPSHOT_PATH" .
164+
mv prometheus_snapshot.tar.gz $GITHUB_WORKSPACE/
165+
166+
- name: Upload Prometheus Snapshot
42167
uses: actions/upload-artifact@v4
43168
with:
44-
name: benchmark-artifacts
45-
path: /tmp/artifacts/
169+
name: prometheus-snapshot
170+
path: prometheus_snapshot.tar.gz
46171

0 commit comments

Comments
 (0)