Repository navigation
Expand file tree
/
Copy pathresult_analysis.py
More file actions
359 lines (281 loc) · 11.1 KB
/
Copy pathresult_analysis.py
File metadata and controls
359 lines (281 loc) · 11.1 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env python3
from __future__ import annotations
import json
import sys
from pathlib import Path
from typing import Any
_SCRIPT_DIR = Path(__file__).resolve().parent
_EXAMPLES_DIR = _SCRIPT_DIR.parent
sys.path.insert(0, str(_EXAMPLES_DIR))
from benchbox.platforms.duckdb import DuckDBAdapter
from benchbox.tpch import TPCH
def generate_sample_results():
print("=" * 70)
print("GENERATING SAMPLE RESULTS")
print("=" * 70)
print()
print("Creating two benchmark runs for comparison...")
print(" Run 1: Baseline (small scale factor)")
print(" Run 2: Recent (same benchmark, simulated variation)")
print()
benchmark = TPCH(
scale_factor=0.01,
output_dir=Path("./benchmark_runs/features/analysis"),
force_regenerate=False,
)
benchmark.generate_data()
print("Running baseline benchmark...")
adapter = DuckDBAdapter(database_path=":memory:")
baseline_results = adapter.run_benchmark(
benchmark,
test_execution_type="power",
query_subset=["1", "3", "6", "12", "14"],
)
baseline_output = Path("./benchmark_runs/features/analysis/baseline")
baseline_output.mkdir(parents=True, exist_ok=True)
baseline_file = baseline_output / "results.json"
with open(baseline_file, "w", encoding="utf-8") as f:
json.dump(baseline_results.model_dump(), f, indent=2)
print(f"✓ Baseline saved to {baseline_file}")
print()
print("Running recent benchmark...")
recent_results = adapter.run_benchmark(
benchmark, test_execution_type="power", query_subset=["1", "3", "6", "12", "14"]
)
recent_output = Path("./benchmark_runs/features/analysis/recent")
recent_output.mkdir(parents=True, exist_ok=True)
recent_file = recent_output / "results.json"
with open(recent_file, "w", encoding="utf-8") as f:
json.dump(recent_results.model_dump(), f, indent=2)
print(f"✓ Recent saved to {recent_file}")
print()
return baseline_file, recent_file
def load_results(result_path: Path) -> dict[str, Any]:
with open(result_path, encoding="utf-8") as f:
return json.load(f)
def compare_overall_metrics(baseline: dict[str, Any], recent: dict[str, Any]):
print("=" * 70)
print("OVERALL METRICS COMPARISON")
print("=" * 70)
print()
baseline_time = baseline.get("total_execution_time", 0)
recent_time = recent.get("total_execution_time", 0)
time_diff = recent_time - baseline_time
percent_change = (time_diff / baseline_time * 100) if baseline_time > 0 else 0
print("Total Execution Time:")
print(f" Baseline: {baseline_time:.2f}s")
print(f" Recent: {recent_time:.2f}s")
print(f" Difference: {time_diff:+.2f}s ({percent_change:+.1f}%)")
if percent_change > 5:
print(f" ⚠️ REGRESSION: {percent_change:.1f}% slower")
elif percent_change < -5:
print(f" ✓ IMPROVEMENT: {-percent_change:.1f}% faster")
else:
print(" ✓ STABLE: Within 5% tolerance")
print()
baseline_queries = baseline.get("total_queries", 0)
recent_queries = recent.get("total_queries", 0)
print("Query Count:")
print(f" Baseline: {baseline_queries}")
print(f" Recent: {recent_queries}")
if baseline_queries != recent_queries:
print(" ⚠️ Query count mismatch!")
print()
def compare_query_results(baseline: dict[str, Any], recent: dict[str, Any]):
print("=" * 70)
print("QUERY-BY-QUERY COMPARISON")
print("=" * 70)
print()
baseline_queries = {q["query_name"]: q for q in baseline.get("query_results", [])}
recent_queries = {q["query_name"]: q for q in recent.get("query_results", [])}
print(f"{'Query':<10} {'Baseline':<12} {'Recent':<12} {'Change':<12} {'Status':<15}")
print("-" * 70)
regressions = []
improvements = []
stable = []
for query_name in sorted(baseline_queries.keys()):
if query_name not in recent_queries:
print(f"{query_name:<10} {'Present':<12} {'MISSING':<12} {'N/A':<12} {'⚠️ MISSING':<15}")
continue
baseline_q = baseline_queries[query_name]
recent_q = recent_queries[query_name]
baseline_time = baseline_q.get("execution_time", 0)
recent_time = recent_q.get("execution_time", 0)
if baseline_time > 0:
time_diff = recent_time - baseline_time
percent_change = (time_diff / baseline_time) * 100
if percent_change > 10:
status = "⚠️ REGRESSION"
regressions.append((query_name, percent_change))
elif percent_change < -10:
status = "✓ IMPROVED"
improvements.append((query_name, -percent_change))
else:
status = "✓ STABLE"
stable.append(query_name)
print(
f"{query_name:<10} {baseline_time:>10.3f}s {recent_time:>10.3f}s {percent_change:>+10.1f}% {status:<15}"
)
print("-" * 70)
print()
total = len(baseline_queries)
print("Summary:")
print(f" Total queries: {total}")
print(f" Regressions (>10%): {len(regressions)} ({len(regressions) / total * 100:.0f}%)")
print(f" Improvements (>10%): {len(improvements)} ({len(improvements) / total * 100:.0f}%)")
print(f" Stable (±10%): {len(stable)} ({len(stable) / total * 100:.0f}%)")
print()
if regressions:
print("⚠️ Worst Regressions:")
for query_name, percent in sorted(regressions, key=lambda x: x[1], reverse=True)[:3]:
print(f" {query_name}: {percent:+.1f}%")
print()
if improvements:
print("✓ Best Improvements:")
for query_name, percent in sorted(improvements, key=lambda x: x[1], reverse=True)[:3]:
print(f" {query_name}: {percent:.1f}% faster")
print()
return regressions, improvements, stable
def detect_regressions(baseline: dict[str, Any], recent: dict[str, Any], threshold: float = 10.0):
print("=" * 70)
print(f"REGRESSION DETECTION (Threshold: {threshold}%)")
print("=" * 70)
print()
baseline_time = baseline.get("total_execution_time", 0)
recent_time = recent.get("total_execution_time", 0)
if baseline_time == 0:
print("⚠️ Cannot detect regressions: baseline time is 0")
return False
percent_change = ((recent_time - baseline_time) / baseline_time) * 100
print(f"Overall Performance Change: {percent_change:+.1f}%")
if percent_change > threshold:
print(f"❌ REGRESSION DETECTED: {percent_change:.1f}% slower than baseline")
print(f" Baseline: {baseline_time:.2f}s")
print(f" Recent: {recent_time:.2f}s")
print()
print("Recommended actions:")
print(" 1. Review recent code changes")
print(" 2. Check for environmental factors (resource contention)")
print(" 3. Run again to verify it's not a transient issue")
print(" 4. Analyze query-by-query results to isolate problem")
print()
return True
else:
print(f"✓ No regression detected (within {threshold}% threshold)")
print()
return False
def show_result_format():
print("=" * 70)
print("RESULT JSON FORMAT")
print("=" * 70)
print()
print("Result files contain:")
print("""
{
"benchmark_name": "tpch",
"platform": "duckdb",
"timestamp": "2024-01-15T10:30:00",
"total_execution_time": 12.45,
"total_queries": 22,
"average_query_time": 0.57,
"query_results": [
{
"query_name": "Q1",
"execution_time": 0.234,
"status": "success",
"rows_returned": 4
},
...
],
"configuration": {
"scale_factor": 1.0,
"test_execution_type": "power"
}
}
""")
def show_analysis_strategies():
print("=" * 70)
print("ANALYSIS STRATEGIES")
print("=" * 70)
print()
print("1. CI/CD REGRESSION DETECTION")
print(" Compare: Current PR vs main branch")
print(" Threshold: 10-20% overall, 25-50% per-query")
print(" Action: Fail build if regression detected")
print()
print("2. PERFORMANCE TREND ANALYSIS")
print(" Compare: Multiple runs over time (daily/weekly)")
print(" Threshold: Look for trends, not single-point changes")
print(" Action: Alert if consistent degradation over 3+ runs")
print()
print("3. PLATFORM COMPARISON")
print(" Compare: Same benchmark on different platforms")
print(" Threshold: N/A (absolute comparison)")
print(" Action: Document relative performance characteristics")
print()
print("4. OPTIMIZATION VALIDATION")
print(" Compare: Before/after optimization")
print(" Threshold: Look for improvements in target queries")
print(" Action: Verify optimization achieved desired effect")
print()
print("5. SCALE FACTOR ANALYSIS")
print(" Compare: Same benchmark at different scales")
print(" Threshold: Check for linear/sub-linear scaling")
print(" Action: Identify queries that don't scale well")
print()
def main() -> int:
print()
print("=" * 70)
print("BENCHBOX FEATURE: RESULT ANALYSIS")
print("=" * 70)
print()
print("This example shows how to load and compare benchmark results")
print("to detect regressions and analyze performance changes.")
print()
baseline_file, recent_file = generate_sample_results()
print("=" * 70)
print("LOADING RESULTS")
print("=" * 70)
print()
print(f"Loading baseline from: {baseline_file}")
print(f"Loading recent from: {recent_file}")
print()
baseline = load_results(baseline_file)
recent = load_results(recent_file)
compare_overall_metrics(baseline, recent)
compare_query_results(baseline, recent)
has_regression = detect_regressions(baseline, recent, threshold=10.0)
show_result_format()
show_analysis_strategies()
print("=" * 70)
print("SUMMARY")
print("=" * 70)
print()
print("You learned how to:")
print(" ✓ Load benchmark results from JSON files")
print(" ✓ Compare overall performance metrics")
print(" ✓ Analyze query-by-query changes")
print(" ✓ Detect performance regressions")
print(" ✓ Choose appropriate analysis strategies")
print()
print("Next steps:")
print(" • Integrate with CI/CD:")
print(" if detect_regressions(baseline, recent, threshold=15):")
print(" sys.exit(1) # Fail build")
print()
print(" • Track trends over time:")
print(" results = [load_results(f) for f in result_files]")
print(" plot_performance_trend(results)")
print()
print(" • Compare across platforms:")
print(" duckdb_results = load_results('duckdb/results.json')")
print(" clickhouse_results = load_results('clickhouse/results.json')")
print(" compare_platforms(duckdb_results, clickhouse_results)")
print()
print(" • Use with unified_runner.py:")
print(" python unified_runner.py ... --formats json")
print(" python result_analysis.py baseline.json recent.json")
print()
return 1 if has_regression else 0
if __name__ == "__main__":
raise SystemExit(main())