Cargo test vs cargo bench --tests


#1

I can’t get cargo bench --tests passing while cargo test passes just fine.

cargo bench --tests
thread ‘main’ has overflowed its stack
fatal runtime error: stack overflow
error: An unknown error occurred

The code is basically as follows here:

    #[bench]
    pub fn my_benchmark(b: &mut Bencher) {
      // setup test ...
      // ... 

      b.iter(|| {
        tested_function();
      });
      let result = tested_function();
      assert_eq!(result == true);
    }
  }

I’m following the recommendations from https://doc.rust-lang.org/1.12.1/book/benchmark-tests.html.

I’d appreciate some pointer explaining why cargo bench and cargo test can behave differently here.


#2

The main difference is that cargo bench compiles with optimizations. I think it will be identical if you run cargo test --release. But I don’t know why this would cause a stack overflow.


#3

Figured this out: cargo bench executes multiple times not just the benchmarked closure but the whole method marked as #[bench]. It wasn’t obvious to me.

The setup code in the method had a bug when being called multiple times. That’s why things crash.