BAEL-4876: Add more benchmarks
This commit is contained in:
parent
bce6207f03
commit
86c36061c9
@ -30,12 +30,12 @@
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>${jmh-core.version}</version>
|
||||
<version>${jmh.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh-generator.version}</version>
|
||||
<version>${jmh.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
@ -55,12 +55,30 @@
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<lombok.version>1.18.20</lombok.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<jmh.version>1.29</jmh.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
@ -0,0 +1,9 @@
|
||||
package com.baeldung.streams.parallel;
|
||||
|
||||
public class BenchmarkRunner {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
org.openjdk.jmh.Main.main(args);
|
||||
}
|
||||
|
||||
}
|
@ -3,10 +3,12 @@ package com.baeldung.streams.parallel;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class DifferentSourceSplitting {
|
||||
@ -15,37 +17,37 @@ public class DifferentSourceSplitting {
|
||||
private static final List<Integer> linkedListOfNumbers = new LinkedList<>();
|
||||
|
||||
static {
|
||||
IntStream.rangeClosed(1, 100_000_000).forEach(i -> {
|
||||
IntStream.rangeClosed(1, 100_000).forEach(i -> {
|
||||
arrayListOfNumbers.add(i);
|
||||
linkedListOfNumbers.add(i);
|
||||
});
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
org.openjdk.jmh.Main.main(args);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public static void arrayListSequential() {
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void differentSourceArrayListSequential() {
|
||||
arrayListOfNumbers.stream().reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public static void arrayListParallel() {
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void differentSourceArrayListParallel() {
|
||||
arrayListOfNumbers.parallelStream().reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public static void linkedListSequential() {
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void differentSourceLinkedListSequential() {
|
||||
linkedListOfNumbers.stream().reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public static void linkedListParallel() {
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void differentSourceLinkedListParallel() {
|
||||
linkedListOfNumbers.parallelStream().reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,52 @@
|
||||
package com.baeldung.streams.parallel;
|
||||
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class MemoryLocalityCosts {
|
||||
|
||||
private static final int[] intArray = new int[100_000];
|
||||
private static final Integer[] integerArray = new Integer[100_000];
|
||||
|
||||
static {
|
||||
IntStream.rangeClosed(1, 100_000).forEach(i -> {
|
||||
intArray[i-1] = i;
|
||||
integerArray[i-1] = i;
|
||||
});
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void localityIntArraySequential() {
|
||||
Arrays.stream(intArray).reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void localityIntArrayParallel() {
|
||||
Arrays.stream(intArray).parallel().reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void localityIntegerArraySequential() {
|
||||
Arrays.stream(integerArray).reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void localityIntegerArrayParallel() {
|
||||
Arrays.stream(integerArray).parallel().reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package com.baeldung.streams.parallel;
|
||||
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class MergingCosts {
|
||||
|
||||
private static final List<Integer> arrayListOfNumbers = new ArrayList<>();
|
||||
|
||||
static {
|
||||
IntStream.rangeClosed(1, 100_000).forEach(i -> {
|
||||
arrayListOfNumbers.add(i);
|
||||
});
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void mergingCostsSumSequential() {
|
||||
arrayListOfNumbers.stream().reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void mergingCostsSumParallel() {
|
||||
arrayListOfNumbers.stream().parallel().reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void mergingCostsGroupingSequential() {
|
||||
arrayListOfNumbers.stream().collect(Collectors.groupingBy(i -> i % 2 == 0));
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void mergingCostsGroupingParallel() {
|
||||
arrayListOfNumbers.stream().parallel().collect(Collectors.groupingBy(i -> i % 2 == 0));
|
||||
}
|
||||
|
||||
}
|
@ -3,24 +3,25 @@ package com.baeldung.streams.parallel;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class SplittingCosts {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
org.openjdk.jmh.Main.main(args);
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void sourceSplittingIntStreamSequential() {
|
||||
IntStream.rangeClosed(1, 100).reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public static void arrayListSequential() {
|
||||
IntStream.rangeClosed(1, 1_000).reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public static void arrayListParallel() {
|
||||
IntStream.rangeClosed(1, 1_000).parallel().reduce(0, Integer::sum);
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void sourceSplittingIntStreamParallel() {
|
||||
IntStream.rangeClosed(1, 100).parallel().reduce(0, Integer::sum);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user