I'm trying to create the benchmark that modifies the collection. The problem is that I don't know how to initialize data for each invocation.
Assume that Test.DATA is a collection that contains 200 elements.
The test
method removes data based on a.isTrue()
's value.
I know that @Setup
is similar to JUnit's @Setup
. I want to use @Before
but I couldn't find one in JMH.
How can I initialize the data each time before the test
method is invoked?
Thank you very 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();
}
}