* BAEL-3414

* BAEL-3414 -> Add unit tests

* BAEL-3414 -> Clean up pom.xml
This commit is contained in:
Rodrigo Graciano
2019-11-12 15:25:27 -05:00
committed by maibin
parent d5d1cde0c9
commit ba8d6405a7
6 changed files with 263 additions and 1 deletions

View File

@@ -0,0 +1,70 @@
package com.baeldung.benchmark;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.list.primitive.IntList;
import org.eclipse.collections.api.list.primitive.MutableIntList;
import org.eclipse.collections.impl.factory.primitive.IntLists;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.list.mutable.primitive.IntArrayList;
import org.openjdk.jmh.annotations.*;
import java.util.ArrayList;
import java.util.List;
import java.util.PrimitiveIterator;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@State(Scope.Benchmark)
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
@Fork(2)
public class IntegerListFilter {
private List<Integer> jdkIntList;
private MutableList<Integer> ecMutableList;
private IntList ecIntList;
private ExecutorService executor;
@Setup
public void setup() {
PrimitiveIterator.OfInt iterator = new Random(1L).ints(-10000, 10000).iterator();
ecMutableList = FastList.newWithNValues(1_000_000, iterator::nextInt);
jdkIntList = new ArrayList<>(1_000_000);
jdkIntList.addAll(ecMutableList);
ecIntList = ecMutableList.collectInt(i -> i, new IntArrayList(1_000_000));
executor = Executors.newWorkStealingPool();
}
@Benchmark
public List<Integer> jdkList() {
return jdkIntList.stream().filter(i -> i % 5 == 0).collect(Collectors.toList());
}
@Benchmark
public MutableList<Integer> ecMutableList() {
return ecMutableList.select(i -> i % 5 == 0);
}
@Benchmark
public List<Integer> jdkListParallel() {
return jdkIntList.parallelStream().filter(i -> i % 5 == 0).collect(Collectors.toList());
}
@Benchmark
public MutableList<Integer> ecMutableListParallel() {
return ecMutableList.asParallel(executor, 100_000).select(i -> i % 5 == 0).toList();
}
@Benchmark
public IntList ecPrimitive() {
return this.ecIntList.select(i -> i % 5 == 0);
}
@Benchmark
public IntList ecPrimitiveParallel() {
return this.ecIntList.primitiveParallelStream().filter(i -> i % 5 == 0).collect(IntLists.mutable::empty, MutableIntList::add, MutableIntList::addAll);
}
}

View File

@@ -0,0 +1,67 @@
package com.baeldung.benchmark;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.list.primitive.IntList;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.list.mutable.primitive.IntArrayList;
import org.openjdk.jmh.annotations.*;
import java.util.ArrayList;
import java.util.List;
import java.util.PrimitiveIterator;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
@State(Scope.Benchmark)
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
@Fork(2)
public class IntegerListSum {
private List<Integer> jdkIntList;
private MutableList<Integer> ecMutableList;
private ExecutorService executor;
private IntList ecIntList;
@Setup
public void setup() {
PrimitiveIterator.OfInt iterator = new Random(1L).ints(-10000, 10000).iterator();
ecMutableList = FastList.newWithNValues(1_000_000, iterator::nextInt);
jdkIntList = new ArrayList<>(1_000_000);
jdkIntList.addAll(ecMutableList);
ecIntList = ecMutableList.collectInt(i -> i, new IntArrayList(1_000_000));
executor = Executors.newWorkStealingPool();
}
@Benchmark
public long jdkList() {
return jdkIntList.stream().mapToLong(i -> i).sum();
}
@Benchmark
public long ecMutableList() {
return ecMutableList.sumOfInt(i -> i);
}
@Benchmark
public long jdkListParallel() {
return jdkIntList.parallelStream().mapToLong(i -> i).sum();
}
@Benchmark
public long ecMutableListParallel() {
return ecMutableList.asParallel(executor, 100_000).sumOfInt(i -> i);
}
@Benchmark
public long ecPrimitive() {
return this.ecIntList.sum();
}
@Benchmark
public long ecPrimitiveParallel() {
return this.ecIntList.primitiveParallelStream().sum();
}
}

View File

@@ -0,0 +1,33 @@
package com.baeldung.benchmark;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class IntegerListFilterUnitTest {
private IntegerListFilter integerListFilter;
@Before
public void init() {
integerListFilter = new IntegerListFilter();
integerListFilter.setup();
}
@Test
public void whenBenchmarkIsExecute_thenJDKListsMustBeOfSameSize() {
assertEquals(integerListFilter.jdkList().size(), integerListFilter.jdkListParallel().size());
}
@Test
public void whenBenchmarkIsExecute_thenMutableListsMustBeOfSameSize() {
assertEquals(integerListFilter.ecMutableList().size(), integerListFilter.ecMutableListParallel().size());
}
@Test
public void whenBenchmarkIsExecute_thenPrimitiveListsMustBeOfSameSize() {
assertEquals(integerListFilter.ecPrimitive().size(), integerListFilter.ecPrimitiveParallel().size());
}
}

View File

@@ -0,0 +1,33 @@
package com.baeldung.benchmark;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class IntegerListSumUnitTest {
private IntegerListSum integerListSum;
@Before
public void init() {
integerListSum = new IntegerListSum();
integerListSum.setup();
}
@Test
public void whenBenchmarkIsExecute_thenJDKListsMustHaveSameValue() {
assertEquals(integerListSum.jdkList(), integerListSum.jdkListParallel());
}
@Test
public void whenBenchmarkIsExecute_thenMutableListsMustHaveSameValue() {
assertEquals(integerListSum.ecMutableList(), integerListSum.ecMutableListParallel());
}
@Test
public void whenBenchmarkIsExecute_thenPrimitiveListsMustHaveSameValue() {
assertEquals(integerListSum.ecPrimitive(), integerListSum.ecPrimitiveParallel());
}
}

View File

@@ -0,0 +1 @@
Sample file content