You can consider @State objects as part of your test, which you need to run, without its creation time being considered part of your measured time.
Let's say you want to measure the time it takes to calculate:
@Benchmark int benchmark() { int foo = 1, bar = 1; return foo + bar; }
Unfortunately for you, the JIT compiler is too smart to allow you to do this, and will reset the method to just return 2 . This, of course, is not what you want to measure. By using state, you can avoid these values ββand let JMH take care not to reset the JIT values. You must initialize the values ββin the @Setup method.
As another use case, you can verify that your test did what you expected. This is possible by checking the status using the @TearDown method.
source share