protest-criterion
Property-Based Benchmarking with Criterion
Integration between Protest property-based testing and Criterion benchmarking framework.
Overview
Property-based benchmarking allows you to:
- 📊 Benchmark with diverse inputs - Use generators to create realistic test data
- 📈 Understand performance distribution - See how your code performs across the input space
- 🔍 Detect performance regressions - Leverage Criterion's statistical analysis
- ⚡ Find performance edge cases - Discover worst-case scenarios automatically
- 🎯 Reproducible benchmarks - Seed-based generation for consistent results
Quick Start
Add to your Cargo.toml:
[]
= "0.1"
= "0.1"
= "0.5"
Create benches/my_benchmark.rs:
use ;
use PropertyBencher;
use IntGenerator;
criterion_group!;
criterion_main!;
Run with:
Features
1. Benchmark Functions with Generated Inputs
Use bench_function_over_inputs to benchmark a function with diverse inputs:
use ;
use PropertyBencher;
use IntGenerator;
criterion_group!;
criterion_main!;
2. Benchmark Property Tests
Use bench_property to benchmark property checks:
use ;
use PropertyBencher;
use VecGenerator;
use Generator;
criterion_group!;
criterion_main!;
3. Benchmark by Input Size
Compare performance across different input sizes:
use ;
use PropertyBenchmarkGroup;
use ;
use Generator;
criterion_group!;
criterion_main!;
Use Cases
Sorting Algorithms
Benchmark sorting with various input distributions:
use ;
use PropertyBencher;
use ;
use Generator;
criterion_group!;
criterion_main!;
String Operations
use ;
use PropertyBencher;
use StringGenerator;
criterion_group!;
criterion_main!;
Hash Map Operations
use ;
use PropertyBencher;
use ;
use HashMap;
criterion_group!;
criterion_main!;
API Reference
PropertyBencher Trait
Extension trait for Criterion providing property-based benchmarking methods.
bench_function_over_inputs
Benchmarks a function with inputs generated by a property-based generator.
Parameters:
name- Name of the benchmarkbench_fn- Function to benchmark (receives bencher and input reference)generator- Generator for creating test inputssample_count- Number of different inputs to generate and benchmark
bench_property
Benchmarks a property test function.
Parameters:
name- Name of the benchmarkgenerator- Generator for creating test inputsproperty- Property function to benchmark (can include assertions)sample_count- Number of different inputs to generate and benchmark
PropertyBenchmarkGroup Trait
Extension trait for BenchmarkGroup for more ergonomic grouped benchmarks.
bench_generated
Benchmarks with generated inputs within a benchmark group.
Best Practices
1. Choose Appropriate Sample Counts
// For fast operations, use more samples
c.bench_function_over_inputs;
// For slow operations, fewer samples
c.bench_function_over_inputs;
2. Use Realistic Input Distributions
// Good: Reflects real-world data
new
// Bad: Unrealistic range
new
3. Benchmark Different Scenarios
let mut group = c.benchmark_group;
// Best case
group.bench_generated;
// Worst case
group.bench_generated;
// Average case
group.bench_generated;
group.finish;
4. Use Seeds for Reproducibility
use ;
// For reproducible benchmarks
let mut rng = seed_from_u64;
Integration with Protest
protest-criterion works seamlessly with all Protest generators:
- Primitives:
IntGenerator,StringGenerator,BoolGenerator, etc. - Collections:
VecGenerator,HashMapGenerator,HashSetGenerator - Composite:
TupleGenerator,OptionGenerator - Custom: Any type implementing
Generator<T>
See protest documentation for available generators.
Examples
See the benches/ directory for complete examples:
example_benchmarks.rs- Basic usage
Performance Tips
- Warm up: Criterion automatically warms up, but consider longer warm-up for complex operations
- Input caching: Pre-generate inputs if generation is expensive
- Batch sizes: Use
criterion::BatchSizeappropriately - Sample size: Balance statistical significance with benchmark duration
Comparison with Traditional Benchmarking
Traditional Criterion
Limitations:
- Single input case
- No coverage of edge cases
- Manual input creation
- No distribution analysis
With protest-criterion
Benefits:
- Tests across input space
- Automatic edge case discovery
- Statistical distribution of performance
- Realistic input generation
Contributing
Contributions are welcome! Please see the main Protest repository for contribution guidelines.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Related Crates
- protest - Property-based testing framework
- criterion - Statistics-driven micro-benchmarking
- protest-extras - Additional generators
- protest-stateful - Stateful property testing
Acknowledgments
Built on top of:
- Criterion.rs - Excellent benchmarking framework
- Protest - Property-based testing for Rust