Attribute Macro test_generator::bench_resources
source · #[bench_resources]
Expand description
Macro generating bench-functions, invoking the fn for each item matching the resource-pattern.
The resource-pattern must not expand to empty list, otherwise an error is raised. The generated test-functions is a regular bench, being compiled by the rust-compiler; and being executed in sequentially by the bench-framework.
ⓘ
#![feature(test)] // nightly feature required for API test::Bencher
#[cfg(test)]
extern crate test; /* required for test::Bencher */
#[cfg(test)]
extern crate test_generator;
#[cfg(test)]
mod tests {
use test_generator::bench_resources;
#[bench_resources("res/*/input.txt")]
fn measure_resource(b: &mut test::Bencher, resource: &str) {
let path = std::path::Path::new(resource);
b.iter(|| path.exists());
}
}
Assuming the following package layout with the bench file mybenches.rs
and resource folder res/
,
the output below will be printed on console. The functionality of build.rs
is explained at crate
build-deps and demonstrated with
example
ⓘ
├── build.rs
├── Cargo.toml
├── res
│ ├── set1
│ │ ├── expect.txt
│ │ └── input.txt
│ ├── set2
│ │ ├── expect.txt
│ │ └── input.txt
│ └── set3
│ ├── expect.txt
│ └── input.txt
├── src
│ └── main.rs
├── benches
│ └── mybenches.rs
└── tests
└── mytests.rs
Output from cargo +nightly bench
for 3 bench-input-files matching the pattern, for this example:
ⓘ
running 3 tests
test bench::measure_resource_res_set1_input_txt ... bench: 2,492 ns/iter (+/- 4,027)
test bench::measure_resource_res_set2_input_txt ... bench: 2,345 ns/iter (+/- 2,167)
test bench::measure_resource_res_set3_input_txt ... bench: 2,269 ns/iter (+/- 1,527)
test result: ok. 0 passed; 0 failed; 0 ignored; 3 measured; 0 filtered out