benchkit 0.21.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
//! Coefficient of Variation (CV) Improvement Patterns
//!
//! This example demonstrates proven techniques for reducing CV and improving
//! benchmark reliability based on real-world success in production systems.
//!
//! Key improvements demonstrated :
//! - Thread pool stabilization (CV reduction: 60-80%)
//! - CPU frequency stabilization (CV reduction: 40-60%) 
//! - Cache and memory warmup (CV reduction: 70-90%)
//! - Systematic CV analysis workflow
//!
//! Run with: cargo run --example `cv_improvement_patterns` --features `enabled,markdown_reports`

#[ cfg( feature = "enabled" ) ]
use core ::time ::Duration;
use std ::time ::Instant;
#[ cfg( feature = "enabled" ) ]
use std ::thread;
#[ cfg( feature = "enabled" ) ]
use std ::collections ::HashMap;

#[ cfg( feature = "enabled" ) ]
fn main()
{

  println!( "๐Ÿ”ฌ CV Improvement Patterns Demonstration" );
  println!( "========================================" );
  println!();

  // Demonstrate CV problems and solutions
  demonstrate_parallel_cv_improvement();
  demonstrate_cpu_cv_improvement();
  demonstrate_memory_cv_improvement();
  demonstrate_systematic_cv_analysis();
  demonstrate_environment_specific_cv();

  println!( "โœ… All CV improvement patterns demonstrated successfully!" );
  println!( "๐Ÿ“Š Check the generated reports for detailed CV analysis." );
}

#[ cfg( feature = "enabled" ) ]
fn demonstrate_parallel_cv_improvement()
{
  println!( "๐Ÿงต Parallel Processing CV Improvement" );
  println!( "=====================================" );
  println!();

  // Simulate a thread pool operation
  let data = generate_parallel_test_data( 1000 );

  println!( "โŒ BEFORE: Unstable parallel benchmark (high CV expected)" );
  
  // Simulate unstable parallel benchmark
  let unstable_times = measure_unstable_parallel( &data );
  let unstable_cv = calculate_cv( &unstable_times );
  
  println!( "   Average: {:.2}ms", mean( &unstable_times ) );
  println!( "   CV: {:.1}% - {}", unstable_cv * 100.0, reliability_status( unstable_cv ) );
  println!();

  println!( "โœ… AFTER: Stabilized parallel benchmark with warmup" );
  
  // Stabilized parallel benchmark  
  let stable_times = measure_stable_parallel( &data );
  let stable_cv = calculate_cv( &stable_times );
  
  println!( "   Average: {:.2}ms", mean( &stable_times ) );
  println!( "   CV: {:.1}% - {}", stable_cv * 100.0, reliability_status( stable_cv ) );
  
  let improvement = ( ( unstable_cv - stable_cv ) / unstable_cv ) * 100.0;
  println!( "   Improvement: {improvement:.1}% CV reduction" );
  println!();

  // Generate documentation
  generate_parallel_cv_report( &unstable_times, &stable_times );
}

#[ cfg( feature = "enabled" ) ]
fn demonstrate_cpu_cv_improvement()
{
  println!( "๐Ÿ–ฅ๏ธ  CPU Frequency CV Improvement" );
  println!( "===============================" );
  println!();

  let data = generate_cpu_test_data( 500 );

  println!( "โŒ BEFORE: CPU frequency scaling causes inconsistent timing" );
  
  let unstable_times = measure_unstable_cpu( &data );
  let unstable_cv = calculate_cv( &unstable_times );
  
  println!( "   Average: {:.2}ms", mean( &unstable_times ) );
  println!( "   CV: {:.1}% - {}", unstable_cv * 100.0, reliability_status( unstable_cv ) );
  println!();

  println!( "โœ… AFTER: CPU frequency stabilization with delays" );
  
  let stable_times = measure_stable_cpu( &data );
  let stable_cv = calculate_cv( &stable_times );
  
  println!( "   Average: {:.2}ms", mean( &stable_times ) );
  println!( "   CV: {:.1}% - {}", stable_cv * 100.0, reliability_status( stable_cv ) );
  
  let improvement = ( ( unstable_cv - stable_cv ) / unstable_cv ) * 100.0;
  println!( "   Improvement: {improvement:.1}% CV reduction" );
  println!();

  generate_cpu_cv_report( &unstable_times, &stable_times );
}

#[ cfg( feature = "enabled" ) ]
fn demonstrate_memory_cv_improvement()
{
  println!( "๐Ÿง  Memory and Cache CV Improvement" );
  println!( "==================================" );
  println!();

  let data = generate_memory_test_data( 2000 );

  println!( "โŒ BEFORE: Cold cache and initialization overhead" );
  
  let cold_times = measure_cold_memory( &data );
  let cold_cv = calculate_cv( &cold_times );
  
  println!( "   Average: {:.2}ms", mean( &cold_times ) );
  println!( "   CV: {:.1}% - {}", cold_cv * 100.0, reliability_status( cold_cv ) );
  println!();

  println!( "โœ… AFTER: Cache warmup and memory preloading" );
  
  let warm_times = measure_warm_memory( &data );
  let warm_cv = calculate_cv( &warm_times );
  
  println!( "   Average: {:.2}ms", mean( &warm_times ) );
  println!( "   CV: {:.1}% - {}", warm_cv * 100.0, reliability_status( warm_cv ) );
  
  let improvement = ( ( cold_cv - warm_cv ) / cold_cv ) * 100.0;
  println!( "   Improvement: {improvement:.1}% CV reduction" );
  println!();

  generate_memory_cv_report( &cold_times, &warm_times );
}

#[ cfg( feature = "enabled" ) ]
fn demonstrate_systematic_cv_analysis()
{
  println!( "๐Ÿ“Š Systematic CV Analysis Workflow" );
  println!( "==================================" );
  println!();

  // Simulate multiple benchmarks with different CV characteristics
  let benchmark_results = vec!
  [
  ( "excellent_benchmark", 0.03 ),    // 3% CV - excellent
  ( "good_benchmark", 0.08 ),         // 8% CV - good
  ( "moderate_benchmark", 0.12 ),     // 12% CV - moderate
  ( "poor_benchmark", 0.22 ),         // 22% CV - poor
  ( "unreliable_benchmark", 0.45 ),   // 45% CV - unreliable
 ];

  println!( "๐Ÿ” Analyzing benchmark suite reliability: " );
  println!();

  for ( name, cv ) in &benchmark_results
  {
  let cv_percent = cv * 100.0;
  let status = reliability_status( *cv );
  let icon = match cv_percent
  {
   cv if cv > 25.0 => "โŒ",
   cv if cv > 10.0 => "โš ๏ธ",
   _ => "โœ…",
 };
  
  println!( "{icon} {name} : CV {cv_percent:.1}% - {status}" );
  
  if cv_percent > 10.0
  {
   print_cv_improvement_suggestions( name, *cv );
 }
 }

  println!();
  println!( "๐Ÿ“ˆ CV Improvement Recommendations: " );
  demonstrate_systematic_improvement_workflow();
}

#[ cfg( feature = "enabled" ) ]
fn demonstrate_environment_specific_cv()
{
  println!( "๐ŸŒ Environment-Specific CV Targets" );
  println!( "==================================" );
  println!();

  let environments = vec!
  [
  ( "Development", 0.15, 15, "Quick feedback cycles" ),
  ( "CI/CD", 0.10, 25, "Reliable regression detection" ),
  ( "Production", 0.05, 50, "Decision-grade reliability" ),
 ];

  println!( "Environment-specific CV targets and sample requirements: " );
  println!();

  for ( env_name, cv_target, sample_count, purpose ) in &environments
  {
  println!( "๐Ÿ”ง {env_name} Environment: " );
  println!( "   Target CV: < {:.0}%", cv_target * 100.0 );
  println!( "   Sample Count: {sample_count} samples" );
  println!( "   Purpose: {purpose}" );
  
  // Simulate benchmark configuration
  let config = create_environment_config( env_name, *cv_target, *sample_count );
  println!( "   Configuration: {config}" );
  println!();
 }

  generate_environment_cv_report( &environments );
}

#[ cfg( feature = "enabled" ) ]
fn demonstrate_systematic_improvement_workflow()
{
  println!( "๐Ÿ”ง Systematic CV Improvement Process: " );
  println!();

  let _ = "sample_benchmark"; // Demonstration only
  let mut current_cv = 0.35; // Start with high CV (35%)

  println!( "๐Ÿ“Š Baseline CV: {:.1}%", current_cv * 100.0 );
  println!();

  let improvements = vec!
  [
  ( "Add warmup runs", 0.60 ),           // 60% improvement
  ( "Stabilize thread pool", 0.40 ),     // 40% improvement  
  ( "Add CPU frequency delay", 0.25 ),   // 25% improvement
  ( "Increase sample count", 0.30 ),     // 30% improvement
 ];

  for ( description, improvement_factor ) in improvements
  {
  println!( "๐Ÿ”จ Applying: {description}" );
  
  let previous_cv = current_cv;
  current_cv *= 1.0 - improvement_factor;
  
  let improvement_percent = ( ( previous_cv - current_cv ) / previous_cv ) * 100.0;
  
  println!( "   โœ… CV improved by {:.1}% (now {:.1}%)", 
  improvement_percent, current_cv * 100.0 );
  println!( "   Status: {}", reliability_status( current_cv ) );
  println!();
 }

  println!( "๐ŸŽฏ Final Result: CV reduced from 35.0% to {:.1}%", current_cv * 100.0 );
  println!( "   Overall improvement: {:.1}%", ( ( 0.35 - current_cv ) / 0.35 ) * 100.0 );
}

// Helper functions for benchmark simulation and analysis

#[ cfg( feature = "enabled" ) ]
fn generate_parallel_test_data( size: usize ) -> Vec< i32 >
{
  ( 0..size ).map( | i | i32 ::try_from( i ).unwrap_or( 0 ) ).collect()
}

#[ cfg( feature = "enabled" ) ]
fn generate_cpu_test_data( size: usize ) -> Vec< f64 >
{
  ( 0..size ).map( | i | i as f64 * 1.5 ).collect()
}

#[ cfg( feature = "enabled" ) ]
fn generate_memory_test_data( size: usize ) -> Vec< String >
{
  ( 0..size ).map( | i | format!( "data_item_{i}" ) ).collect()
}

#[ cfg( feature = "enabled" ) ]
fn measure_unstable_parallel( data: &[ i32 ] ) -> Vec< f64 >
{
  let mut times = Vec ::new();
  
  for _ in 0..20
  {
  let start = Instant ::now();
  
  // Simulate unstable parallel processing (no warmup)
  let _result = simulate_parallel_processing( data );
  
  let duration = start.elapsed();
  times.push( duration.as_secs_f64() * 1000.0 ); // Convert to ms
 }
  
  times
}

#[ cfg( feature = "enabled" ) ]
fn measure_stable_parallel( data: &[ i32 ] ) -> Vec< f64 >
{
  let mut times = Vec ::new();
  
  for _ in 0..20
  {
  // Warmup run to stabilize thread pool
  let _ = simulate_parallel_processing( data );
  
  // Small delay to let threads stabilize
  thread ::sleep( Duration ::from_millis( 2 ) );
  
  let start = Instant ::now();
  
  // Actual measurement run
  let _result = simulate_parallel_processing( data );
  
  let duration = start.elapsed();
  times.push( duration.as_secs_f64() * 1000.0 );
 }
  
  times
}

#[ cfg( feature = "enabled" ) ]
fn measure_unstable_cpu( data: &[ f64 ] ) -> Vec< f64 >
{
  let mut times = Vec ::new();
  
  for _ in 0..20
  {
  let start = Instant ::now();
  
  // Simulate CPU-intensive operation without frequency stabilization
  let _result = simulate_cpu_intensive( data );
  
  let duration = start.elapsed();
  times.push( duration.as_secs_f64() * 1000.0 );
 }
  
  times
}

#[ cfg( feature = "enabled" ) ]
fn measure_stable_cpu( data: &[ f64 ] ) -> Vec< f64 >
{
  let mut times = Vec ::new();
  
  for _ in 0..20
  {
  // Force CPU to stable frequency with delay
  thread ::sleep( Duration ::from_millis( 1 ) );
  
  let start = Instant ::now();
  
  // Actual measurement with stabilized CPU
  let _result = simulate_cpu_intensive( data );
  
  let duration = start.elapsed();
  times.push( duration.as_secs_f64() * 1000.0 );
 }
  
  times
}

#[ cfg( feature = "enabled" ) ]
fn measure_cold_memory( data: &[ String ] ) -> Vec< f64 >
{
  let mut times = Vec ::new();
  
  for _ in 0..20
  {
  let start = Instant ::now();
  
  // Simulate memory operation with cold cache
  let _result = simulate_memory_operation( data );
  
  let duration = start.elapsed();
  times.push( duration.as_secs_f64() * 1000.0 );
  
  // Clear caches between measurements to simulate cold effects
  thread ::sleep( Duration ::from_millis( 5 ) );
 }
  
  times
}

#[ cfg( feature = "enabled" ) ]
fn measure_warm_memory( data: &[ String ] ) -> Vec< f64 >
{
  let mut times = Vec ::new();
  
  for _ in 0..20
  {
  // Multiple warmup cycles to eliminate cold effects
  for _ in 0..3
  {
   let _ = simulate_memory_operation( data );
 }
  thread ::sleep( Duration ::from_micros( 10 ) );
  
  let start = Instant ::now();
  
  // Actual measurement with warmed cache
  let _result = simulate_memory_operation( data );
  
  let duration = start.elapsed();
  times.push( duration.as_secs_f64() * 1000.0 );
 }
  
  times
}

#[ cfg( feature = "enabled" ) ]
fn simulate_parallel_processing( data: &[ i32 ] ) -> i64
{
  // Simulate parallel work with some randomness
  use std ::sync :: { Arc, Mutex };
  
  let counter = Arc ::new( Mutex ::new( 0 ) );
  let mut handles = vec![];
  
  for chunk in data.chunks( 100 )
  {
  let counter_clone = Arc ::clone( &counter );
  let chunk_sum: i32 = chunk.iter().sum();
  
  let handle = thread ::spawn( move ||
  {
   // Simulate work
   let work_result = chunk_sum * 2;
   
   // Add to shared counter
   let mut num = counter_clone.lock().unwrap();
   *num += i64 ::from( work_result );
 });
  
  handles.push( handle );
 }
  
  for handle in handles
  {
  handle.join().unwrap();
 }
  
  let result = *counter.lock().unwrap();
  result
}

#[ cfg( feature = "enabled" ) ]
fn simulate_cpu_intensive( data: &[ f64 ] ) -> f64
{
  // Simulate CPU-intensive computation
  let mut result = 0.0;
  
  for &value in data
  {
  result += value.sin().cos().tan().sqrt();
 }
  
  result
}

#[ cfg( feature = "enabled" ) ]
fn simulate_memory_operation( data: &[ String ] ) -> HashMap< String, usize >
{
  // Simulate memory-intensive operation
  let mut map = HashMap ::new();
  
  for ( index, item ) in data.iter().enumerate()
  {
  map.insert( item.clone(), index );
 }
  
  map
}

#[ cfg( feature = "enabled" ) ]
fn calculate_cv( times: &[ f64 ] ) -> f64
{
  let mean_time = mean( times );
  let variance = times.iter()
  .map( | time | ( time - mean_time ).powi( 2 ) )
  .sum :: < f64 >() / ( times.len() as f64 - 1.0 );
  
  let std_dev = variance.sqrt();
  std_dev / mean_time
}

#[ cfg( feature = "enabled" ) ]
fn mean( values: &[ f64 ] ) -> f64
{
  values.iter().sum :: < f64 >() / values.len() as f64
}

#[ cfg( feature = "enabled" ) ]
fn reliability_status( cv: f64 ) -> &'static str
{
  match cv
  {
  cv if cv < 0.05 => "โœ… Excellent reliability",
  cv if cv < 0.10 => "โœ… Good reliability",
  cv if cv < 0.15 => "โš ๏ธ Moderate reliability",
  cv if cv < 0.25 => "โš ๏ธ Poor reliability",
  _ => "โŒ Unreliable",
 }
}

#[ cfg( feature = "enabled" ) ]
fn print_cv_improvement_suggestions( benchmark_name: &str, cv: f64 )
{
  println!( "   ๐Ÿ’ก Improvement suggestions for {benchmark_name} : " );
  
  if cv > 0.25
  {
  println!( "      โ€ข Add extensive warmup runs (3-5 iterations)" );
  println!( "      โ€ข Increase sample count to 50+ measurements" );
  println!( "      โ€ข Check for external interference (other processes)" );
 }
  else if cv > 0.15
  {
  println!( "      โ€ข Add moderate warmup (1-2 iterations)" );
  println!( "      โ€ข Increase sample count to 30+ measurements" );
  println!( "      โ€ข Add CPU frequency stabilization delays" );
 }
  else
  {
  println!( "      โ€ข Minor warmup improvements" );
  println!( "      โ€ข Consider increasing sample count to 25+" );
 }
}

#[ cfg( feature = "enabled" ) ]
fn create_environment_config( env_name: &str, cv_target: f64, sample_count: i32 ) -> String
{
  format!( "BenchmarkSuite ::new(\"{}\").with_cv_tolerance({:.2}).with_sample_count({})", 
   env_name.to_lowercase(), cv_target, sample_count )
}

#[ cfg( feature = "enabled" ) ]
fn generate_parallel_cv_report( unstable_times: &[ f64 ], stable_times: &[ f64 ] )
{
  println!( "๐Ÿ“„ Generating parallel processing CV improvement report..." );
  
  let unstable_cv = calculate_cv( unstable_times );
  let stable_cv = calculate_cv( stable_times );
  let improvement = ( ( unstable_cv - stable_cv ) / unstable_cv ) * 100.0;
  
  println!( "   Report: Parallel CV improved by {:.1}% (from {:.1}% to {:.1}%)", 
   improvement, unstable_cv * 100.0, stable_cv * 100.0 );
}

#[ cfg( feature = "enabled" ) ]
fn generate_cpu_cv_report( unstable_times: &[ f64 ], stable_times: &[ f64 ] )
{
  println!( "๐Ÿ“„ Generating CPU frequency CV improvement report..." );
  
  let unstable_cv = calculate_cv( unstable_times );
  let stable_cv = calculate_cv( stable_times );
  let improvement = ( ( unstable_cv - stable_cv ) / unstable_cv ) * 100.0;
  
  println!( "   Report: CPU CV improved by {:.1}% (from {:.1}% to {:.1}%)", 
   improvement, unstable_cv * 100.0, stable_cv * 100.0 );
}

#[ cfg( feature = "enabled" ) ]
fn generate_memory_cv_report( cold_times: &[ f64 ], warm_times: &[ f64 ] )
{
  println!( "๐Ÿ“„ Generating memory/cache CV improvement report..." );
  
  let cold_cv = calculate_cv( cold_times );
  let warm_cv = calculate_cv( warm_times );
  let improvement = ( ( cold_cv - warm_cv ) / cold_cv ) * 100.0;
  
  println!( "   Report: Memory CV improved by {:.1}% (from {:.1}% to {:.1}%)", 
   improvement, cold_cv * 100.0, warm_cv * 100.0 );
}

#[ cfg( feature = "enabled" ) ]
fn generate_environment_cv_report( environments: &[ ( &str, f64, i32, &str ) ] )
{
  println!( "๐Ÿ“„ Generating environment-specific CV targets report..." );
  
  for ( env_name, cv_target, sample_count, _purpose ) in environments
  {
  println!( "   {} : Target CV < {:.0}%, {} samples", 
  env_name, cv_target * 100.0, sample_count );
 }
}

#[ cfg( not( feature = "enabled" ) ) ]
fn main()
{
  println!( "This example requires the 'enabled' feature to be activated." );
  println!( "Please run: cargo run --example cv_improvement_patterns --features enabled,markdown_reports" );
}