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