Skip to content

Commit 4875950

Browse files
committed
✅ Adding tests.
1 parent 8aca43f commit 4875950

2 files changed

Lines changed: 88 additions & 0 deletions

File tree

pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,20 @@
131131
</exclusions>
132132
</dependency>
133133

134+
<dependency>
135+
<groupId>org.openjdk.jmh</groupId>
136+
<artifactId>jmh-core</artifactId>
137+
<version>1.9.3</version>
138+
</dependency>
139+
140+
<dependency>
141+
<groupId>org.openjdk.jmh</groupId>
142+
<artifactId>jmh-generator-annprocess</artifactId>
143+
<version>1.9.3</version>
144+
</dependency>
145+
146+
147+
134148

135149
</dependencies>
136150

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.crossoverjie.basic;
2+
3+
import org.openjdk.jmh.annotations.*;
4+
import org.openjdk.jmh.runner.Runner;
5+
import org.openjdk.jmh.runner.RunnerException;
6+
import org.openjdk.jmh.runner.options.Options;
7+
import org.openjdk.jmh.runner.options.OptionsBuilder;
8+
9+
import java.util.ArrayList;
10+
import java.util.LinkedList;
11+
import java.util.List;
12+
import java.util.concurrent.TimeUnit;
13+
14+
/**
15+
* Function:
16+
*
17+
* @author crossoverJie
18+
* Date: 2019-06-27 00:11
19+
* @since JDK 1.8
20+
*/
21+
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
22+
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
23+
public class CollectionsTest {
24+
25+
private static final int TEN_MILLION = 10000000;
26+
27+
@Benchmark
28+
@BenchmarkMode(Mode.AverageTime)
29+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
30+
public void arrayList() {
31+
32+
List<String> array = new ArrayList<>();
33+
34+
for (int i = 0; i < TEN_MILLION; i++) {
35+
array.add("123");
36+
}
37+
38+
}
39+
40+
@Benchmark
41+
@BenchmarkMode(Mode.AverageTime)
42+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
43+
public void arrayListSize() {
44+
List<String> array = new ArrayList<>(TEN_MILLION);
45+
46+
for (int i = 0; i < TEN_MILLION; i++) {
47+
array.add("123");
48+
}
49+
50+
}
51+
52+
@Benchmark
53+
@BenchmarkMode(Mode.AverageTime)
54+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
55+
public void linkedList() {
56+
List<String> array = new LinkedList<>();
57+
58+
for (int i = 0; i < TEN_MILLION; i++) {
59+
array.add("123");
60+
}
61+
62+
}
63+
64+
65+
public static void main(String[] args) throws RunnerException {
66+
Options opt = new OptionsBuilder()
67+
.include(CollectionsTest.class.getSimpleName())
68+
.forks(1)
69+
.build();
70+
71+
72+
new Runner(opt).run();
73+
}
74+
}

0 commit comments

Comments
 (0)