不每晚使用时如何忽略基准测试?
How to ignore benchmarks when not using nightly?
我有一个包含一些基准测试和测试的文件,想针对稳定版、测试版和夜间版进行测试。但是,要么我不使用基准测试,要么 stable/beta 抱怨。使用 stable/beta 时有没有办法隐藏所有基准部分?
作为示例,以下代码来自 book:
#![feature(test)]
extern crate test;
pub fn add_two(a: i32) -> i32 {
a + 2
}
#[cfg(test)]
mod tests {
use super::*;
use test::Bencher;
#[test]
fn it_works() {
assert_eq!(4, add_two(2));
}
#[bench]
fn bench_add_two(b: &mut Bencher) {
b.iter(|| add_two(2));
}
}
我正在使用 rustup 并希望同一个文件适用于所有构建,调用如下:
rustup run nightly cargo bench --bin bench --features "bench"
rustup run nightly cargo test --bin bench --features "bench"
rustup run beta cargo test --bin bench
rustup run stable cargo test --bin bench
我可以用 #![cfg_attr(feature = "bench", feature(test))]
隐藏 #![feature(test)]
。我可以做类似于其余基准测试部分的事情吗?什么是功能标志的好资源?
在我的项目中,我将基准测试放在一个单独的模块中,就像我对测试所做的那样。然后,我创建了一个启用它们的 Cargo 功能。在此摘录中,我使用了功能名称 unstable
,但您可以使用任何您喜欢的名称:
Cargo.toml
# ...
[features]
unstable = []
# ...
src/lib.rs
#![cfg_attr(feature = "unstable", feature(test))]
#[cfg(test)]
mod tests {
#[test]
fn a_test() {
assert_eq!(1, 1);
}
}
#[cfg(all(feature = "unstable", test))]
mod bench {
extern crate test;
use self::test::Bencher;
#[bench]
fn a_bench(b: &mut Bencher) {
let z = b.iter(|| {
test::black_box(|| {
1 + 1
})
});
}
}
#[cfg(all(feature = "unstable", test))]
行表示如果设置了该功能则只编译以下项目,并且无论如何我们都在测试模式下编译。同样,#![cfg_attr(feature = "unstable", feature(test))]
仅在启用 unstable
功能时启用 test
功能标志。
这是一个示例 in the wild。
Is there a way to hide all the benchmark parts when using stable/beta?
是的,您可以使用 build script 自动执行此操作,因此在执行 cargo 时无需指定 --features
。在构建脚本中,您可以检测 Rust 编译器的版本并定义一个功能(例如 "nightly"
)。然后,在源代码中,您可以对基准进行分组,如果定义了该功能,则启用它们。
Cargo.toml
[package]
build = "build.rs"
[features]
nightly = []
[build-dependencies]
rustc_version = "0.1.*"
build.rs
extern crate rustc_version;
use rustc_version::{version_meta, Channel};
fn main() {
if version_meta().channel == Channel::Nightly {
println!("cargo:rustc-cfg=feature=\"nightly\"");
}
}
src/lib.rs
#![cfg_attr(all(feature = "nightly", test), feature(test))]
#[cfg(all(feature = "nightly", test))]
extern crate test;
pub fn add_two(a: i32) -> i32 {
a + 2
}
#[cfg(test)]
mod tests {
// tests
}
#[cfg(all(feature = "nightly", test))]
mod benchs {
use test::Bencher;
// benchs
}
Cargo 支持用于基准测试的 benches 目录。如果你把它们放在那里,你就永远不会 运行 "cargo bench" beta/stable,并且只能 运行 每晚。
我有一个包含一些基准测试和测试的文件,想针对稳定版、测试版和夜间版进行测试。但是,要么我不使用基准测试,要么 stable/beta 抱怨。使用 stable/beta 时有没有办法隐藏所有基准部分?
作为示例,以下代码来自 book:
#![feature(test)]
extern crate test;
pub fn add_two(a: i32) -> i32 {
a + 2
}
#[cfg(test)]
mod tests {
use super::*;
use test::Bencher;
#[test]
fn it_works() {
assert_eq!(4, add_two(2));
}
#[bench]
fn bench_add_two(b: &mut Bencher) {
b.iter(|| add_two(2));
}
}
我正在使用 rustup 并希望同一个文件适用于所有构建,调用如下:
rustup run nightly cargo bench --bin bench --features "bench"
rustup run nightly cargo test --bin bench --features "bench"
rustup run beta cargo test --bin bench
rustup run stable cargo test --bin bench
我可以用 #![cfg_attr(feature = "bench", feature(test))]
隐藏 #![feature(test)]
。我可以做类似于其余基准测试部分的事情吗?什么是功能标志的好资源?
在我的项目中,我将基准测试放在一个单独的模块中,就像我对测试所做的那样。然后,我创建了一个启用它们的 Cargo 功能。在此摘录中,我使用了功能名称 unstable
,但您可以使用任何您喜欢的名称:
Cargo.toml
# ...
[features]
unstable = []
# ...
src/lib.rs
#![cfg_attr(feature = "unstable", feature(test))]
#[cfg(test)]
mod tests {
#[test]
fn a_test() {
assert_eq!(1, 1);
}
}
#[cfg(all(feature = "unstable", test))]
mod bench {
extern crate test;
use self::test::Bencher;
#[bench]
fn a_bench(b: &mut Bencher) {
let z = b.iter(|| {
test::black_box(|| {
1 + 1
})
});
}
}
#[cfg(all(feature = "unstable", test))]
行表示如果设置了该功能则只编译以下项目,并且无论如何我们都在测试模式下编译。同样,#![cfg_attr(feature = "unstable", feature(test))]
仅在启用 unstable
功能时启用 test
功能标志。
这是一个示例 in the wild。
Is there a way to hide all the benchmark parts when using stable/beta?
是的,您可以使用 build script 自动执行此操作,因此在执行 cargo 时无需指定 --features
。在构建脚本中,您可以检测 Rust 编译器的版本并定义一个功能(例如 "nightly"
)。然后,在源代码中,您可以对基准进行分组,如果定义了该功能,则启用它们。
Cargo.toml
[package]
build = "build.rs"
[features]
nightly = []
[build-dependencies]
rustc_version = "0.1.*"
build.rs
extern crate rustc_version;
use rustc_version::{version_meta, Channel};
fn main() {
if version_meta().channel == Channel::Nightly {
println!("cargo:rustc-cfg=feature=\"nightly\"");
}
}
src/lib.rs
#![cfg_attr(all(feature = "nightly", test), feature(test))]
#[cfg(all(feature = "nightly", test))]
extern crate test;
pub fn add_two(a: i32) -> i32 {
a + 2
}
#[cfg(test)]
mod tests {
// tests
}
#[cfg(all(feature = "nightly", test))]
mod benchs {
use test::Bencher;
// benchs
}
Cargo 支持用于基准测试的 benches 目录。如果你把它们放在那里,你就永远不会 运行 "cargo bench" beta/stable,并且只能 运行 每晚。