Search code examples
rustrust-criterion

Can't import my crate into criterion benchmark


I'm trying to use the criterion crate to benchmark a function in my binary crate.

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use rand::Rng;
use enigma::enigma::Enigma; // failed to resolve: use of undeclared crate or module `enigma` use of undeclared crate or module `enigma`rustcE0433

fn gen_rand_string(n: usize) -> String {
    const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    let mut rng = rand::thread_rng();
    (0..n)
        .map(|_| {
            let idx = rng.gen_range(0..CHARSET.len());
            CHARSET[idx] as char
        })
        .collect()
}

// Lots of red squigglies because of imports
fn construct_enigma() -> Enigma {
    let rotors: RotorConfig =
        RotorConfig::try_from([(Rotors::I, 'A'), (Rotors::II, 'X'), (Rotors::IV, 'N')])
            .unwrap();
    let plugs = Plugs::try_from(vec![]).unwrap();
    let plugboard: Plugboard = Plugboard::try_from(plugs).unwrap();
    let reflector: Reflectors = Reflectors::B;

    Enigma::new(rotors, plugboard, reflector)
}

fn criterion_benchmark(c:&mut Criterion){
    let e = construct_enigma();
    let s1000 = gen_rand_string(1000);
    c.bench_function("ENC 1000", |b|b.iter(||))
}

Here's my cargo.toml

[package]
name = "enigma"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.65"
bimap = "0.6.2"
bruh_moment = "0.1.1"
itertools = "0.10.3"
log = "0.4.17"
rand = "0.8.5"
strum = "0.24.1"
strum_macros = "0.24.3"
thiserror = "1.0.37"

[dev-dependencies]
criterion = "0.4.0"

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

by my understanding i should be importing my function using %mycratename%::foo::bar instead of crate::foo::bar but i'm getting an error saying that my crate can't be found. How do I get rust to recognize my local unpublished crate for criterion benchmarks?


Solution

  • That's a Known Limitation of the Criterion Benchmark. It doesn't work with binaries, and can bench only libraries.

    See: https://bheisler.github.io/criterion.rs/book/user_guide/known_limitations.html:

    [...] It is not possible to benchmark functions in binary crates. Binary crates cannot be dependencies of other crates, and that includes external tests and benchmarks [...]