首页 > 解决方案 > Rust 标准基准测试模块`未解决的导入`crate::some_module`

问题描述

我有一个包含许多内部模块来组织功能的二进制项目。

我正在尝试让Criterion为基准工作。

错误:

error[E0432]: unresolved import `crate::some_module`
 --> benches/benchmarks.rs:3:5
  |
3 | use crate::some_module;
  |     ^^^^^^^^^^^^^^^^^^ no `some_module` in the root

error: aborting due to previous error

最小示例.ziphttps ://drive.google.com/file/d/1TASKI9_ODJniamHp3RBRscoflabPT-kP/view?usp=sharing

我当前的最小示例如下所示:

.
├── Cargo.lock
├── Cargo.toml
├── src/
│   ├── main.rs
│   └── some_module/
│       ├── mod.rs
│       └── some_module_file.rs
└── benches/
    └── benchmarks.rs

Cargo.toml

[package]
name = "criterion-bin-test"
version = "0.1.0"
authors = ["Jonathan <jonthanwoollettlight@gmail.com>"]
edition = "2018"

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

[dependencies]

[dev-dependencies]
criterion = "0.3"

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

main.rs

mod some_module;

fn main() {
    println!("Hello, world!");

    println!("{}",some_module::some_mod_file::two());
}

mod.rs

pub mod some_mod_file;

some_module_file

pub fn two() -> i32 {
    2
}

benchmarks.rs

use criterion::{black_box, criterion_group, criterion_main, Criterion};

use crate::some_module;

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

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

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

错误出现在 上use crate::some_module,我无法弄清楚如何将 Criterion 基准测试应用到内部模块。非常感谢这里的任何帮助。

我当前项目中的测试按以下方式处理:

.
├── Cargo.lock
├── Cargo.toml
├── src/
|   ├── main.rs
.   ├── tests.rs
    . some_module/
        ├── mod.rs
        ├── tests.rs
        .

这允许use crate::some_module.

标签: rustrust-criterion

解决方案


推荐阅读