JMH State Classes and Common and Non-Separated States

I am new to jmh and understand what happens behind threads, etc.

So, I started reading and stuck in the @State annotation and shared vs unshared states.

I am reading this example: http://hg.openjdk.java.net/code-tools/jmh/file/ecd9e76155fe/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_03_States.java and u him few questions.

First question: what exactly is the role of state classes? hold parameters? let's say I want to compare a program that encrypts a key in two different ways. Should I store a key (String object) in a state class that is annotated with a specific state? or just save a String object in a reference class? An explanation for this would be wonderful.

The second question: why in the above example, the performance of the fuzzy state class was much better than the general? How does a multithreaded state change it?

I feel really obscure since I am new to this thing and could not find β€œexplain to me how to me 5” examples for jmh and its variants.

+5
source share
1 answer

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.

+2
source

Source: https://habr.com/ru/post/1265164/


All Articles