【发布时间】:2021-05-15 03:38:45
【问题描述】:
我开始在 Rust 中使用“bench”和 Rust nightly 编写基准测试,如文档中所述。
为了在测试和基准测试之间共享代码,我添加了 Option<&mut Bencher> 并直接运行代码块或通过 bencher ("src/lib.rs") 运行代码块:
fn block_requests(bencher_option: Option<&mut Bencher>, ...) {
...
let mut block = || {
... // shared
}
match bencher_option {
// regular test
None => block(),
// benchmark
Some(bencher) => {
bencher.iter(block);
}
}
...
}
// call from test
#[test]
fn test_smth() {
block_requests(None, &requests, &mut matcher);
}
// call from benchmark
#[bench]
fn bench_smth(b: &mut Bencher) {
block_requests(Some(b), &requests, &mut matcher);
}
现在我想使用 Rust stable 进行基准测试。 由于"bencher" crate 3 年未更新,因此"criterion" crate 似乎是默认选项。为此,我必须将代码移动到“./benches/my_benchmark.rs”。
我怎样才能在测试和基准测试之间共享block_requests(..)?
【问题讨论】:
-
use mycrate::block_requests
标签: testing rust benchmarking source-sets