| Crates.io | dir-bench |
| lib.rs | dir-bench |
| version | 0.1.0 |
| created_at | 2024-12-22 17:28:19.000608+00 |
| updated_at | 2024-12-22 17:28:19.000608+00 |
| description | dir-bench provides a macro to generating benchmarks from files in a directory |
| homepage | |
| repository | https://github.com/lzldev/dir-bench |
| max_upload_size | |
| id | 1492151 |
| size | 6,397 |
dir-bench provides a macro to generate benchmarks from files in a directory.
NOTE: currently rust only supports running benchmarks in the nigthly channel.
crate based on dir-test
Add the following dependency to your Cargo.toml.
[dev-dependencies]
dir-bench = "0.1.0"
extern crate test;
use test::Bencher;
use dir_bench::{dir_bench, Fixture};
#[dir_bench(
dir: "$CARGO_MANIFEST_DIR/fixtures",
glob: "**/*.txt",
)]
fn benchmark(b: &mut Bencher,fixture: Fixture<&str>) {
// The file content and the absolute path of the file are available as follows.
let content = fixture.content();
let path = fixture.path();
// Setup your benchmark
// ...
b.iter(|| {
// Write your benchmark code here
// ...
})
}
Assuming your crate is as follows, then the above code generates two benchmarks
cases mybenchmark__foo() and mybenchmark__fixtures_a_bar().
my-crate/
├─ fixtures/
│ ├─ foo.txt
│ ├─ fixtures_a/
│ │ ├─ bar.txt
├─ src/
│ ├─ ...
│ ├─ lib.rs
├─ Cargo.toml
├─ README.md
🔽
#[bench]
fn mybenchmark__foo(b: &mut Bencher) {
//...
mybenchmark(b,fixture);
}
#[bench]
fn mybenchmark__fixtures_a_bar(b: &mut Bencher) {
//...
mybenchmark(b,fixture);
}
NOTE: The dir argument must be specified in an absolute path because
of the limitation of the current procedural macro system. Consider using
environment variables, dir-bench crate resolves environment variables
internally.
Benchmark attributes can specified by the dir_bench_attr attribute. The
attributes inside dir_bench_attr are applied to the all generated benchmarks.
use dir_bench::{dir_bench, Fixture};
#[dir_test(
dir: "$CARGO_MANIFEST_DIR/fixtures",
glob: "**/*.txt",
)]
#[dir_bench_atrr(
#[wasm_bindgen_test]
#[cfg(target_family = "wasm")]
)]
fn wasm_test(fixture: Fixture<std::io::Result<String>>) {
// ...
}