How to initialize data for each call in JMH?

I am trying to create a test that modifies a collection. The problem is that I do not know how to initialize the data for each call.

Suppose Test.DATA is a collection containing 200 items.
The test method deletes data based on the value of a.isTrue() .
I know that @Setup is similar to JUnit @Setup . I want to use @Before but I could not find it in JMH. How can I initialize data every time before calling the test method?
Thank you so much in advance.

 @State(Scope.Thread) public class JavaCollectionBenchmark { List<Foo> cols; @Setup public void prepare(){ cols= new ArrayList<>(Test.DATA); } @Benchmark public long test(){ if(cols.size() != 200) { System.out.println("SECOND TIME DOESN'T WORK!"); System.exit(0); }else{ System.out.println("FIRST TIME"); } cols.removeIf(a-> a.isTrue()); return cols.size(); } } 
+5
source share
1 answer

Check the Level parameter in the @Setup annotation. @Before equivalent is

 @Setup(Level.Invocation) 

which is explained along with many warnings ( WARNING: HERE BE DRAGONS! THIS IS A SHARP TOOL. etc.) here

+2
source

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


All Articles