|
16 | 16 | use std::time::Instant; |
17 | 17 | #[ cfg( feature = "benchmarks" ) ] |
18 | 18 | use unilang::interner::{ StringInterner, intern_command_name }; |
| 19 | +#[ cfg( feature = "benchmarks" ) ] |
| 20 | +use benchkit::prelude::*; |
19 | 21 |
|
20 | 22 | #[ derive( Debug, Clone ) ] |
21 | 23 | #[ cfg( feature = "benchmarks" ) ] |
@@ -226,6 +228,148 @@ fn print_result( result : &StringInterningResult ) |
226 | 228 | println!(); |
227 | 229 | } |
228 | 230 |
|
| 231 | +/// Run statistical analysis benchmarks using benchkit |
| 232 | +#[ cfg( feature = "benchmarks" ) ] |
| 233 | +fn run_statistical_analysis_benchmarks() |
| 234 | +{ |
| 235 | + println!( "📊 String Interning Statistical Analysis (Benchkit)" ); |
| 236 | + println!( "===================================================\n" ); |
| 237 | + |
| 238 | + // Realistic command patterns from typical usage |
| 239 | + let test_commands = vec![ |
| 240 | + vec![ "file", "create" ], |
| 241 | + vec![ "file", "delete" ], |
| 242 | + vec![ "user", "login" ], |
| 243 | + vec![ "user", "logout" ], |
| 244 | + vec![ "system", "status" ], |
| 245 | + vec![ "database", "migrate" ], |
| 246 | + vec![ "cache", "clear" ], |
| 247 | + vec![ "config", "get", "value" ], |
| 248 | + vec![ "config", "set", "key" ], |
| 249 | + vec![ "deploy", "production", "service" ], |
| 250 | + ]; |
| 251 | + |
| 252 | + let command_slices : Vec< &[ &str ] > = test_commands.iter().map( std::vec::Vec::as_slice ).collect(); |
| 253 | + |
| 254 | + // Use benchkit's statistical analysis with multiple measurements (25+ samples) |
| 255 | + println!( "📈 Running statistical analysis with 25 samples per algorithm...\n" ); |
| 256 | + |
| 257 | + // Create measurement config for 25 samples |
| 258 | + let config = MeasurementConfig { |
| 259 | + iterations: 25, |
| 260 | + warmup_iterations: 3, |
| 261 | + max_time: std::time::Duration::from_secs(30), |
| 262 | + }; |
| 263 | + |
| 264 | + // Benchmark 1: String construction (baseline) |
| 265 | + let baseline_result = bench_function_with_config("string_construction", &config, || { |
| 266 | + for slices in &command_slices { |
| 267 | + let _command_name = slices.join("."); // String allocation per call |
| 268 | + } |
| 269 | + }); |
| 270 | + |
| 271 | + // Benchmark 2: String interning (cache miss) |
| 272 | + let interner_miss_result = bench_function_with_config("string_interning_miss", &config, || { |
| 273 | + let interner = StringInterner::new(); |
| 274 | + for slices in &command_slices { |
| 275 | + let _interned = interner.intern_command_name(slices); |
| 276 | + } |
| 277 | + }); |
| 278 | + |
| 279 | + // Benchmark 3: String interning (cache hit - pre-warm cache) |
| 280 | + let interner_hit_result = bench_function_with_config("string_interning_hit", &config, || { |
| 281 | + let interner = StringInterner::new(); |
| 282 | + // Pre-warm cache |
| 283 | + for slices in &command_slices { |
| 284 | + let _interned = interner.intern_command_name(slices); |
| 285 | + } |
| 286 | + // Now measure cache hits |
| 287 | + for slices in &command_slices { |
| 288 | + let _interned = interner.intern_command_name(slices); |
| 289 | + } |
| 290 | + }); |
| 291 | + |
| 292 | + // Benchmark 4: Global interner |
| 293 | + let global_interner_result = bench_function_with_config("global_interner", &config, || { |
| 294 | + for slices in &command_slices { |
| 295 | + let _interned = intern_command_name(slices); |
| 296 | + } |
| 297 | + }); |
| 298 | + |
| 299 | + println!( "🔬 Statistical Analysis Results" ); |
| 300 | + println!( "==============================\n" ); |
| 301 | + |
| 302 | + // Analyze each result with statistical significance testing |
| 303 | + let algorithms = vec![ |
| 304 | + ("String Construction (Baseline)", &baseline_result), |
| 305 | + ("String Interning (Cache Miss)", &interner_miss_result), |
| 306 | + ("String Interning (Cache Hit)", &interner_hit_result), |
| 307 | + ("Global Interner", &global_interner_result), |
| 308 | + ]; |
| 309 | + |
| 310 | + let mut reliable_results: Vec<(&str, &BenchmarkResult, StatisticalAnalysis)> = Vec::new(); |
| 311 | + |
| 312 | + for (name, result) in &algorithms { |
| 313 | + println!( "📊 {name}" ); |
| 314 | + |
| 315 | + if let Ok(analysis) = StatisticalAnalysis::analyze(result, SignificanceLevel::Standard) { |
| 316 | + println!( " Mean Time: {:.2?} ± {:.2?} (95% confidence)", |
| 317 | + analysis.mean_confidence_interval.point_estimate, |
| 318 | + analysis.mean_confidence_interval.margin_of_error ); |
| 319 | + println!( " Coefficient of Variation: {:.1}%", analysis.coefficient_of_variation * 100.0 ); |
| 320 | + println!( " Statistical Power: {:.3}", analysis.statistical_power ); |
| 321 | + println!( " Sample Size: {}", result.times.len() ); |
| 322 | + |
| 323 | + if analysis.is_reliable() { |
| 324 | + println!( " Quality: ✅ Statistically reliable" ); |
| 325 | + reliable_results.push((name, result, analysis)); |
| 326 | + } else { |
| 327 | + println!( " Quality: ⚠️ Not statistically reliable - need more samples" ); |
| 328 | + println!( " Recommendation: Increase sample size to at least {}", |
| 329 | + (25 as f64 * 1.5) as usize ); // Simple heuristic |
| 330 | + } |
| 331 | + } else { |
| 332 | + println!( " Quality: ❌ Statistical analysis failed" ); |
| 333 | + } |
| 334 | + println!(); |
| 335 | + } |
| 336 | + |
| 337 | + // Comparative analysis for reliable results only |
| 338 | + if reliable_results.len() >= 2 { |
| 339 | + println!( "🎯 Performance Comparison (Reliable Results Only)" ); |
| 340 | + println!( "================================================\n" ); |
| 341 | + |
| 342 | + let baseline_analysis = reliable_results.iter() |
| 343 | + .find(|(name, _, _)| name.contains("Baseline")) |
| 344 | + .map(|(_, _, analysis)| analysis); |
| 345 | + |
| 346 | + if let Some(baseline) = baseline_analysis { |
| 347 | + for (name, _result, analysis) in &reliable_results { |
| 348 | + if !name.contains("Baseline") { |
| 349 | + // Compare with baseline using statistical comparison |
| 350 | + if let Ok(comparison) = StatisticalAnalysis::compare( |
| 351 | + &baseline_result, |
| 352 | + _result, |
| 353 | + SignificanceLevel::Standard |
| 354 | + ) { |
| 355 | + let improvement = baseline.mean_confidence_interval.point_estimate.as_nanos() as f64 |
| 356 | + / analysis.mean_confidence_interval.point_estimate.as_nanos() as f64; |
| 357 | + |
| 358 | + if comparison.is_significant { |
| 359 | + println!( "✅ {name}: {:.1}x faster than baseline (statistically significant)", improvement ); |
| 360 | + } else { |
| 361 | + println!( "🔍 {name}: {:.1}x faster than baseline (not statistically significant)", improvement ); |
| 362 | + } |
| 363 | + } |
| 364 | + } |
| 365 | + } |
| 366 | + } |
| 367 | + } else { |
| 368 | + println!( "⚠️ Not enough reliable results for performance comparison" ); |
| 369 | + println!( " Increase sample sizes and rerun for statistical analysis" ); |
| 370 | + } |
| 371 | +} |
| 372 | + |
229 | 373 | #[ cfg( feature = "benchmarks" ) ] |
230 | 374 | fn run_string_interning_benchmarks() |
231 | 375 | { |
@@ -320,6 +464,13 @@ fn run_string_interning_benchmarks() |
320 | 464 | #[ cfg( feature = "benchmarks" ) ] |
321 | 465 | fn main() |
322 | 466 | { |
| 467 | + // Run statistical analysis benchmarks (new benchkit approach) |
| 468 | + run_statistical_analysis_benchmarks(); |
| 469 | + println!( "\n" ); |
| 470 | + |
| 471 | + // Run legacy benchmarks for comparison |
| 472 | + println!( "📚 Legacy Benchmark Results (for comparison)" ); |
| 473 | + println!( "============================================\n" ); |
323 | 474 | run_string_interning_benchmarks(); |
324 | 475 | } |
325 | 476 |
|
|
0 commit comments