benchkit 0.19.0

Lightweight benchmarking toolkit focused on practical performance analysis and report generation. Non-restrictive alternative to criterion, designed for easy integration and markdown report generation.
Documentation
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! Comprehensive demonstration of benchkit applied to `strs_tools`
//!
//! This example shows the transformation from complex criterion-based benchmarks
//! to clean, research-grade benchkit analysis with dramatically reduced code.

#![allow(clippy ::format_push_string)]
#![allow(clippy ::uninlined_format_args)]
#![allow(clippy ::std_instead_of_core)]
#![allow(clippy ::unnecessary_wraps)]
#![allow(clippy ::useless_format)]
#![allow(clippy ::redundant_closure_for_method_calls)]
#![allow(clippy ::cast_possible_truncation)]
#![allow(clippy ::cast_sign_loss)]

use benchkit ::prelude :: *;

use std ::collections ::HashMap;

type Result< T > = core ::result ::Result< T, Box<dyn core ::error ::Error >>;

fn main() -> Result< () >
{
  println!("🚀 Benchkit Applied to strs_tools: The Complete Transformation");
  println!("================================================================");
  println!();

  // 1. Data Generation Showcase
  println!("1️⃣ Advanced Data Generation");
  println!("---------------------------");
  demonstrate_data_generation();
  println!();

  // 2. Memory Tracking Showcase
  println!("2️⃣ Memory Allocation Tracking");
  println!("-----------------------------");
  demonstrate_memory_tracking();
  println!();

  // 3. Throughput Analysis Showcase
  println!("3️⃣ Throughput Analysis");
  println!("----------------------");
  demonstrate_throughput_analysis()?;
  println!();

  // 4. Statistical Analysis Showcase
  #[ cfg(feature = "statistical_analysis") ]
  {
  println!("4️⃣ Research-Grade Statistical Analysis");
  println!("-------------------------------------");
  demonstrate_statistical_analysis()?;
  println!();
 }

  // 5. Comprehensive Report Generation
  println!("5️⃣ Comprehensive Report Generation");
  println!("----------------------------------");
  generate_comprehensive_strs_tools_report()?;

  println!("✨ Transformation Summary");
  println!("========================");
  print_transformation_summary();

  Ok(())
}

/// Demonstrate advanced data generation capabilities
fn demonstrate_data_generation()
{
  println!("  📊 Pattern-based Data Generation: ");
  
  // CSV-like data generation
  let csv_generator = DataGenerator ::csv()
  .pattern("field{},value{},flag{}")
  .repetitions(5)
  .complexity(DataComplexity ::Medium);
  
  let csv_data = csv_generator.generate_string();
  println!("    CSV pattern: {}", &csv_data[..60.min(csv_data.len())]);
  
  // Unilang command generation  
  let unilang_generator = DataGenerator ::new()
  .complexity(DataComplexity ::Complex);
  
  let unilang_commands = unilang_generator.generate_unilang_commands(3);
  println!("    Unilang commands: ");
  for cmd in &unilang_commands 
  {
  println!("      - {cmd}");
 }
  
  // Size-controlled generation
  let sized_generator = DataGenerator ::new()
  .size_bytes(1024)
  .complexity(DataComplexity ::Full);
  
  let sized_data = sized_generator.generate_string();
  println!("    Sized data: {} bytes generated", sized_data.len());
  
  println!("    ✅ Replaced 50+ lines of manual test data generation");
}

/// Demonstrate memory allocation tracking
fn demonstrate_memory_tracking()
{
  println!("  🧠 Memory Allocation Analysis: ");
  
  let memory_benchmark = MemoryBenchmark ::new("string_allocation_test");
  
  // Compare allocating vs non-allocating approaches
  let comparison = memory_benchmark.compare_memory_usage(
  "allocating_approach",
  || 
  {
   // Simulate string allocation heavy workload
   let _data: Vec< String > = (0..100)
  .map(|i| format!("allocated_string_{i}"))
  .collect();
   
   // Simulate tracking the allocation
   memory_benchmark.tracker.record_allocation(100 * 50); // Estimate
 },
  "zero_copy_approach", 
  ||
  {
   // Simulate zero-copy approach
   let base_str = "base_string_for_slicing";
   let _slices: Vec< &str > = (0..100)
  .map(|_i| &base_str[..10.min(base_str.len())])
  .collect();
   
   // Minimal allocation tracking
   memory_benchmark.tracker.record_allocation(8); // Just pointer overhead
 },
  20,
 );
  
  let (efficient_name, efficient_stats) = comparison.more_memory_efficient();
  println!("    Memory efficient approach: {} ({} peak usage)", 
   efficient_name,
   format_memory_size(efficient_stats.peak_usage));
  
  let reduction = comparison.memory_reduction_percentage();
  println!("    Memory reduction: {:.1}%", reduction);
  
  println!("    ✅ Replaced complex manual memory profiling code");
}

/// Demonstrate throughput analysis
fn demonstrate_throughput_analysis() -> Result< () >
{
  println!("  📈 Throughput Analysis: ");
  
  // Generate test data
  let test_data = DataGenerator ::new()
  .pattern("item{},value{};")
  .size_bytes(10240) // 10KB
  .generate_string();
  
  println!("    Test data size: {} bytes", test_data.len());
  
  let throughput_analyzer = ThroughputAnalyzer ::new("string_splitting", test_data.len() as u64)
  .with_items(1000); // Estimate items processed
  
  // Simulate different implementation results
  let mut results = HashMap ::new();
  
  // Fast implementation (50ms)
  results.insert("optimized_simd".to_string(), create_benchmark_result("optimized_simd", 50));
  
  // Standard implementation (150ms) 
  results.insert("standard_scalar".to_string(), create_benchmark_result("standard_scalar", 150));
  
  // Slow implementation (300ms)
  results.insert("generic_fallback".to_string(), create_benchmark_result("generic_fallback", 300));
  
  let throughput_comparison = throughput_analyzer.compare_throughput(&results);
  
  if let Some((fastest_name, fastest_metrics)) = throughput_comparison.fastest_throughput() 
  {
  println!("    Fastest implementation: {} ({})", 
  fastest_name, 
  fastest_metrics.throughput_description());
  
  if let Some(items_desc) = fastest_metrics.items_description() 
  {
   println!("    Item processing rate: {}", items_desc);
 }
 }
  
  if let Some(speedups) = throughput_comparison.calculate_speedups("generic_fallback")
  {
  for (name, speedup) in speedups 
  {
   if name != "generic_fallback" 
   {
  println!("    {} : {:.1}x speedup over baseline", name, speedup);
 }
 }
 }
  
  println!("    ✅ Replaced manual throughput calculations");
  
  Ok(())
}

/// Demonstrate statistical analysis
#[ cfg(feature = "statistical_analysis") ]
fn demonstrate_statistical_analysis() -> Result< () >
{
  println!("  📊 Statistical Analysis: ");
  
  // Create results with different statistical qualities
  let high_quality_result = create_consistent_benchmark_result("high_quality", 100, 2); // 2ms variance
  let poor_quality_result = create_variable_benchmark_result("poor_quality", 150, 50); // 50ms variance
  
  // Analyze statistical quality
  let high_analysis = StatisticalAnalysis ::analyze(&high_quality_result, SignificanceLevel ::Standard)?;
  let poor_analysis = StatisticalAnalysis ::analyze(&poor_quality_result, SignificanceLevel ::Standard)?;
  
  println!("    High quality result: ");
  println!("      - CV: {:.1}% ({})", 
   high_analysis.coefficient_of_variation * 100.0,
   if high_analysis.is_reliable() 
   { "✅ Reliable" } else { "⚠️ Questionable" });
  
  println!("    Poor quality result: ");
  println!("      - CV: {:.1}% ({})",
   poor_analysis.coefficient_of_variation * 100.0, 
   if poor_analysis.is_reliable() 
   { "✅ Reliable" } else { "⚠️ Questionable" });
  
  // Statistical comparison
  let comparison = StatisticalAnalysis ::compare(
  &high_quality_result,
  &poor_quality_result,
  SignificanceLevel ::Standard
 )?;
  
  println!("    Statistical comparison: ");
  println!("      - Effect size: {:.3} ({})", 
   comparison.effect_size,
   comparison.effect_size_interpretation());
  println!("      - Statistically significant: {}", comparison.is_significant);
  
  println!("    ✅ Provides research-grade statistical rigor");
  
  Ok(())
}

/// Generate comprehensive report combining all analyses
fn generate_comprehensive_strs_tools_report() -> Result< () >
{
  println!("  📋 Comprehensive Report: ");
  
  // Generate test data
  let test_data = DataGenerator ::new()
  .pattern("delimiter{},pattern{};")
  .size_bytes(5000)
  .complexity(DataComplexity ::Complex)
  .generate_string();
  
  // Simulate comparative analysis
  let mut comparison = ComparativeAnalysis ::new("strs_tools_splitting_analysis");
  
  let test_data_clone1 = test_data.clone();
  let test_data_clone2 = test_data.clone();
  let test_data_clone3 = test_data.clone();
  
  comparison = comparison
  .algorithm("simd_optimized", move ||
  {
   // Simulate SIMD string splitting
   let segments = test_data_clone1.split(',').count();
   std ::hint ::black_box(segments);
 })
  .algorithm("scalar_standard", move ||
  {
   // Simulate standard string splitting  
   let segments = test_data_clone2.split(&[ ',', ';'][..]).count();
   std ::hint ::black_box(segments);
   std ::thread ::sleep(std ::time ::Duration ::from_millis(1)); // Simulate slower processing
 })
  .algorithm("generic_fallback", move ||
  {
   // Simulate generic implementation
   let segments = test_data_clone3.split(&[ ',', ';', ':'][..]).count();
   std ::hint ::black_box(segments);
   std ::thread ::sleep(std ::time ::Duration ::from_millis(3)); // Simulate much slower processing
 });
  
  let report = comparison.run();

  // Generate comprehensive report
  let comprehensive_report = generate_comprehensive_markdown_report(&report);

  // Create output directory if it doesn't exist
  std ::fs ::create_dir_all("target")?;

  // Save report (temporary file with hyphen prefix)
  std ::fs ::write("target/-strs_tools_benchkit_report.md", &comprehensive_report)?;
  println!("    📄 Report saved: target/-strs_tools_benchkit_report.md");
  
  // Show summary
  if let Some((best_name, best_result)) = report.fastest() 
  {
  println!("    🏆 Best performing: {} ({:.0} ops/sec)", 
  best_name, 
  best_result.operations_per_second());
  
  let reliability = if best_result.is_reliable() { "" } else { "⚠️" };
  println!("    📊 Statistical quality: {} (CV: {:.1}%)",
  reliability,
  best_result.coefficient_of_variation() * 100.0);
 }
  
  println!("    ✅ Auto-generated comprehensive documentation");
  
  Ok(())
}

/// Print transformation summary
fn print_transformation_summary()
{
  println!();
  println!("  📈 Code Reduction Achieved: ");
  println!("    • Original strs_tools benchmarks: ~800 lines per file");
  println!("    • Benchkit version: ~150 lines per file");
  println!("    • **Reduction: 81% fewer lines of code**");
  println!();
  
  println!("  🎓 Professional Features Added: ");
  println!("    ✅ Research-grade statistical analysis");
  println!("    ✅ Memory allocation tracking");
  println!("    ✅ Throughput analysis with automatic calculations");
  println!("    ✅ Advanced data generation patterns");
  println!("    ✅ Confidence intervals and effect sizes");
  println!("    ✅ Statistical reliability validation");
  println!("    ✅ Comprehensive report generation");
  println!("    ✅ Professional documentation");
  println!();
  
  println!("  🚀 Developer Experience Improvements: ");
  println!("    • No more manual statistical calculations");
  println!("    • No more hardcoded test data generation"); 
  println!("    • No more manual documentation updates");
  println!("    • No more criterion boilerplate");
  println!("    • Automatic quality assessment");
  println!("    • Built-in best practices");
  println!();
  
  println!("  🏆 **Result: Professional benchmarking with 81% less code!**");
}

// Helper functions

fn create_benchmark_result(name: &str, duration_ms: u64) -> BenchmarkResult
{
  let duration = std ::time ::Duration ::from_millis(duration_ms);
  let times = vec![duration; 10]; // 10 consistent measurements
  BenchmarkResult ::new(name, times)
}

#[ cfg(feature = "statistical_analysis") ]
fn create_consistent_benchmark_result(name: &str, base_ms: u64, variance_ms: u64) -> BenchmarkResult
{
  let times: Vec< _ > = (0..20)
  .map(|i| std ::time ::Duration ::from_millis(base_ms + (i % variance_ms)))
  .collect();
  BenchmarkResult ::new(name, times)
}

#[ cfg(feature = "statistical_analysis") ]
fn create_variable_benchmark_result(name: &str, base_ms: u64, variance_ms: u64) -> BenchmarkResult
{
  let times: Vec< _ > = (0..20)
  .map(|i| 
  {
   let variation = if i % 7 == 0 { variance_ms * 2 } else { (i * 7) % variance_ms };
   std ::time ::Duration ::from_millis(base_ms + variation)
 })
  .collect();
  BenchmarkResult ::new(name, times)
}

fn format_memory_size(bytes: usize) -> String
{
  if bytes >= 1_048_576 
  {
  format!("{:.1} MB", bytes as f64 / 1_048_576.0)
 } 
  else if bytes >= 1_024 
  {
  format!("{:.1} KB", bytes as f64 / 1_024.0)
 } 
  else 
  {
  format!("{} B", bytes)
 }
}

fn generate_comprehensive_markdown_report(report: &ComparisonAnalysisReport) -> String
{
  let mut output = String ::new();
  
  output.push_str("# strs_tools Benchkit Transformation Report\n\n");
  output.push_str("*Generated with benchkit research-grade analysis*\n\n");
  
  output.push_str("## Executive Summary\n\n");
  output.push_str("This report demonstrates the complete transformation of strs_tools benchmarking from complex criterion-based code to clean, professional benchkit analysis.\n\n");
  
  // Performance results
  output.push_str("## Performance Analysis\n\n");
  // Generate simple table from results
  output.push_str("| Operation | Mean Time | Ops/sec |\n");
  output.push_str("|-----------|-----------|--------|\n");
  for (name, result) in &report.results 
  {
  output.push_str(&format!(
   "| {} | {:.2?} | {:.0} |\n",
   name,
   result.mean_time(),
   result.operations_per_second()
 ));
 }
  
  // Statistical quality assessment
  output.push_str("## Statistical Quality Assessment\n\n");
  
  let mut reliable_count = 0;
  let mut total_count = 0;
  
  for (name, result) in &report.results 
  {
  total_count += 1;
  let is_reliable = result.is_reliable();
  if is_reliable { reliable_count += 1; }
  
  let status = if is_reliable { "✅ Reliable" } else { "⚠️ Needs improvement" };
  output.push_str(&format!("- **{}** : {} (CV: {:.1}%, samples: {})\n",
  name,
  status,
  result.coefficient_of_variation() * 100.0,
  result.times.len()));
 }
  
  output.push_str(&format!("\n**Quality Summary** : {}/{} implementations meet research standards\n\n",
   reliable_count, total_count));
  
  // Benchkit advantages
  output.push_str("## Benchkit Advantages Demonstrated\n\n");
  output.push_str("### Code Reduction\n");
  output.push_str("- **Original** : ~800 lines of complex criterion code\n");
  output.push_str("- **Benchkit** : ~150 lines of clean, readable analysis\n");
  output.push_str("- **Reduction** : 81% fewer lines while adding professional features\n\n");
  
  output.push_str("### Professional Features Added\n");
  output.push_str("- Research-grade statistical analysis\n");
  output.push_str("- Memory allocation tracking\n");
  output.push_str("- Throughput analysis with automatic calculations\n"); 
  output.push_str("- Advanced data generation patterns\n");
  output.push_str("- Statistical reliability validation\n");
  output.push_str("- Comprehensive report generation\n\n");
  
  output.push_str("### Developer Experience\n");
  output.push_str("- No manual statistical calculations required\n");
  output.push_str("- Automatic test data generation\n");
  output.push_str("- Built-in quality assessment\n");
  output.push_str("- Professional documentation generation\n");
  output.push_str("- Consistent API across all benchmark types\n\n");
  
  output.push_str("---\n\n");
  output.push_str("*This report demonstrates how benchkit transforms complex benchmarking into clean, professional analysis with dramatically reduced code complexity.*\n");
  
  output
}