-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathanalysis_examples.py
More file actions
392 lines (301 loc) · 12.7 KB
/
analysis_examples.py
File metadata and controls
392 lines (301 loc) · 12.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
#!/usr/bin/env python3
"""
Examples of using the Grainchain benchmark analysis system
"""
import sys
from datetime import datetime, timedelta
from pathlib import Path
# Add the parent directory to Python path to import grainchain
sys.path.insert(0, str(Path(__file__).parent.parent))
from benchmarks.analysis import (
AnalysisConfig,
BenchmarkComparator,
BenchmarkDataParser,
BenchmarkReporter,
BenchmarkVisualizer,
)
def example_basic_comparison():
"""Example: Basic provider comparison"""
print("=" * 60)
print("EXAMPLE: Basic Provider Comparison")
print("=" * 60)
# Initialize the data parser
parser = BenchmarkDataParser("benchmarks/results")
# Load all available results
results = parser.load_all_results()
print(f"Loaded {len(results)} benchmark results")
if len(results) == 0:
print("No benchmark data found. Run some benchmarks first!")
return
# Get available providers
providers = set()
for result in results:
providers.update(result.provider_results.keys())
print(f"Available providers: {', '.join(sorted(providers))}")
if len(providers) < 2:
print("Need at least 2 providers for comparison")
return
# Compare the first two providers
provider_list = sorted(list(providers))
provider1, provider2 = provider_list[0], provider_list[1]
# Initialize comparator and perform comparison
comparator = BenchmarkComparator(parser)
comparison = comparator.compare_providers(provider1, provider2, days=30)
print("\nComparison Results:")
print(f"Baseline: {comparison.baseline}")
print(f"Target: {comparison.target}")
print(f"Summary: {comparison.summary}")
if comparison.improvements:
print(f"\n{provider2} improvements:")
for metric, value in comparison.improvements.items():
print(f" • {metric}: {value:.2f}")
if comparison.regressions:
print(f"\n{provider2} regressions:")
for metric, value in comparison.regressions.items():
print(f" • {metric}: {value:.2f}")
def example_trend_analysis():
"""Example: Trend analysis over time"""
print("\n" + "=" * 60)
print("EXAMPLE: Trend Analysis")
print("=" * 60)
parser = BenchmarkDataParser("benchmarks/results")
comparator = BenchmarkComparator(parser)
# Get available providers
results = parser.load_all_results()
if not results:
print("No benchmark data found for trend analysis")
return
providers = set()
for result in results:
providers.update(result.provider_results.keys())
if not providers:
print("No providers found in benchmark data")
return
provider = sorted(list(providers))[0]
print(f"Analyzing trends for provider: {provider}")
# Analyze success rate trends
trend = comparator.analyze_time_trends(provider, days=30, metric="success_rate")
print("\nTrend Analysis Results:")
print(f"Metric: {trend.metric_name}")
print(f"Provider: {trend.provider}")
print(f"Time Period: {trend.time_period}")
print(f"Trend Direction: {trend.trend_direction}")
print(f"Trend Strength: {trend.trend_strength:.2f}")
print(f"Data Points: {len(trend.data_points)}")
if trend.statistical_summary:
stats = trend.statistical_summary
print("\nStatistical Summary:")
print(f" Mean: {stats.get('mean', 0):.2f}")
print(f" Std Dev: {stats.get('std_dev', 0):.2f}")
print(f" Min: {stats.get('min', 0):.2f}")
print(f" Max: {stats.get('max', 0):.2f}")
def example_regression_detection():
"""Example: Automatic regression detection"""
print("\n" + "=" * 60)
print("EXAMPLE: Regression Detection")
print("=" * 60)
parser = BenchmarkDataParser("benchmarks/results")
comparator = BenchmarkComparator(parser)
# Detect regressions
regressions = comparator.detect_performance_regressions(
baseline_days=7, comparison_days=7, threshold=0.1
)
if not regressions:
print("✅ No performance regressions detected!")
return
print(f"❌ Found {len(regressions)} performance regression(s):")
for i, regression in enumerate(regressions, 1):
provider = regression.detailed_analysis["provider"]
print(f"\n{i}. Provider: {provider}")
for metric, value in regression.regressions.items():
if metric == "success_rate":
print(f" • Success rate decreased by {value:.1f}%")
elif "time" in metric:
print(
f" • {metric.replace('_', ' ').title()} increased by {value:.2f}s"
)
def example_provider_recommendation():
"""Example: Provider recommendation"""
print("\n" + "=" * 60)
print("EXAMPLE: Provider Recommendation")
print("=" * 60)
parser = BenchmarkDataParser("benchmarks/results")
comparator = BenchmarkComparator(parser)
# Get recommendations for different use cases
use_cases = ["general", "speed", "reliability"]
for use_case in use_cases:
print(f"\n--- Recommendation for {use_case.upper()} use case ---")
recommendation = comparator.recommend_provider(use_case, days=30)
print(f"Recommended Provider: {recommendation.recommended_provider}")
print(f"Confidence Score: {recommendation.confidence_score:.1%}")
if recommendation.reasoning:
print("Reasons:")
for reason in recommendation.reasoning:
print(f" • {reason}")
if recommendation.performance_summary:
summary = recommendation.performance_summary
print("Performance Summary:")
print(f" • Success Rate: {summary.get('success_rate', 0):.1f}%")
print(
f" • Avg Execution Time: {summary.get('avg_execution_time', 0):.2f}s"
)
print(f" • Avg Creation Time: {summary.get('avg_creation_time', 0):.2f}s")
def example_visualization():
"""Example: Creating visualizations"""
print("\n" + "=" * 60)
print("EXAMPLE: Visualization Generation")
print("=" * 60)
parser = BenchmarkDataParser("benchmarks/results")
results = parser.load_all_results()
if not results:
print("No benchmark data found for visualization")
return
# Initialize visualizer
visualizer = BenchmarkVisualizer("examples/charts")
print("Generating visualizations...")
# Create performance dashboard
dashboard_path = visualizer.create_performance_dashboard(results)
print(f"✅ Performance dashboard saved to: {dashboard_path}")
# Create interactive dashboard (if plotly is available)
try:
interactive_path = visualizer.create_interactive_dashboard(results)
if interactive_path:
print(f"✅ Interactive dashboard saved to: {interactive_path}")
except Exception as e:
print(f"⚠️ Interactive dashboard not available: {e}")
# Export chart data
data_path = visualizer.export_chart_data(results)
print(f"✅ Chart data exported to: {data_path}")
# If we have multiple providers, create a comparison chart
providers = set()
for result in results:
providers.update(result.provider_results.keys())
if len(providers) >= 2:
provider_list = sorted(list(providers))
comparator = BenchmarkComparator(parser)
comparison = comparator.compare_providers(provider_list[0], provider_list[1])
chart_path = visualizer.create_provider_comparison_chart(comparison)
print(f"✅ Provider comparison chart saved to: {chart_path}")
def example_report_generation():
"""Example: Generating comprehensive reports"""
print("\n" + "=" * 60)
print("EXAMPLE: Report Generation")
print("=" * 60)
parser = BenchmarkDataParser("benchmarks/results")
results = parser.load_all_results()
if not results:
print("No benchmark data found for report generation")
return
# Initialize reporter
reporter = BenchmarkReporter("examples/reports")
print("Generating reports...")
# Generate markdown report
md_report = reporter.generate_comprehensive_report(
results, format="markdown", include_charts=False
)
print(f"✅ Markdown report saved to: {md_report}")
# Generate HTML report with charts
html_report = reporter.generate_comprehensive_report(
results, format="html", include_charts=True
)
print(f"✅ HTML report saved to: {html_report}")
# Generate comparison report if we have multiple providers
providers = set()
for result in results:
providers.update(result.provider_results.keys())
if len(providers) >= 2:
provider_list = sorted(list(providers))
comparator = BenchmarkComparator(parser)
comparison = comparator.compare_providers(provider_list[0], provider_list[1])
comparison_report = reporter.generate_comparison_report(
comparison, format="html"
)
print(f"✅ Comparison report saved to: {comparison_report}")
def example_configuration():
"""Example: Working with configuration"""
print("\n" + "=" * 60)
print("EXAMPLE: Configuration Management")
print("=" * 60)
# Load default configuration
config = AnalysisConfig()
print("Current Configuration:")
print(f" Time Range Days: {config.time_range_days}")
print(f" Comparison Threshold: {config.comparison_threshold}")
print(f" Chart Format: {config.preferred_chart_format}")
print(f" Report Format: {config.preferred_report_format}")
# Modify configuration
print("\nModifying configuration...")
config.set("default_settings.time_range_days", 60)
config.set("default_settings.comparison_threshold", 0.05)
print(f" New Time Range Days: {config.time_range_days}")
print(f" New Comparison Threshold: {config.comparison_threshold}")
# Get provider display names
print("\nProvider Display Names:")
for provider in ["local", "e2b", "modal", "daytona", "morph"]:
display_name = config.get_provider_display_name(provider)
color = config.get_provider_color(provider)
print(f" {provider} -> {display_name} (color: {color})")
# Save configuration to a custom file
custom_config_path = Path("examples/custom_analysis_config.json")
config.save(custom_config_path)
print(f"\n✅ Custom configuration saved to: {custom_config_path}")
def example_data_filtering():
"""Example: Advanced data filtering and analysis"""
print("\n" + "=" * 60)
print("EXAMPLE: Advanced Data Filtering")
print("=" * 60)
parser = BenchmarkDataParser("benchmarks/results")
# Get all results
all_results = parser.load_all_results()
print(f"Total benchmark results: {len(all_results)}")
if not all_results:
print("No benchmark data found")
return
# Filter by date range
end_date = datetime.now()
start_date = end_date - timedelta(days=7)
recent_results = parser.get_results_by_date_range(start_date, end_date)
print(f"Results from last 7 days: {len(recent_results)}")
# Filter by provider
providers = set()
for result in all_results:
providers.update(result.provider_results.keys())
for provider in sorted(providers):
provider_results = parser.get_results_by_provider(provider)
print(f"Results for {provider}: {len(provider_results)}")
# Get latest result
latest_result = parser.get_latest_result()
if latest_result:
print("\nLatest benchmark:")
print(f" Timestamp: {latest_result.timestamp}")
print(f" Duration: {latest_result.duration_seconds:.1f}s")
print(f" Providers: {', '.join(latest_result.providers_tested)}")
print(f" Test Scenarios: {latest_result.test_scenarios}")
def main():
"""Run all examples"""
print("Grainchain Benchmark Analysis Examples")
print("=" * 60)
# Create output directories
Path("examples/charts").mkdir(parents=True, exist_ok=True)
Path("examples/reports").mkdir(parents=True, exist_ok=True)
try:
# Run all examples
example_basic_comparison()
example_trend_analysis()
example_regression_detection()
example_provider_recommendation()
example_visualization()
example_report_generation()
example_configuration()
example_data_filtering()
print("\n" + "=" * 60)
print("✅ All examples completed successfully!")
print(
"Check the examples/charts and examples/reports directories for generated files."
)
except Exception as e:
print(f"\n❌ Error running examples: {e}")
print("Make sure you have benchmark data in benchmarks/results/")
print("Run 'grainchain benchmark' to generate some test data first.")
if __name__ == "__main__":
main()