Here's a literal answer, but don't actually use this!
#![feature(test)]
extern crate test;
#[path = "../src/foo.rs"] // Here
mod foo;
#[bench]
fn bencher(_: &mut test::Bencher) {
println!("{:?}", foo::Thang);
}
In fact, it's very likely that this won't work because your code in foo.rs
needs supporting code from other files that won't be included.
Instead of doing this, just create a library. You have the pure definition of a library - a piece of code that wants to be used in two different executables. You don't have to give up having an executable or even create separate directories (see Rust package with both a library and a binary?), but creating reusable code is a key component of making good code.
Your end state would look something like:
demo
├── Cargo.lock
├── Cargo.toml
├── benches
│?? └── crypto_bench.rs
├── benchmarks
└── src
├── bin
│?? └── main.rs
├── crypto.rs
└── lib.rs
Move the reusable code to a library:
src/lib.rs
pub mod crypto;
src/crypto.rs
pub struct Crypto;
impl Crypto {
pub fn secret() {}
}
Then import your library from the benchmark and the binary:
benches/crypto_bench.rs
#![feature(test)]
extern crate test;
use demo::crypto::Crypto;
use test::Bencher;
#[bench]
fn speedy(b: &mut Bencher) {
b.iter(|| Crypto::secret());
}
src/bin/main.rs
use demo::crypto::Crypto;
fn main() {
Crypto::secret();
eprintln!("Did the secret thing!");
}
You can then run it in different ways:
$ cargo build
Compiling demo v0.1.0 (/private/tmp/example)
Finished dev [unoptimized + debuginfo] target(s) in 0.51s
$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/main`
Did the secret thing!
$ cargo +nightly bench
Compiling demo v0.1.0 (/private/tmp/example)
Finished release [optimized] target(s) in 0.70s
Running target/release/deps/my_benchmark-5c9c5716763252a0
running 1 test
test speedy ... bench: 1 ns/iter (+/- 0)
test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured; 0 filtered out
See also:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…