Skip to content

Commit 9ff15da

Browse files
committed
ci: make performance regression script more robust with shebang and error guards
1 parent b14774c commit 9ff15da

1 file changed

Lines changed: 24 additions & 8 deletions

File tree

scripts/check_perf_regression.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
1+
#!/usr/bin/env python3
12
import json
23
import sys
34
import os
45

56
def check_regression(current_file, baseline_file, threshold=0.2):
6-
if not os.path.exists(baseline_file):
7-
print(f"No baseline found at {baseline_file}. Skipping comparison.")
8-
return True
9-
7+
# Load current results - failure here is a fatal error
108
try:
119
with open(current_file) as f:
1210
current = json.load(f)
11+
except Exception as e:
12+
print(f"Error loading current performance results from {current_file}: {e}")
13+
return False
14+
15+
# Load baseline results - missing file is handled gracefully
16+
try:
1317
with open(baseline_file) as f:
1418
baseline = json.load(f)
15-
except Exception as e:
16-
print(f"Error loading JSON: {e}")
19+
except FileNotFoundError:
20+
print(f"No baseline found at {baseline_file}. Skipping comparison.")
1721
return True
22+
except Exception as e:
23+
print(f"Error loading baseline performance results from {baseline_file}: {e}")
24+
return False
1825

1926
regressions = []
2027

2128
# Create map of baseline metrics
22-
base_map = {b['name']: b['real_time'] for b in baseline['benchmarks']}
29+
base_map = {b['name']: b.get('real_time') for b in baseline['benchmarks']}
2330

2431
print(f"{'Benchmark':<40} | {'Old (ns)':<12} | {'New (ns)':<12} | {'Change':<10}")
2532
print("-" * 85)
@@ -28,8 +35,17 @@ def check_regression(current_file, baseline_file, threshold=0.2):
2835
name = b['name']
2936
if name in base_map:
3037
old_time = base_map[name]
31-
new_time = b['real_time']
38+
new_time = b.get('real_time')
3239

40+
if old_time is None or new_time is None:
41+
print(f"{name:<40} | {'N/A':<12} | {'N/A':<12} | {'N/A':>9}")
42+
continue
43+
44+
# Guard against division by zero
45+
if old_time <= 0:
46+
print(f"{name:<40} | {old_time:<12.2f} | {new_time:<12.2f} | {'NEW/ZERO':>9}")
47+
continue
48+
3349
# Increase in time means decrease in performance
3450
change = (new_time - old_time) / old_time
3551
print(f"{name:<40} | {old_time:<12.2f} | {new_time:<12.2f} | {change:>+9.1%}")

0 commit comments

Comments
 (0)