【问题标题】:Rust Criterion benchmarking modules `unresolved import `crate::some_module``Rust 标准基准测试模块`未解决的导入`crate::some_module`
【发布时间】:2021-06-28 05:48:52
【问题描述】:

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

我正在尝试让 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

最小的例子.zip:https://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

【问题讨论】:

  • 与放在 /tests 目录中的测试一样,放在 /benches 目录中的基准测试只能访问您的 crate 的 public API;由于除了二进制文件之外,您的 crate 不提供库目标,因此它目前没有任何公共 API。要么将 some_module 移动到库目标(链接到二进制目标和基准),要么将这些基准移动到 /src 文件夹中,以便它们可以使用二进制的 private API(就像您当前在 /src 文件夹中的测试一样)。
  • 好的,所以将benchmarks.rs 移动到src/ 会产生错误can't find 'benchmarks' bench, specify bench.path,我认为这与Cargo.toml[[bench]] \n name = "benchmarks" \n harness = false 的设置有关,不知道在此之后如何进行?跨度>
  • 好吧,如果您采用将基准移动到二进制文件中的方式,那么您将不再需要构建单独的基准目标(因此应该从您的Cargo.toml 中删除该目标)。我对 Criterion 不够熟悉,无法说出它是否支持这样的嵌入式基准测试——可能不支持。我想你最好走另一条路,将some_module 移动到库目标中,这样你就可以按照 Criterion 文档坚持使用独立的基准测试目标。
  • 如何将some_module 移动到库目标中?
  • 最简单的方法是将main.rs 移动到/src/bin,将mod some_module; 更改为use criterion_bin_test::some_module;,然后创建包含pub mod some_module;/src/lib.rs

标签: rust rust-criterion


【解决方案1】:

对于任何在这个问题上苦苦挣扎的人。

在实现基准测试(或示例)时,如果我们想要包含我们的一些模块/功能,我们必须按照与包含已安装包中的那些相同的方式进行操作。

例如,在上面的问题中,有一个错误表明:

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

那是因为它应该是这样的:

use criterion-bin-test::some_module;

其中 criterion-bin-test 是实际的包名称,在实施示例或基准测试时使用它而不是 crate::

【讨论】:

    猜你喜欢
    • 2017-01-23
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    • 2020-04-14
    • 1970-01-01
    相关资源
    最近更新 更多