pub trait PropertyBencher {
// Required methods
fn bench_function_over_inputs<I, G, F>(
&mut self,
name: &str,
bench_fn: F,
generator: G,
sample_count: usize,
)
where I: Clone + 'static,
G: Generator<I>,
F: FnMut(&mut Bencher<'_>, &I);
fn bench_property<I, G, P>(
&mut self,
name: &str,
generator: G,
property: P,
sample_count: usize,
)
where I: Clone + 'static,
G: Generator<I>,
P: Fn(&I) + 'static;
}Expand description
Extension trait for Criterion to enable property-based benchmarking
Required Methods§
Sourcefn bench_function_over_inputs<I, G, F>(
&mut self,
name: &str,
bench_fn: F,
generator: G,
sample_count: usize,
)
fn bench_function_over_inputs<I, G, F>( &mut self, name: &str, bench_fn: F, generator: G, sample_count: usize, )
Benchmark a function over inputs generated by a property-based generator
§Arguments
name- Name of the benchmarkbench_fn- Function to benchmark (takes immutable reference to input)generator- Generator for creating diverse test inputssample_count- Number of samples to generate and benchmark
§Example
use criterion::{criterion_group, criterion_main, Criterion};
use protest_criterion::PropertyBencher;
use protest::primitives::IntGenerator;
fn bench_abs(c: &mut Criterion) {
c.bench_function_over_inputs(
"i32::abs",
|b, &input: &i32| b.iter(|| input.abs()),
IntGenerator::new(-1000, 1000),
100,
);
}
criterion_group!(benches, bench_abs);
criterion_main!(benches);Sourcefn bench_property<I, G, P>(
&mut self,
name: &str,
generator: G,
property: P,
sample_count: usize,
)
fn bench_property<I, G, P>( &mut self, name: &str, generator: G, property: P, sample_count: usize, )
Benchmark a property test (function that takes ownership and may assert)
§Arguments
name- Name of the benchmarkgenerator- Generator for creating diverse test inputsproperty- Property function to benchmarksample_count- Number of samples to generate and benchmark
§Example
use criterion::{criterion_group, criterion_main, Criterion};
use protest_criterion::PropertyBencher;
use protest::primitives::VecGenerator;
use protest::Generator;
fn bench_reverse_involutive(c: &mut Criterion) {
c.bench_property(
"vec reverse is involutive",
VecGenerator::new(
protest::primitives::IntGenerator::new(0, 100),
0,
100
),
|v: &Vec<i32>| {
let mut reversed = v.clone();
reversed.reverse();
reversed.reverse();
},
50,
);
}
criterion_group!(benches, bench_reverse_involutive);
criterion_main!(benches);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.