How to benchmark a private function on all versions of the compiler

I am trying to assess one of my libraries' (emcee) performance. There is a low level function (propose_stretch) which is called quite a few times, and performs some non-trivial code. I wish to benchmark this function, but it is not marked pub - I do not forsee users of the library to call this function for themselves.

The problem is I'd like to keep my library running on stable rust, and the benchmarking system is nightly only. I current have a separate benches subdirectory at the root of my project, which benchmarks a higher level, but public function. This works because normal compilation (on stable/beta) will ignore the benches subdir during installation/testing for users of the crate.

To be able to benchmark the function in question, I have to move the benchmark functionality into the same file/module as the function - where currently I already have tests, but this will cause compilation to fail as I have to add:

#![feature(test)]
extern crate test;

to the root of my crate, which will not compile on stable/beta.

I think I have these options:

  1. make the function public, which I would prefer not to do
  2. somehow add the lines above with a nightly feature flag - and I do not know if one exists
  3. give up on benchmarking this function, as the caller function is benchmarked and the benchmark code is deterministic, however I would prefer to benchmark at a lower level

Apologies in advance if this is a problem with a known solution

1 Like

You could expose the function only in test builds:

#[cfg(test)]
pub fn public_wrapper() {
   private_function();
}

This often doesn't work, because, for example, in integration tests, your library is not compiled with cfg(test) on, since it's using your library like an external person would.

I forget if benchmark tests do this or not.

One way to do it is to manually add a benchmark feature (disabled by default) to the crate and enclose the benchmarking of the private function in that. When you want to bench the function you run with: cargo +nightly bench --features benchmarks