Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 0 additions & 14 deletions .travis.yml

This file was deleted.

65 changes: 32 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# Java Micro-Benchmarks With JMH

[![Build Status](https://travis-ci.org/crowdcode-de/java-benchmark.svg?branch=master)](https://travis-ci.org/crowdcode-de/java-benchmark)
[![Build](https://github.com/crowdcode-de/java-benchmarks/actions/workflows/build.yml/badge.svg)](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
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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
Expand Down
50 changes: 25 additions & 25 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ THE POSSIBILITY OF SUCH DAMAGE.

<organization>
<name>CROWDCODE GmbH + Co. KG</name>
<url>https://www.crowdcode</url>
<url>https://www.crowdcode.io</url>
</organization>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jmh.version>1.37</jmh.version>
<javac.target>1.8</javac.target>
<java.release>17</java.release>
<junit.version>6.1.3</junit.version>
<uberjar.name>benchmarks</uberjar.name>
</properties>

Expand All @@ -67,40 +68,39 @@ THE POSSIBILITY OF SUCH DAMAGE.
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
<version>5.5.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
<version>5.5.2</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>3.1.0</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<version>3.16.0</version>
<configuration>
<compilerVersion>${javac.target}</compilerVersion>
<source>${javac.target}</source>
<target>${javac.target}</target>
<release>${java.release}</release>
<!-- Since JDK 23 javac no longer runs annotation processors found on the class path. -->
<annotationProcessorPaths>
<path>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.0</version>
<version>3.6.2</version>
<executions>
<execution>
<phase>package</phase>
Expand Down Expand Up @@ -137,39 +137,39 @@ THE POSSIBILITY OF SUCH DAMAGE.
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.2.0</version>
<version>3.5.0</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.1</version>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.1.1</version>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<version>3.5.1</version>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.5.0</version>
<version>3.12.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
<version>3.5.0</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.12.1</version>
<version>3.22.0</version>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>3.3.0</version>
<version>3.4.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<version>3.6.1</version>
</plugin>
</plugins>
</pluginManagement>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<BrokenHashCodeComparableKey, String> 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<BrokenHashCodeKey, String> 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<HashCodeKey, String> map = state.getHashMap();
for (HashCodeKey key : state.getLookupKeys()) {
blackhole.consume(map.get(key));
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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<HashCodeKey, String> hashMap;
private HashMap<BrokenHashCodeKey, String> hashMapWithBrokenHashCode;
Expand All @@ -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<HashCodeKey, String> getHashMap() {
Expand Down
Loading
Loading