1

I want to bench a function with criterion crate, this function is also a test function. It seems not to be possible to call the function during benchmarking when the #[test] attribute is present.

#[test]
fn verify_encryption() {
    // ...
}

A function that runs an integration test (check that a set of functions behave as expected) is also a good target for benchmark. Is there a workaround ?

Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
Ostn
  • 803
  • 1
  • 9
  • 27
  • 1
    A slightly verbose solution: `fn foo() { /* do the foo */} #[test] fn foo_test() { foo() } #[bench] fn foo_bench() { foo() }`. – mcarton Sep 02 '20 at 10:37
  • *"A function that runs an integration test is also a good target for benchmark."* - is it? The benchmark would only show how fast the integration test is; prepping, asserting, mocking, and all. I question what you're hoping to gain from it. – kmdreko Sep 02 '20 at 10:39
  • @kmdreko For a simple lib, integration test in is an end to end test. In my case I am interested in the speed of different cryptographic functions as well as their appropriate behavior. – Ostn Sep 02 '20 at 11:07
  • @mcarton nice but then the `foo` function is part of the lib compilation – Ostn Sep 02 '20 at 11:14
  • It's hard to answer your question because it doesn't include a [MRE]. We can't tell what crates (and their versions), types, traits, fields, etc. are present in the code. It would make it easier for us to help you if you try to reproduce your error on the [Rust Playground](https://play.rust-lang.org) if possible, otherwise in a brand new Cargo project, then [edit] your question to include the additional info. There are [Rust-specific MRE tips](//stackoverflow.com/tags/rust/info) you can use to reduce your original code for posting here. Thanks! – Shepmaster Sep 02 '20 at 13:28

0 Answers0