Skip to content

Commit ba8d640

Browse files
rodrigolgracianomaibin
authored andcommitted
BAEL-3414 (eugenp#8177)
* BAEL-3414 * BAEL-3414 -> Add unit tests * BAEL-3414 -> Clean up pom.xml
1 parent d5d1cde commit ba8d640

6 files changed

Lines changed: 263 additions & 1 deletion

File tree

core-java-modules/core-java-11/pom.xml

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,27 @@
2929
<version>${assertj.version}</version>
3030
<scope>test</scope>
3131
</dependency>
32+
<dependency>
33+
<groupId>org.openjdk.jmh</groupId>
34+
<artifactId>jmh-core</artifactId>
35+
<version>${jmh.version}</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.openjdk.jmh</groupId>
39+
<artifactId>jmh-generator-annprocess</artifactId>
40+
<version>${jmh.version}</version>
41+
<scope>provided</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.eclipse.collections</groupId>
45+
<artifactId>eclipse-collections</artifactId>
46+
<version>10.0.0</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.eclipse.collections</groupId>
50+
<artifactId>eclipse-collections-api</artifactId>
51+
<version>10.0.0</version>
52+
</dependency>
3253
</dependencies>
3354

3455
<build>
@@ -42,6 +63,42 @@
4263
<target>${maven.compiler.target.version}</target>
4364
</configuration>
4465
</plugin>
66+
<plugin>
67+
<groupId>org.apache.maven.plugins</groupId>
68+
<artifactId>maven-shade-plugin</artifactId>
69+
<version>3.2.1</version>
70+
<executions>
71+
<execution>
72+
<phase>package</phase>
73+
<goals>
74+
<goal>shade</goal>
75+
</goals>
76+
<configuration>
77+
<finalName>${uberjar.name}</finalName>
78+
<transformers>
79+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
80+
<mainClass>org.openjdk.jmh.Main</mainClass>
81+
</transformer>
82+
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
83+
</transformers>
84+
<filters>
85+
<filter>
86+
<!--
87+
Shading signed JARs will fail without this.
88+
http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar
89+
-->
90+
<artifact>*:*</artifact>
91+
<excludes>
92+
<exclude>META-INF/*.SF</exclude>
93+
<exclude>META-INF/*.DSA</exclude>
94+
<exclude>META-INF/*.RSA</exclude>
95+
</excludes>
96+
</filter>
97+
</filters>
98+
</configuration>
99+
</execution>
100+
</executions>
101+
</plugin>
45102
</plugins>
46103
</build>
47104

@@ -50,6 +107,7 @@
50107
<maven.compiler.target.version>11</maven.compiler.target.version>
51108
<guava.version>27.1-jre</guava.version>
52109
<assertj.version>3.11.1</assertj.version>
110+
<uberjar.name>benchmarks</uberjar.name>
111+
<jmh.version>1.22</jmh.version>
53112
</properties>
54-
55113
</project>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.baeldung.benchmark;
2+
3+
import org.eclipse.collections.api.list.MutableList;
4+
import org.eclipse.collections.api.list.primitive.IntList;
5+
import org.eclipse.collections.api.list.primitive.MutableIntList;
6+
import org.eclipse.collections.impl.factory.primitive.IntLists;
7+
import org.eclipse.collections.impl.list.mutable.FastList;
8+
import org.eclipse.collections.impl.list.mutable.primitive.IntArrayList;
9+
import org.openjdk.jmh.annotations.*;
10+
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
import java.util.PrimitiveIterator;
14+
import java.util.Random;
15+
import java.util.concurrent.ExecutorService;
16+
import java.util.concurrent.Executors;
17+
import java.util.concurrent.TimeUnit;
18+
import java.util.stream.Collectors;
19+
20+
@State(Scope.Benchmark)
21+
@BenchmarkMode(Mode.Throughput)
22+
@OutputTimeUnit(TimeUnit.SECONDS)
23+
@Fork(2)
24+
public class IntegerListFilter {
25+
26+
private List<Integer> jdkIntList;
27+
private MutableList<Integer> ecMutableList;
28+
private IntList ecIntList;
29+
private ExecutorService executor;
30+
31+
@Setup
32+
public void setup() {
33+
PrimitiveIterator.OfInt iterator = new Random(1L).ints(-10000, 10000).iterator();
34+
ecMutableList = FastList.newWithNValues(1_000_000, iterator::nextInt);
35+
jdkIntList = new ArrayList<>(1_000_000);
36+
jdkIntList.addAll(ecMutableList);
37+
ecIntList = ecMutableList.collectInt(i -> i, new IntArrayList(1_000_000));
38+
executor = Executors.newWorkStealingPool();
39+
}
40+
41+
@Benchmark
42+
public List<Integer> jdkList() {
43+
return jdkIntList.stream().filter(i -> i % 5 == 0).collect(Collectors.toList());
44+
}
45+
46+
@Benchmark
47+
public MutableList<Integer> ecMutableList() {
48+
return ecMutableList.select(i -> i % 5 == 0);
49+
}
50+
51+
@Benchmark
52+
public List<Integer> jdkListParallel() {
53+
return jdkIntList.parallelStream().filter(i -> i % 5 == 0).collect(Collectors.toList());
54+
}
55+
56+
@Benchmark
57+
public MutableList<Integer> ecMutableListParallel() {
58+
return ecMutableList.asParallel(executor, 100_000).select(i -> i % 5 == 0).toList();
59+
}
60+
61+
@Benchmark
62+
public IntList ecPrimitive() {
63+
return this.ecIntList.select(i -> i % 5 == 0);
64+
}
65+
66+
@Benchmark
67+
public IntList ecPrimitiveParallel() {
68+
return this.ecIntList.primitiveParallelStream().filter(i -> i % 5 == 0).collect(IntLists.mutable::empty, MutableIntList::add, MutableIntList::addAll);
69+
}
70+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.baeldung.benchmark;
2+
3+
import org.eclipse.collections.api.list.MutableList;
4+
import org.eclipse.collections.api.list.primitive.IntList;
5+
import org.eclipse.collections.impl.list.mutable.FastList;
6+
import org.eclipse.collections.impl.list.mutable.primitive.IntArrayList;
7+
import org.openjdk.jmh.annotations.*;
8+
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
import java.util.PrimitiveIterator;
12+
import java.util.Random;
13+
import java.util.concurrent.ExecutorService;
14+
import java.util.concurrent.Executors;
15+
import java.util.concurrent.TimeUnit;
16+
17+
@State(Scope.Benchmark)
18+
@BenchmarkMode(Mode.Throughput)
19+
@OutputTimeUnit(TimeUnit.SECONDS)
20+
@Fork(2)
21+
public class IntegerListSum {
22+
23+
private List<Integer> jdkIntList;
24+
private MutableList<Integer> ecMutableList;
25+
private ExecutorService executor;
26+
private IntList ecIntList;
27+
28+
@Setup
29+
public void setup() {
30+
PrimitiveIterator.OfInt iterator = new Random(1L).ints(-10000, 10000).iterator();
31+
ecMutableList = FastList.newWithNValues(1_000_000, iterator::nextInt);
32+
jdkIntList = new ArrayList<>(1_000_000);
33+
jdkIntList.addAll(ecMutableList);
34+
ecIntList = ecMutableList.collectInt(i -> i, new IntArrayList(1_000_000));
35+
executor = Executors.newWorkStealingPool();
36+
}
37+
38+
@Benchmark
39+
public long jdkList() {
40+
return jdkIntList.stream().mapToLong(i -> i).sum();
41+
}
42+
43+
@Benchmark
44+
public long ecMutableList() {
45+
return ecMutableList.sumOfInt(i -> i);
46+
}
47+
48+
@Benchmark
49+
public long jdkListParallel() {
50+
return jdkIntList.parallelStream().mapToLong(i -> i).sum();
51+
}
52+
53+
@Benchmark
54+
public long ecMutableListParallel() {
55+
return ecMutableList.asParallel(executor, 100_000).sumOfInt(i -> i);
56+
}
57+
58+
@Benchmark
59+
public long ecPrimitive() {
60+
return this.ecIntList.sum();
61+
}
62+
63+
@Benchmark
64+
public long ecPrimitiveParallel() {
65+
return this.ecIntList.primitiveParallelStream().sum();
66+
}
67+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.benchmark;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertEquals;
7+
8+
9+
public class IntegerListFilterUnitTest {
10+
11+
private IntegerListFilter integerListFilter;
12+
13+
@Before
14+
public void init() {
15+
integerListFilter = new IntegerListFilter();
16+
integerListFilter.setup();
17+
}
18+
19+
@Test
20+
public void whenBenchmarkIsExecute_thenJDKListsMustBeOfSameSize() {
21+
assertEquals(integerListFilter.jdkList().size(), integerListFilter.jdkListParallel().size());
22+
}
23+
24+
@Test
25+
public void whenBenchmarkIsExecute_thenMutableListsMustBeOfSameSize() {
26+
assertEquals(integerListFilter.ecMutableList().size(), integerListFilter.ecMutableListParallel().size());
27+
}
28+
29+
@Test
30+
public void whenBenchmarkIsExecute_thenPrimitiveListsMustBeOfSameSize() {
31+
assertEquals(integerListFilter.ecPrimitive().size(), integerListFilter.ecPrimitiveParallel().size());
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.benchmark;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertEquals;
7+
8+
9+
public class IntegerListSumUnitTest {
10+
11+
private IntegerListSum integerListSum;
12+
13+
@Before
14+
public void init() {
15+
integerListSum = new IntegerListSum();
16+
integerListSum.setup();
17+
}
18+
19+
@Test
20+
public void whenBenchmarkIsExecute_thenJDKListsMustHaveSameValue() {
21+
assertEquals(integerListSum.jdkList(), integerListSum.jdkListParallel());
22+
}
23+
24+
@Test
25+
public void whenBenchmarkIsExecute_thenMutableListsMustHaveSameValue() {
26+
assertEquals(integerListSum.ecMutableList(), integerListSum.ecMutableListParallel());
27+
}
28+
29+
@Test
30+
public void whenBenchmarkIsExecute_thenPrimitiveListsMustHaveSameValue() {
31+
assertEquals(integerListSum.ecPrimitive(), integerListSum.ecPrimitiveParallel());
32+
}
33+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Sample file content

0 commit comments

Comments
 (0)