2

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?

Brandon Piña
  • 614
  • 1
  • 6
  • 20
  • This isn't the full code, can you please share the complete code that can be compiled [mcve] ... For example, did you use `criterion_main!` we don't know :-) so it's difficult to recreate the problem and test? – Ahmed Masud Oct 13 '22 at 23:39
  • There is no criterion main because I didn't get to it. I got stopped by my import issue – Brandon Piña Oct 14 '22 at 00:00

1 Answers1

2

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 [...]

Igor Artamonov
  • 35,450
  • 10
  • 82
  • 113