-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathrun_benchmarks.py
More file actions
executable file
·145 lines (122 loc) · 4.13 KB
/
Copy pathrun_benchmarks.py
File metadata and controls
executable file
·145 lines (122 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python3
"""
Benchmark Runner Script for SecuScan.
Runs the performance benchmarks, compares results against thresholds,
and exits non-zero if any regressions are detected.
"""
import json
import subprocess
import sys
from pathlib import Path
# ANSI color codes
GREEN = "\033[92m"
RED = "\033[91m"
BOLD = "\033[1m"
RESET = "\033[0m"
def main():
root_dir = Path(__file__).resolve().parents[1]
thresholds_path = (
root_dir / "testing" / "backend" / "benchmarks" / "thresholds.json"
)
results_path = root_dir / "benchmark_results.json"
comparison_path = root_dir / "benchmark_threshold_comparison.json"
# 1. Load thresholds
if not thresholds_path.exists():
print(f"{RED}Error: Thresholds file not found at {thresholds_path}{RESET}")
sys.exit(1)
with open(thresholds_path) as f:
thresholds = json.load(f)
# Remove stale results if they exist from a previous run
if results_path.exists():
try:
results_path.unlink()
except OSError:
pass
# 2. Run pytest benchmarks
print(f"{BOLD}Running SecuScan Performance Benchmarks...{RESET}\n")
cmd = [
sys.executable,
"-m",
"pytest",
str(root_dir / "testing" / "backend" / "benchmarks"),
"-m",
"benchmark",
"-v",
"-s",
]
# Run the tests. We capture output/errors normally.
result = subprocess.run(cmd, cwd=str(root_dir))
if result.returncode != 0 and not results_path.exists():
sys.exit(result.returncode)
# 3. Read results
if not results_path.exists():
print(f"\n{RED}Error: Benchmark run did not produce {results_path}{RESET}")
sys.exit(1)
with open(results_path) as f:
results = json.load(f)
# 4. Compare results against thresholds
print(f"\n{BOLD}=== Performance Benchmark Report ==={RESET}\n")
print(
f"{'Benchmark Metric':<45} | {'Measured':<12} | {'Threshold':<12} | {'Status':<6}"
)
print("-" * 82)
has_regression = False
comparison_report = []
for metric, threshold in thresholds.items():
if metric not in results:
print(f"{metric:<45} | {'N/A':<12} | {threshold:<12} | {RED}MISSING{RESET}")
comparison_report.append(
{
"metric": metric,
"measured": None,
"threshold": threshold,
"status": "MISSING",
}
)
has_regression = True
continue
value = results[metric]
# Check if throughput metric (higher is better) or latency metric (lower is better)
if "throughput" in metric:
passed = value >= threshold
status_str = f"{GREEN}PASS{RESET}" if passed else f"{RED}FAIL{RESET}"
unit = "calls/s"
else:
passed = value <= threshold
status_str = f"{GREEN}PASS{RESET}" if passed else f"{RED}FAIL{RESET}"
unit = "ms"
val_fmt = f"{value:.2f} {unit}"
thresh_fmt = f"{threshold:.2f} {unit}"
# If we failed the threshold, mark regression
if not passed:
has_regression = True
print(f"{metric:<45} | {val_fmt:<12} | {thresh_fmt:<12} | {status_str:<6}")
comparison_report.append(
{
"metric": metric,
"measured": value,
"threshold": threshold,
"status": "PASS" if passed else "FAIL",
}
)
print("\n" + "=" * 82 + "\n")
with open(comparison_path, "w") as f:
json.dump(
{
"benchmarks": comparison_report,
"regression_detected": has_regression,
},
f,
indent=2,
)
print(f"Benchmark comparison saved to {comparison_path}")
if has_regression:
print(
f"{RED}{BOLD}Performance regression detected! One or more metrics exceeded thresholds.{RESET}"
)
sys.exit(1)
else:
print(f"{GREEN}{BOLD}All performance benchmarks passed!{RESET}")
sys.exit(0)
if __name__ == "__main__":
main()