Ignore benchmarks when using stable / beta

I have a file with some criteria and tests, and I would like to test it on stable, beta and nightly versions. However, either I do not use the benchmark, but a stable / beta complaint. Is there any way to hide all reference parts when using stable / beta?

The following code from book is an example:

#![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)); } } 

I use rustup and would like the same file to work with all assemblies, calling something like:

 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 

I managed to hide #![feature(test)] with #![cfg_attr(feature = "bench", feature(test))] . Can I do something similar to the rest of the reference parts? What is a good resource for function flags?

+7
benchmarking rust
source share
3 answers

In my projects, I install tests in a separate module, as for tests. Then I create a Cargo function that allows them. In this passage, I used the function name unstable , but you can use whatever you want:

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 }) }); } } 

The line #[cfg(all(feature = "unstable", test))] says only to compile the next element if the function is installed and we still compile in test mode. Similarly, #![cfg_attr(feature = "unstable", feature(test))] only includes the flag of the test function when the unstable function is enabled.

Here is an example in the wild .

+11
source share

Cargo maintains a bench catalog for test cases. If you place them there, you can simply never run the “load bench” in beta / stable operation and only run it at night.

+7
source share

Is there any way to hide all reference parts when using stable / beta?

Yes, and you can do this automatically using the build script , so there is no need to specify --features when executing loads. In the build script, you can detect the Rust compiler version and define a function (for example, "nightly" ). Then in the source code you can group your tests and enable them if a function has been defined.

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 } 
+5
source share

All Articles