Migrating from libtest

This page shows an example of converting a libtest or bencher benchmark to use Criterion.rs.

The Benchmark

We'll start with this benchmark as an example:


# #![allow(unused_variables)]
#![feature(test)]
#fn main() {
extern crate test;
use test::Bencher;
use test::black_box;

fn fibonacci(n: u64) -> u64 {
    match n {
        0 => 1,
        1 => 1,
        n => fibonacci(n-1) + fibonacci(n-2),
    }
}

#[bench]
fn bench_fib(b: &mut Bencher) {
    b.iter(|| fibonacci(black_box(20)));
}
#}

The Migration

The first thing to do is update the Cargo.toml to disable the libtest benchmark harness:

[[bench]]
name = "example"
harness = false

We also need to add Criterion.rs to the dev-dependencies section of Cargo.toml:

[dev-dependencies]
criterion = "0.2"

The next step is to update the imports:


# #![allow(unused_variables)]
#fn main() {
#[macro_use]
extern crate criterion;
use criterion::Criterion;
use criterion::black_box;
#}

Then, we can change the bench_fib function. Remove the #[bench] and change the argument to &mut Criterion instead. The contents of this function need to change as well:


# #![allow(unused_variables)]
#fn main() {
fn bench_fib(c: &mut Criterion) {
    c.bench_function("fib 20", |b| b.iter(|| fibonacci(black_box(20))));
}
#}

Finally, we need to invoke some macros to generate a main function, since we no longer have libtest to provide one:


# #![allow(unused_variables)]
#fn main() {
criterion_group!(benches, bench_fib);
criterion_main!(benches);
#}

And that's it! The complete migrated benchmark code is below:


# #![allow(unused_variables)]
#fn main() {
#[macro_use]
extern crate criterion;
use criterion::Criterion;
use criterion::black_box;

fn fibonacci(n: u64) -> u64 {
    match n {
        0 => 1,
        1 => 1,
        n => fibonacci(n-1) + fibonacci(n-2),
    }
}

fn bench_fib(c: &mut Criterion) {
    c.bench_function("fib 20", |b| b.iter(|| fibonacci(black_box(20))));
}

criterion_group!(benches, bench_fib);
criterion_main!(benches);
#}