PropertyBencher

Trait PropertyBencher 

Source
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§

Source

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),

Benchmark a function over inputs generated by a property-based generator

§Arguments
  • name - Name of the benchmark
  • bench_fn - Function to benchmark (takes immutable reference to input)
  • generator - Generator for creating diverse test inputs
  • sample_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);
Source

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,

Benchmark a property test (function that takes ownership and may assert)

§Arguments
  • name - Name of the benchmark
  • generator - Generator for creating diverse test inputs
  • property - Property function to benchmark
  • sample_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.

Implementations on Foreign Types§

Source§

impl PropertyBencher for Criterion

Source§

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),

Source§

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,

Implementors§