Skip to content

Commit b49844c

Browse files
ericgoebelbeckerericgoebelbecker
authored andcommitted
BAEL-1374 - use JMH. Move out of test area.
1 parent 89ab473 commit b49844c

2 files changed

Lines changed: 74 additions & 39 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.array;
2+
3+
import org.openjdk.jmh.runner.Runner;
4+
import org.openjdk.jmh.runner.options.Options;
5+
import org.openjdk.jmh.runner.options.OptionsBuilder;
6+
7+
public class ArrayBenchmarkRunner {
8+
9+
public static void main(String[] args) throws Exception {
10+
11+
Options options = new OptionsBuilder()
12+
.include(SearchArrayTest.class.getSimpleName()).threads(1)
13+
.forks(1).shouldFailOnError(true).shouldDoGC(true)
14+
.jvmArgs("-server").build();
15+
16+
new Runner(options).run();
17+
18+
19+
}
20+
21+
}

core-java/src/test/java/com/baeldung/array/SearchArrayTest.java renamed to core-java/src/main/java/com/baeldung/array/SearchArrayTest.java

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,92 @@
11
package com.baeldung.array;
22

3-
import org.junit.Test;
3+
import org.openjdk.jmh.annotations.*;
44

55
import java.util.*;
6+
import java.util.concurrent.TimeUnit;
67

78
public class SearchArrayTest {
89

9-
10-
@Test
11-
public void searchArrayAllocNewCollections() {
10+
@Benchmark
11+
@BenchmarkMode(Mode.AverageTime)
12+
@Warmup(iterations = 5)
13+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
14+
public void searchArrayLoop() {
1215

1316
int count = 1000;
14-
1517
String[] strings = seedArray(count);
16-
17-
long startTime = System.nanoTime();
1818
for (int i = 0; i < count; i++) {
19-
searchList(strings, "W");
19+
searchLoop(strings, "T");
2020
}
21-
long duration = System.nanoTime() - startTime;
22-
System.out.println("SearchList: " + duration / 10000);
21+
}
22+
23+
@Benchmark
24+
@BenchmarkMode(Mode.AverageTime)
25+
@Warmup(iterations = 5)
26+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
27+
public void searchArrayAllocNewList() {
2328

24-
startTime = System.nanoTime();
29+
int count = 1000;
30+
String[] strings = seedArray(count);
2531
for (int i = 0; i < count; i++) {
26-
searchSet(strings,"S");
32+
searchList(strings, "W");
2733
}
28-
duration = System.nanoTime() - startTime;
29-
System.out.println("SearchSet: " + duration / 10000);
3034

31-
startTime = System.nanoTime();
35+
}
36+
37+
@Benchmark
38+
@BenchmarkMode(Mode.AverageTime)
39+
@Warmup(iterations = 5)
40+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
41+
public void searchArrayAllocNewSet() {
42+
43+
int count = 1000;
44+
String[] strings = seedArray(count);
3245
for (int i = 0; i < count; i++) {
33-
searchLoop(strings, "T");
46+
searchSet(strings, "S");
3447
}
35-
duration = System.nanoTime() - startTime;
36-
System.out.println("SearchLoop: " + duration / 10000);
3748
}
3849

39-
@Test
40-
public void searchArrayReuseCollections() {
4150

42-
int count = 10000;
51+
@Benchmark
52+
@BenchmarkMode(Mode.AverageTime)
53+
@Warmup(iterations = 5)
54+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
55+
public void searchArrayReuseList() {
56+
57+
int count = 1000;
4358
String[] strings = seedArray(count);
4459

4560
List<String> asList = Arrays.asList(strings);
46-
Set<String> asSet = new HashSet<>(Arrays.asList(strings));
4761

48-
long startTime = System.nanoTime();
4962
for (int i = 0; i < count; i++) {
5063
asList.contains("W");
5164
}
52-
long duration = System.nanoTime() - startTime;
53-
System.out.println("List: " + duration / 10000);
65+
}
5466

55-
startTime = System.nanoTime();
56-
for (int i = 0; i < count; i++) {
57-
asSet.contains("S");
58-
}
59-
duration = System.nanoTime() - startTime;
60-
System.out.println("Set: " + duration / 10000);
6167

62-
startTime = System.nanoTime();
68+
@Benchmark
69+
@BenchmarkMode(Mode.AverageTime)
70+
@Warmup(iterations = 5)
71+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
72+
public void searchArrayReuseSet() {
73+
74+
int count = 1000;
75+
String[] strings = seedArray(count);
76+
Set<String> asSet = new HashSet<>(Arrays.asList(strings));
6377
for (int i = 0; i < count; i++) {
64-
searchLoop(strings, "T");
78+
asSet.contains("S");
6579
}
66-
duration = System.nanoTime() - startTime;
67-
System.out.println("Loop: " + duration / 10000);
68-
6980
}
7081

7182

72-
@Test
83+
@Benchmark
84+
@BenchmarkMode(Mode.AverageTime)
85+
@Warmup(iterations = 5)
86+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
7387
public void searchArrayBinarySearch() {
7488

75-
int count = 10000;
89+
int count = 1000;
7690
String[] strings = seedArray(count);
7791
Arrays.sort(strings);
7892

@@ -81,7 +95,7 @@ public void searchArrayBinarySearch() {
8195
Arrays.binarySearch(strings, "A");
8296
}
8397
long duration = System.nanoTime() - startTime;
84-
System.out.println("Binary search: " + duration / 10000);
98+
//System.out.println("Binary search: " + duration / 10000);
8599

86100
}
87101

0 commit comments

Comments
 (0)