Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Benches in src for lib #2

Closed
radevgit opened this issue Oct 5, 2023 · 2 comments
Closed

Benches in src for lib #2

radevgit opened this issue Oct 5, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@radevgit
Copy link

radevgit commented Oct 5, 2023

All the given code examples assume the benches are created in /examples/...
Is it possible to have benches in /src/something.rs for a library project?
I tried to use #[divan::bench] the same way I use the #[test], in every module and used the
[dependencies]
divan = "0.1.0"
in Cargo.toml, however the "cargo bench" does not produce any results (only run tests).
Maybe we need some examples how to do it in that case.

@nvzqz nvzqz added the enhancement New feature or request label Oct 30, 2023
@nvzqz
Copy link
Owner

nvzqz commented Oct 30, 2023

Divan is able to benchmark internals of a library. As of 558176a, Divan now benchmarks some of its own internals in CI:

divan/src/time/timer.rs

Lines 176 to 196 in 558176a

#[cfg(feature = "internal_benches")]
#[crate::bench(crate = crate)]
fn get_tsc() -> Result<Timer, TscUnavailable> {
Timer::get_tsc()
}
#[cfg(feature = "internal_benches")]
mod measure {
use super::*;
#[crate::bench(crate = crate)]
fn precision() -> FineDuration {
Timer::Os.measure_precision()
}
#[cfg(feature = "internal_benches")]
#[crate::bench(crate = crate)]
fn sample_loop_overhead() -> FineDuration {
Timer::Os.measure_sample_loop_overhead()
}
}

Note

Your code should not do crate = crate since this only applies to Divan.

Feel free to use 558176a as a guide for benchmarking your own library's internals. That said, this solution is rather clunky and I wish we had something better.

@nvzqz nvzqz closed this as completed Oct 30, 2023
@nvzqz
Copy link
Owner

nvzqz commented Oct 30, 2023

cc @realFlowControl @AliciaBytes @CyriacBr @HadrienG2 @IndigoLily

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants