rust 无法将我的crate导入criterion benchmark

py49o6xq  于 2023-05-07  发布在  其他
关注(0)|答案(1)|浏览(238)

我尝试使用criterion crate来对我的二进制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(||))
}

这是我的货物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

根据我的理解,我应该使用%mycratename%::foo::bar而不是crate::foo::bar导入我的函数,但我得到一个错误,说我的crate找不到。我如何让rust识别我本地未发布的crate来进行criterion基准测试?

gwbalxhn

gwbalxhn1#

这是Criterion Benchmark的一个已知限制。它不适用于 * 二进制文件 *,并且只能对 * 库 * 进行测试。
请参阅:https://bheisler.github.io/criterion.rs/book/user_guide/known_limitations.html
[...]在二进制箱中对函数进行基准测试是不可能的。二进制crate不能依赖于其他crate,这包括外部测试和基准测试[...]

相关问题