Details
Description
@BeforeClass methods only run once; @Before methods will run on every round. There should be another level for methods that run before each @Test benchmark.
I have created a @BeforeBenchmark annotation that does just this. Attached is a patch that includes code to modify BenchmarkStatement.evaluate() so that the @BeforeBenchmark methods are run at the correct place. This patch also forces GC after calling the benchmark befores and at the end of the evaluate() since I think that was something that was missing. Perhaps this should be another BenchmarkOption to call GC between tests? I can open a separate defect if desired.
Note that similar code could be added for an @AfterBenchmark annotation. I can submit a patch for that if needed too. Not sure if that's as useful. I didn't need it in my usecase but maybe others do.
Thanks for your contribution, but I'd rather not put it in. Here is my motivation:
Your code is fine, don't get me wrong. It is a biased opinion not to commit it in. If you desperately need a before/ after benchmark code then your patch would be fine and it can be applied separately. Feel free to fork the code on github if you wish.
- I think a single benchmarks pass should be as simple as possible, without any additional surrounding code. This code can interfere with the benchmark by allocating memory or even by influencing the jit so that it takes different optimization paths.
- The GC is called before every round (in BaseEvaluator#evaluateInternally) so that measurements can be taken after a round is complete. It doesn't make sense to invoke it after or from other places that are not part of the measurement. GC measurement is not that reliable either – it could be improved by observing major GC counters via JMX, but I don't think it's worth the effort, honestly.
- I just wouldn't want too many features to creep in, especially in the light of potential refactorings.
Your code is fine, don't get me wrong. It is a biased opinion not to commit it in. If you desperately need a before/ after benchmark code then your patch would be fine and it can be applied separately. Feel free to fork the code on github if you wish.