11package com .baeldung .performance ;
22
33import org .openjdk .jmh .annotations .*;
4+ import org .openjdk .jmh .runner .Runner ;
5+ import org .openjdk .jmh .runner .options .Options ;
6+ import org .openjdk .jmh .runner .options .OptionsBuilder ;
47
58import java .util .ArrayList ;
69import java .util .HashSet ;
@@ -15,14 +18,14 @@ public static class MyState {
1518 private Set <Employee > employeeSet = new HashSet <>();
1619 private List <Employee > employeeList = new ArrayList <>();
1720
18- private final long m_count = 16 ;
21+ private long iterations = 10000 ;
1922
2023 private Employee employee = new Employee (100L , "Harry" );
2124
22- @ Setup (Level .Invocation )
25+ @ Setup (Level .Trial )
2326 public void setUp () {
2427
25- for (long ii = 0 ; ii < m_count ; ii ++) {
28+ for (long ii = 0 ; ii < iterations ; ii ++) {
2629 employeeSet .add (new Employee (ii , "John" ));
2730 employeeList .add (new Employee (ii , "John" ));
2831
@@ -35,19 +38,26 @@ public void setUp() {
3538
3639 @ Benchmark
3740 @ BenchmarkMode (Mode .AverageTime )
38- @ OutputTimeUnit (TimeUnit .MINUTES )
41+ @ OutputTimeUnit (TimeUnit .NANOSECONDS )
42+ @ Warmup (iterations = 1 )
3943 public boolean testArrayList (MyState state ) {
4044 return state .employeeList .contains (state .employee );
4145 }
4246
4347 @ Benchmark
4448 @ BenchmarkMode (Mode .AverageTime )
45- @ OutputTimeUnit (TimeUnit .MINUTES )
49+ @ OutputTimeUnit (TimeUnit .NANOSECONDS )
50+ @ Warmup (iterations = 1 )
4651 public boolean testHashSet (MyState state ) {
4752 return state .employeeSet .contains (state .employee );
4853 }
4954
5055 public static void main (String [] args ) throws Exception {
51- org .openjdk .jmh .Main .main (args );
56+ Options options = new OptionsBuilder ()
57+ .include (CollectionsBenchmark .class .getSimpleName ()).threads (1 )
58+ .forks (1 ).shouldFailOnError (true )
59+ .shouldDoGC (true )
60+ .jvmArgs ("-server" ).build ();
61+ new Runner (options ).run ();
5262 }
5363}
0 commit comments