Use criterion to benchmark a function in an app

Can Criterion be used to benchmark a function in a bin (ie not a lib) ?

I don't find how to make the function visible in the benches/ and I find no example inline not involving a lib (in fact it's hard to find an example not being the Fibonacci one...)

I found this old answer on SO which basically says there's no way without creating a lib:

But I find it hard to imagine this problem hasn't been fixed and isn't documented in Criterion documentation. So I guess there's an easy obvious way to benchmark the code of applications. maintainer here. No, there is no way to benchmark binary crates with or any other benchmark harness - the built-in harness is still a bit magical like that. It's fairly common for applications to be a thin glue code wrapper around a library crate that implements all of the application logic, which neatly solves this problem.

Though now I think of it, I don't think I've tried it with the #[criterion] macro, it's possible that might work.

1 Like

@bheisler I think there should be a page dedicated to app internals benchmarking in the documentation. Such a page would at the very least explain that it's difficult and give some advice or guidelines, for example about how to extract part of the app into a sub-crate lib (this is what I'm going to do in order to bench my functions).