-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresult_analysis.py
More file actions
441 lines (354 loc) · 13.7 KB
/
Copy pathresult_analysis.py
File metadata and controls
441 lines (354 loc) · 13.7 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#!/usr/bin/env python3
"""Demonstrate loading and comparing benchmark results.
This example shows how to:
- Load benchmark results from JSON files
- Compare performance across multiple runs
- Detect performance regressions
- Analyze query-by-query changes
- Track performance trends over time
Usage:
python features/result_analysis.py
Key Concepts:
- Result JSON format and structure
- Loading previous benchmark results
- Query-by-query comparison
- Regression detection strategies
- Statistical analysis of performance changes
"""
from __future__ import annotations
import json
import sys
from pathlib import Path
from typing import Any
# Add parent directory to path for imports
_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():
"""Generate two sample benchmark runs for comparison.
In real scenarios, these would be:
- Before/after a code change
- Different platform versions
- Different tuning configurations
- Different time periods
"""
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()
# Create benchmark
benchmark = TPCH(
scale_factor=0.01,
output_dir=Path("./benchmark_runs/features/analysis"),
force_regenerate=False,
)
benchmark.generate_data()
# Run 1: Baseline
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"], # Subset for faster demo
)
baseline_output = Path("./benchmark_runs/features/analysis/baseline")
baseline_output.mkdir(parents=True, exist_ok=True)
baseline_file = baseline_output / "results.json"
# Save baseline results
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()
# Run 2: Recent (simulated with slight variations)
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"
# Save recent results
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]:
"""Load benchmark results from JSON file.
The result JSON contains:
- Metadata (benchmark name, platform, timestamp)
- Overall metrics (total time, query count)
- Query-by-query results (execution times, status)
- Configuration details
"""
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]):
"""Compare overall benchmark metrics between two runs."""
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()
# Query counts
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]):
"""Compare query-by-query performance between runs.
This is the most important analysis for identifying:
- Which queries regressed
- Which queries improved
- Query performance variance
"""
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", [])}
# Table header
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
# Classify change
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()
# Summary
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()
# Highlight worst regressions
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()
# Highlight best improvements
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):
"""Detect performance regressions above a threshold.
This is useful for CI/CD pipelines where you want to:
- Fail builds if performance degrades significantly
- Alert teams to performance changes
- Track regression trends
Args:
baseline: Baseline benchmark results
recent: Recent benchmark results
threshold: Percent change threshold for regression (default 10%)
Returns:
True if regressions detected, False otherwise
"""
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():
"""Show the structure of result JSON files."""
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():
"""Show different analysis strategies for various use cases."""
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:
"""Demonstrate result analysis workflow."""
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()
# Generate sample results
baseline_file, recent_file = generate_sample_results()
# Load 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
compare_overall_metrics(baseline, recent)
# Compare query-by-query
compare_query_results(baseline, recent)
# Detect regressions
has_regression = detect_regressions(baseline, recent, threshold=10.0)
# Show result format
show_result_format()
# Show analysis strategies
show_analysis_strategies()
# Summary
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()
# Exit with error code if regression detected (for CI/CD demo)
return 1 if has_regression else 0
if __name__ == "__main__":
raise SystemExit(main())