Hello all. I'm trying to prototype a benchmarking library where benchmarks are collected using the inventory crate, but it's not working as I expect.
What I have now looks like this: I have a workspace with the main library benchpress
and an example consumer of the library benchpress-example
. benchpress
defines a struct Benchmark
that's collected with inventory::collect!()
, and benchpress-example
adds a benchmark using inventory::submit!()
. Then, the added benchmarks are iterated in benchpress-example/benches/bench.rs
.
exa -T
.
├── benchpress
│ ├── Cargo.toml
│ └── src
│ └── lib.rs
├── benchpress-example
│ ├── benches
│ │ └── bench.rs
│ ├── Cargo.toml
│ └── src
│ └── lib.rs
├── Cargo.lock
├── Cargo.toml
Cargo.toml
:
[workspace]
members = [
"benchpress",
"benchpress-example",
]
benchpress/Cargo.toml
[package]
name = "benchpress"
version = "0.1.0"
authors = ["Billy Rieger <wrieger@protonmail.com>"]
edition = "2018"
[dependencies]
inventory = "0.1"
benchpress-example/Cargo.toml
[package]
name = "benchpress-example"
version = "0.1.0"
authors = ["Billy Rieger <wrieger@protonmail.com>"]
edition = "2018"
[dependencies]
benchpress = { path = "../benchpress" }
inventory = "0.1"
[[bench]]
name = "bench"
harness = false
benchpress/src/lib.rs
#[derive(Debug)]
pub struct Benchmark {
name: &'static str,
}
inventory::collect!(Benchmark);
impl Benchmark {
pub fn new(name: &'static str) -> Self {
Self { name }
}
}
pub fn bench() {
for benchmark in inventory::iter::<Benchmark> {
println!("{:?}", benchmark);
}
}
inventory::submit!(Benchmark::new("hello from benchpress crate"));
benchpress-example/src/lib.rs
inventory::submit!(benchpress::Benchmark::new(
"hello from benchpress-example crate"
));
benchpress-example/benches/bench.rs
fn main() {
benchpress::bench();
}
When I run cargo bench -- --nocapture
, I would expect to see both "hello from benchpress crate"
and "hello from benchpress-example crate"
. However, the actual output only shows the former:
$ cargo bench -- --nocapture
Finished release [optimized] target(s) in 0.04s
...
Running target/release/deps/bench-138439ced4f75337
Benchmark { name: "hello from benchpress crate" }
Is this a limitation of the inventory
crate or am I doing something incorrectly?
Edit: fixed a typo.