diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
new file mode 100644
index 0000000..695b3f5
--- /dev/null
+++ b/.github/workflows/build.yml
@@ -0,0 +1,24 @@
+name: Build
+
+on:
+ push:
+ branches: [master]
+ pull_request:
+
+jobs:
+ build:
+ runs-on: ubuntu-latest
+ strategy:
+ matrix:
+ java: [17, 21, 25]
+ steps:
+ - uses: actions/checkout@v4
+ - uses: actions/setup-java@v4
+ with:
+ distribution: temurin
+ java-version: ${{ matrix.java }}
+ cache: maven
+ - name: Build
+ run: mvn -B -ntp package -DskipTests
+ - name: List benchmarks
+ run: java -jar target/benchmarks.jar -l
diff --git a/.travis.yml b/.travis.yml
deleted file mode 100644
index 01580c2..0000000
--- a/.travis.yml
+++ /dev/null
@@ -1,14 +0,0 @@
-language: java
-jdk:
- - oraclejdk8
- - oraclejdk9
- - oraclejdk10
- - oraclejdk11
- - oraclejdk12
- - oraclejdk13
- - openjdk8
- - openjdk9
- - openjdk10
- - openjdk11
- - openjdk12
- - openjdk13
diff --git a/README.md b/README.md
index 09deca6..601612a 100644
--- a/README.md
+++ b/README.md
@@ -1,20 +1,20 @@
# Java Micro-Benchmarks With JMH
-[](https://travis-ci.org/crowdcode-de/java-benchmark)
+[](https://github.com/crowdcode-de/java-benchmarks/actions/workflows/build.yml)
-These benchmarks are build with [OpenJDK JMH](https://openjdk.java.net/projects/code-tools/jmh/).
+These benchmarks are build with [OpenJDK JMH](https://github.com/openjdk/jmh).
Just another collection of typical Java Code Benchmarks.
### How To Run The Benchmarks
-Build the project `mvn clean install`.
+Build the project `mvn clean install` (requires JDK 17 or newer).
You can call `java -jar target/benchmarks.jar -help` to see available command line parameters.
For instance,
```
- java -Xmx1G -jar targget/benchmarks.jar -f 1 -r 5 -i -wi 5 EmptyBenchmark
+ java -Xmx1G -jar target/benchmarks.jar -f 1 -r 5 -i 5 -wi 5 EmptyBenchmark
```
### Create Your Own Benchmark
@@ -24,7 +24,7 @@ mvn archetype:generate
-DinteractiveMode=false
-DarchetypeGroupId=org.openjdk.jmh
-DarchetypeArtifactId=jmh-java-benchmark-archetype
- -DarchetypeVersion=1.21
+ -DarchetypeVersion=1.37
-DgroupId=org.sample
-DartifactId=test
-Dversion=1.0-SNAPSHOT
@@ -38,6 +38,8 @@ experiments, perform baseline and negative tests that provide experimental contr
the benchmarking environment is safe on JVM/OS/HW level, ask for reviews from the domain experts.
Do not assume the numbers tell you what you want them to tell.
+All results below were measured with OpenJDK 25.0.4 (Zulu) on an Apple M4 Max (macOS, aarch64).
+
### Use Multiply Instead Of Divide
Check `MultiplyVsDivisionBenchmark` to see that it is faster to multiply than to divide doubles.
@@ -56,63 +58,60 @@ better use
double a = nextDouble() * factor; // repeat this 1_000 times
```
-Results with JDK 13:
-
````
java -Xmx1G -jar target/benchmarks.jar -f 1 -r 1 -i 5 -w 1 -wi 5 MultiplyVsDivisionBenchmark
````
-|Benchmark | Mode | Cnt | Score | Error | Units|
-|:-----------------------------------------------------|:-----:|:----:|:----------:|:----------:|:----:|
-|MultiplyVsDivisionBenchmark.divideDoubles | thrpt | 5 | 207619,369 | ± 2662,392 | ops/s|
-|MultiplyVsDivisionBenchmark.divideDoublesWithMultiply | thrpt | 5 | 508495,675 | ± 8033,898 | ops/s|
+|Benchmark | Mode | Cnt | Score | Error | Units|
+|:-----------------------------------------------------|:-----:|:---:|:-----------:|:------------:|:----:|
+|MultiplyVsDivisionBenchmark.divideDoubles | thrpt | 5 | 409528,749 | ± 208420,064 | ops/s|
+|MultiplyVsDivisionBenchmark.divideDoublesWithMultiply | thrpt | 5 | 1750332,557 | ± 15010,078 | ops/s|
### Random Subset Without Duplicates
`RandomSubsetBenchmark`
-Results with JDK 13:
-
````
java -Xmx1G -jar target/benchmarks.jar -f 1 -r 1 -i 5 -w 1 -wi 5 RandomSubsetBenchmark
````
-|Benchmark | Mode | Cnt | Score | Error | Units|
-|:-----------------------------------------------------|:-----:|:----:|:----------:|:----------:|:----:|
-|RandomSubsetBenchmark.subsetWithForLoop | thrpt | 5| 1720,292 | ± 330,771 | ops/s|
-|RandomSubsetBenchmark.subsetWithParallelStreams | thrpt | 5| 779,079 | ± 23,238 | ops/s|
-|RandomSubsetBenchmark.subsetWithStreams | thrpt | 5| 1200,939 | ± 140,346 | ops/s|
+|Benchmark | Mode | Cnt | Score | Error | Units|
+|:-----------------------------------------------------|:-----:|:---:|:---------:|:----------:|:----:|
+|RandomSubsetBenchmark.subsetWithForLoop | thrpt | 5 | 14836,280 | ± 1454,678 | ops/s|
+|RandomSubsetBenchmark.subsetWithParallelStreams | thrpt | 5 | 190,552 | ± 16,030 | ops/s|
+|RandomSubsetBenchmark.subsetWithStreams | thrpt | 5 | 2439,445 | ± 277,583 | ops/s|
### Date Parsing And Adding An Hour Benchmark
`DateBenchmark`
-
````
java -Xmx1G -jar target/benchmarks.jar -f 1 -r 1 -i 5 -w 1 -wi 5 DateBenchmark
````
-|Benchmark | Mode | Cnt | Score | Error | Units|
-|:-----------------------------------------------------|:-----:|:----:|:----------:|:----------:|:----:|
-|DateBenchmark.dateLong | thrpt | 5 | 386571214,471 | ± 10171784,541 | ops/s|
-|DateBenchmark.dateLongWithFormat | thrpt | 5 | 3358368,673 | ± 31265,425 | ops/s|
-|DateBenchmark.dateParsingWithFormat | thrpt | 5 | 728327,799 | ± 19937,432 | ops/s|
-|DateBenchmark.parsingLocalDateTime | thrpt | 5 | 873308,951 | ± 97291,855 | ops/s|
+|Benchmark | Mode | Cnt | Score | Error | Units|
+|:-----------------------------------|:-----:|:---:|:--------------:|:---------------:|:----:|
+|DateBenchmark.dateLong | thrpt | 5 | 2331729611,705 | ± 513245910,474 | ops/s|
+|DateBenchmark.dateLongWithFormat | thrpt | 5 | 9561979,184 | ± 798131,749 | ops/s|
+|DateBenchmark.dateParsingWithFormat | thrpt | 5 | 2718024,132 | ± 17929,441 | ops/s|
+|DateBenchmark.parsingLocalDateTime | thrpt | 5 | 2847071,202 | ± 52263,829 | ops/s|
### Using Initial Capacity At ArrayList
-Run with JDK 12 and `java -jar target/benchmarks.jar -f 1 -r 2 -w 2 -wi 5 InitialCapacityBenchmark`
+`InitialCapacityBenchmark`
-|Benchmark | Mode | Cnt | Score | Error | Units|
-|:-------------------------------------------------------------|:-----:|:----:|:-----:|:--------:|:----:|
-|InitialCapacityBenchmark.addWithInitialCapacity | thrpt | 5 | 29,552 | ± 13,596 | ops/s|
-|InitialCapacityBenchmark.addWithInitialCapacityWithStreams | thrpt | 5 | 25,123 | ± 10,103 | ops/s|
-|InitialCapacityBenchmark.addWithoutInitialCapacity | thrpt | 5 | 19,304 | ± 3,389 | ops/s|
-|InitialCapacityBenchmark.addWithoutInitialCapacityWithStreams | thrpt | 5 | 19,012 | ± 6,849 | ops/s|
+````
+java -jar target/benchmarks.jar -f 1 -r 2 -w 2 -wi 5 InitialCapacityBenchmark
+````
-
+|Benchmark | Mode | Cnt | Score | Error | Units|
+|:-------------------------------------------------------------|:-----:|:---:|:-------:|:--------:|:----:|
+|InitialCapacityBenchmark.addWithInitialCapacity | thrpt | 5 | 659,095 | ± 7,489 | ops/s|
+|InitialCapacityBenchmark.addWithInitialCapacityWithStreams | thrpt | 5 | 402,918 | ± 67,462 | ops/s|
+|InitialCapacityBenchmark.addWithoutInitialCapacity | thrpt | 5 | 238,364 | ± 34,793 | ops/s|
+|InitialCapacityBenchmark.addWithoutInitialCapacityWithStreams | thrpt | 5 | 183,303 | ± 11,041 | ops/s|
### Created By
diff --git a/pom.xml b/pom.xml
index 96e234e..544629b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -42,13 +42,14 @@ THE POSSIBILITY OF SUCH DAMAGE.
CROWDCODE GmbH + Co. KG
- https://www.crowdcode
+ https://www.crowdcode.io
UTF-8
1.37
- 1.8
+ 17
+ 6.1.3
benchmarks
@@ -67,22 +68,15 @@ THE POSSIBILITY OF SUCH DAMAGE.
org.junit.jupiter
junit-jupiter-api
+ ${junit.version}
test
- 5.5.2
org.junit.jupiter
junit-jupiter-engine
+ ${junit.version}
test
- 5.5.2
-
- org.mockito
- mockito-junit-jupiter
- 3.1.0
- test
-
-
@@ -90,17 +84,23 @@ THE POSSIBILITY OF SUCH DAMAGE.
org.apache.maven.plugins
maven-compiler-plugin
- 3.11.0
+ 3.16.0
- ${javac.target}
- ${javac.target}
- ${javac.target}
+ ${java.release}
+
+
+
+ org.openjdk.jmh
+ jmh-generator-annprocess
+ ${jmh.version}
+
+
org.apache.maven.plugins
maven-shade-plugin
- 3.5.0
+ 3.6.2
package
@@ -137,39 +137,39 @@ THE POSSIBILITY OF SUCH DAMAGE.
maven-clean-plugin
- 3.2.0
+ 3.5.0
maven-deploy-plugin
- 3.1.1
+ 3.2.0
maven-install-plugin
- 3.1.1
+ 3.2.0
maven-jar-plugin
- 3.3.0
+ 3.5.1
maven-javadoc-plugin
- 3.5.0
+ 3.12.0
maven-resources-plugin
- 3.3.1
+ 3.5.0
maven-site-plugin
- 3.12.1
+ 3.22.0
maven-source-plugin
- 3.3.0
+ 3.4.0
maven-surefire-plugin
- 3.1.2
+ 3.6.1
diff --git a/src/main/java/io/crowdcode/java/benchmarks/collections/HashMapBenchmark.java b/src/main/java/io/crowdcode/java/benchmarks/collections/HashMapBenchmark.java
index 169ceda..43e6d13 100644
--- a/src/main/java/io/crowdcode/java/benchmarks/collections/HashMapBenchmark.java
+++ b/src/main/java/io/crowdcode/java/benchmarks/collections/HashMapBenchmark.java
@@ -1,33 +1,43 @@
package io.crowdcode.java.benchmarks.collections;
+import io.crowdcode.java.benchmarks.collections.model.BrokenHashCodeComparableKey;
+import io.crowdcode.java.benchmarks.collections.model.BrokenHashCodeKey;
+import io.crowdcode.java.benchmarks.collections.model.HashCodeKey;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;
+import java.util.HashMap;
+
+/**
+ * Looks up {@link HashMapState#LOOKUPS} existing keys per invocation.
+ */
@State(Scope.Benchmark)
public class HashMapBenchmark {
@Benchmark
public void getBrokenWithComparableKeys(HashMapState state, Blackhole blackhole) {
- state.getHashMapWithBrokenHashCodeComparable().values().stream()
- .map(k -> state.getHashMap().get(k))
- .forEach(blackhole::consume);
+ HashMap map = state.getHashMapWithBrokenHashCodeComparable();
+ for (BrokenHashCodeComparableKey key : state.getBrokenComparableLookupKeys()) {
+ blackhole.consume(map.get(key));
+ }
}
@Benchmark
- public void getBrokenKeys(HashMapState state, Blackhole blackhole) {
- state.getHashMap().values().stream()
- .map(k -> state.getHashMapWithBrokenHashCode().get(k))
- .forEach(blackhole::consume);
- }
+ public void getBrokenKeys(HashMapState state, Blackhole blackhole) {
+ HashMap map = state.getHashMapWithBrokenHashCode();
+ for (BrokenHashCodeKey key : state.getBrokenLookupKeys()) {
+ blackhole.consume(map.get(key));
+ }
+ }
@Benchmark
public void getRandomKeys(HashMapState state, Blackhole blackhole) {
- state.getHashMap().values().stream()
- .map(k -> state.getHashMap().get(k))
- .forEach(blackhole::consume);
+ HashMap map = state.getHashMap();
+ for (HashCodeKey key : state.getLookupKeys()) {
+ blackhole.consume(map.get(key));
+ }
}
-
}
diff --git a/src/main/java/io/crowdcode/java/benchmarks/collections/HashMapState.java b/src/main/java/io/crowdcode/java/benchmarks/collections/HashMapState.java
index 595d6fe..d0ebe21 100644
--- a/src/main/java/io/crowdcode/java/benchmarks/collections/HashMapState.java
+++ b/src/main/java/io/crowdcode/java/benchmarks/collections/HashMapState.java
@@ -16,6 +16,11 @@
public class HashMapState {
public static final int AMOUNT = 50_000;
+ public static final int LOOKUPS = 1_000;
+
+ private final HashCodeKey[] lookupKeys = new HashCodeKey[LOOKUPS];
+ private final BrokenHashCodeKey[] brokenLookupKeys = new BrokenHashCodeKey[LOOKUPS];
+ private final BrokenHashCodeComparableKey[] brokenComparableLookupKeys = new BrokenHashCodeComparableKey[LOOKUPS];
private HashMap hashMap;
private HashMap hashMapWithBrokenHashCode;
@@ -32,6 +37,26 @@ public HashMapState() {
hashMapWithBrokenHashCode.put(new BrokenHashCodeKey(i), uuid);
hashMapWithBrokenHashCodeComparable.put(new BrokenHashCodeComparableKey(i), uuid);
}
+
+ // keys to look up, spread evenly over all stored keys
+ for (int i = 0; i < LOOKUPS; i++) {
+ long key = (long) i * (AMOUNT / LOOKUPS);
+ lookupKeys[i] = new HashCodeKey(key);
+ brokenLookupKeys[i] = new BrokenHashCodeKey(key);
+ brokenComparableLookupKeys[i] = new BrokenHashCodeComparableKey(key);
+ }
+ }
+
+ public HashCodeKey[] getLookupKeys() {
+ return lookupKeys;
+ }
+
+ public BrokenHashCodeKey[] getBrokenLookupKeys() {
+ return brokenLookupKeys;
+ }
+
+ public BrokenHashCodeComparableKey[] getBrokenComparableLookupKeys() {
+ return brokenComparableLookupKeys;
}
public HashMap getHashMap() {
diff --git a/src/main/java/io/crowdcode/java/benchmarks/time/LocalDateTimeNowBenchmark.java b/src/main/java/io/crowdcode/java/benchmarks/time/LocalDateTimeNowBenchmark.java
index a6b8566..78bb3c4 100644
--- a/src/main/java/io/crowdcode/java/benchmarks/time/LocalDateTimeNowBenchmark.java
+++ b/src/main/java/io/crowdcode/java/benchmarks/time/LocalDateTimeNowBenchmark.java
@@ -14,16 +14,16 @@ public class LocalDateTimeNowBenchmark {
@Benchmark
- public void previousIsRunning() {
- boolean result = (beginDateTime.isBefore(LocalDateTime.now())
+ public boolean previousIsRunning() {
+ return (beginDateTime.isBefore(LocalDateTime.now())
|| beginDateTime.isEqual(LocalDateTime.now()))
&& expireDateTime.isAfter(LocalDateTime.now());
}
@Benchmark
- public void optimizedIsRunning() {
+ public boolean optimizedIsRunning() {
LocalDateTime now = LocalDateTime.now();
- boolean result = (beginDateTime.isBefore(now)
+ return (beginDateTime.isBefore(now)
|| beginDateTime.isEqual(now))
&& expireDateTime.isAfter(now);
}