forked from runpod/runpod-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompare_benchmarks.py
More file actions
executable file
·174 lines (137 loc) · 5.37 KB
/
compare_benchmarks.py
File metadata and controls
executable file
·174 lines (137 loc) · 5.37 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python3
"""
Compare cold start benchmark results between two runs.
Usage:
python scripts/compare_benchmarks.py baseline.json optimized.json
python scripts/compare_benchmarks.py benchmark_results/cold_start_1234.json benchmark_results/cold_start_5678.json
"""
import json
import sys
from pathlib import Path
def load_benchmark(file_path: str) -> dict:
"""Load benchmark results from JSON file."""
with open(file_path) as f:
return json.load(f)
def calculate_improvement(baseline: float, optimized: float) -> dict:
"""Calculate improvement metrics."""
diff = baseline - optimized
percent = (diff / baseline) * 100 if baseline > 0 else 0
return {
"diff_ms": round(diff, 2),
"percent": round(percent, 2),
"improved": diff > 0,
}
def compare_benchmarks(baseline_file: str, optimized_file: str):
"""Compare two benchmark results and print analysis."""
baseline = load_benchmark(baseline_file)
optimized = load_benchmark(optimized_file)
print("=" * 70)
print("COLD START BENCHMARK COMPARISON")
print("=" * 70)
print(f"\nBaseline: {baseline_file}")
print(f"Optimized: {optimized_file}")
print()
# Compare main measurements
print("IMPORT TIME COMPARISON")
print("-" * 70)
print(
f"{'Metric':<25} {'Baseline':>12} {'Optimized':>12} {'Δ ms':>10} {'Δ %':>8}"
)
print("-" * 70)
measurements = baseline["measurements"]
opt_measurements = optimized["measurements"]
total_improvement_ms = 0
total_baseline_ms = 0
for key in sorted(measurements.keys()):
if key in opt_measurements:
baseline_val = measurements[key]["mean"]
optimized_val = opt_measurements[key]["mean"]
improvement = calculate_improvement(baseline_val, optimized_val)
symbol = "↓" if improvement["improved"] else "↑"
color = "\033[92m" if improvement["improved"] else "\033[91m"
reset = "\033[0m"
print(
f"{key:<25} {baseline_val:>10.2f}ms {optimized_val:>10.2f}ms "
f"{color}{symbol}{improvement['diff_ms']:>8.2f}ms {improvement['percent']:>6.2f}%{reset}"
)
if key == "runpod_total":
total_improvement_ms = improvement["diff_ms"]
total_baseline_ms = baseline_val
print("-" * 70)
# Module counts
print("\nMODULE LOAD COMPARISON")
print("-" * 70)
baseline_counts = baseline.get("module_counts", {})
opt_counts = optimized.get("module_counts", {})
if baseline_counts and opt_counts:
total_diff = baseline_counts["total"] - opt_counts["total"]
filtered_diff = baseline_counts["filtered"] - opt_counts["filtered"]
print(f"Total modules loaded:")
print(
f" Baseline: {baseline_counts['total']:>4} Optimized: {opt_counts['total']:>4} Δ: {total_diff:>4}"
)
print(f"Runpod modules loaded:")
print(
f" Baseline: {baseline_counts['filtered']:>4} Optimized: {opt_counts['filtered']:>4} Δ: {filtered_diff:>4}"
)
# Lazy loading checks
print("\nLAZY LOADING STATUS")
print("-" * 70)
checks = [
("paramiko_eagerly_loaded", "Paramiko"),
("ssh_cli_loaded", "SSH CLI"),
]
for key, label in checks:
baseline_loaded = baseline.get(key, False)
opt_loaded = optimized.get(key, False)
baseline_status = "LOADED" if baseline_loaded else "NOT LOADED"
opt_status = "LOADED" if opt_loaded else "NOT LOADED"
if baseline_loaded and not opt_loaded:
status_symbol = "✓ NOW LAZY"
color = "\033[92m"
elif not baseline_loaded and opt_loaded:
status_symbol = "✗ NOW EAGER"
color = "\033[91m"
else:
status_symbol = "- NO CHANGE"
color = "\033[93m"
reset = "\033[0m"
print(
f"{label:<20} Baseline: {baseline_status:<12} Optimized: {opt_status:<12} {color}{status_symbol}{reset}"
)
# Summary
print("\n" + "=" * 70)
print("SUMMARY")
print("=" * 70)
if total_improvement_ms > 0:
percent_improvement = (
total_improvement_ms / total_baseline_ms
) * 100
print(f"✓ Cold start improved by {total_improvement_ms:.2f}ms")
print(
f"✓ That's a {percent_improvement:.1f}% improvement over baseline"
)
print(
f"✓ Baseline: {total_baseline_ms:.2f}ms → Optimized: {total_baseline_ms - total_improvement_ms:.2f}ms"
)
elif total_improvement_ms < 0:
print(
f"✗ Cold start regressed by {abs(total_improvement_ms):.2f}ms"
)
print(" Review changes - performance got worse!")
else:
print("- No significant change in cold start time")
print("=" * 70)
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: uv run python scripts/compare_benchmarks.py <baseline.json> <optimized.json>")
sys.exit(1)
baseline_file = sys.argv[1]
optimized_file = sys.argv[2]
if not Path(baseline_file).exists():
print(f"Error: Baseline file not found: {baseline_file}")
sys.exit(1)
if not Path(optimized_file).exists():
print(f"Error: Optimized file not found: {optimized_file}")
sys.exit(1)
compare_benchmarks(baseline_file, optimized_file)