Skip to content

Commit 7dc08b0

Browse files
committed
add bench
0 parents  commit 7dc08b0

File tree

5 files changed

+328
-0
lines changed

5 files changed

+328
-0
lines changed

.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
release/
2+
target/
3+
.idea/
4+
*.iml
5+
*/*.iml
6+
.DS_Store
7+
classes/
8+
generated/
9+
a.out
10+
*.dylib
11+
*.so
12+
*.dll
13+
hs_err_pid*.log
14+
out/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Deepu K Sasidharan
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Readme.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Java Loom benchmarks
2+
3+
Benchmarks for Java Project Loom Virtual threads vs Platform Threads with JDK (openjdk 19 2022-09-20)
4+
5+
Setup instructions for benchmark:
6+
7+
## Clone the project
8+
9+
```bash
10+
git clone https://github.com/deepu105/java-loom-benchmarks.git
11+
```
12+
13+
## Setup Java 19 EA build using SDK man
14+
15+
```bash
16+
sdk install java 19.ea.36-open
17+
sdk use java 19.ea.36-open
18+
```
19+
20+
## Build and run using Maven
21+
22+
```bash
23+
mvn clean verify
24+
java -jar target/benchmarks.jar
25+
```
26+
27+
## Typical Results
28+
29+
These results vary because they were ran on a developer machine with other services running. Also, according to the JMH
30+
docs, to avoid `blackholes` (methods that return void). e.g. If you call a method and return void the JVM will optimize
31+
by removing dead code. To ensure the code isn't removed the method returns teh current time.
32+
33+
Below are some results (smaller numbers are better).
34+
35+
### Linux (JDK 19)
36+
37+
```text
38+
Benchmark Mode Cnt Score Error Units
39+
LoomBenchmark.platformThreadPerTask thrpt 5 0.362 ± 0.079 ops/s
40+
LoomBenchmark.platformThreadPool thrpt 5 0.528 ± 0.067 ops/s
41+
LoomBenchmark.virtualThreadPerTask thrpt 5 1.843 ± 0.093 ops/s
42+
LoomBenchmark.platformThreadPerTask avgt 5 5.600 ± 0.768 s/op
43+
LoomBenchmark.platformThreadPool avgt 5 3.887 ± 0.717 s/op
44+
LoomBenchmark.virtualThreadPerTask avgt 5 1.098 ± 0.020 s/op
45+
```

pom.xml

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
<!--
2+
Copyright (c) 2014, Oracle America, Inc.
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
8+
* Redistributions of source code must retain the above copyright notice,
9+
this list of conditions and the following disclaimer.
10+
11+
* Redistributions in binary form must reproduce the above copyright
12+
notice, this list of conditions and the following disclaimer in the
13+
documentation and/or other materials provided with the distribution.
14+
15+
* Neither the name of Oracle nor the names of its contributors may be used
16+
to endorse or promote products derived from this software without
17+
specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29+
THE POSSIBILITY OF SUCH DAMAGE.
30+
-->
31+
32+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
34+
<modelVersion>4.0.0</modelVersion>
35+
36+
<groupId>org.sample</groupId>
37+
<artifactId>test</artifactId>
38+
<version>1.0</version>
39+
<packaging>jar</packaging>
40+
41+
<name>JMH benchmark sample: Java</name>
42+
43+
<!--
44+
This is the demo/sample template build script for building Java benchmarks with JMH.
45+
Edit as needed.
46+
-->
47+
48+
<dependencies>
49+
<dependency>
50+
<groupId>org.openjdk.jmh</groupId>
51+
<artifactId>jmh-core</artifactId>
52+
<version>${jmh.version}</version>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.openjdk.jmh</groupId>
56+
<artifactId>jmh-generator-annprocess</artifactId>
57+
<version>${jmh.version}</version>
58+
<scope>provided</scope>
59+
</dependency>
60+
</dependencies>
61+
62+
<properties>
63+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
64+
<jmh.version>1.34</jmh.version>
65+
<javacpp.version>1.4.4</javacpp.version>
66+
<javac.target>19</javac.target>
67+
<uberjar.name>benchmarks</uberjar.name>
68+
</properties>
69+
70+
<build>
71+
<plugins>
72+
<plugin>
73+
<groupId>org.codehaus.mojo</groupId>
74+
<artifactId>build-helper-maven-plugin</artifactId>
75+
<version>3.0.0</version>
76+
<executions>
77+
<execution>
78+
<phase>generate-sources</phase>
79+
<goals>
80+
<goal>add-source</goal>
81+
</goals>
82+
<configuration>
83+
<sources>
84+
<source>generated/src/main/java</source>
85+
</sources>
86+
</configuration>
87+
</execution>
88+
</executions>
89+
</plugin>
90+
<plugin>
91+
<groupId>org.apache.maven.plugins</groupId>
92+
<artifactId>maven-compiler-plugin</artifactId>
93+
<version>3.8.0</version>
94+
<configuration>
95+
<compilerVersion>${javac.target}</compilerVersion>
96+
<source>${javac.target}</source>
97+
<target>${javac.target}</target>
98+
<compilerArgs>
99+
<arg>--enable-preview</arg>
100+
</compilerArgs>
101+
</configuration>
102+
</plugin>
103+
<plugin>
104+
<groupId>org.apache.maven.plugins</groupId>
105+
<artifactId>maven-shade-plugin</artifactId>
106+
<version>3.2.1</version>
107+
<executions>
108+
<execution>
109+
<phase>package</phase>
110+
<goals>
111+
<goal>shade</goal>
112+
</goals>
113+
<configuration>
114+
<finalName>${uberjar.name}</finalName>
115+
<transformers>
116+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
117+
<mainClass>org.openjdk.jmh.Main</mainClass>
118+
</transformer>
119+
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
120+
</transformers>
121+
<filters>
122+
<filter>
123+
<!--
124+
Shading signed JARs will fail without this.
125+
http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar
126+
-->
127+
<artifact>*:*</artifact>
128+
<excludes>
129+
<exclude>META-INF/*.SF</exclude>
130+
<exclude>META-INF/*.DSA</exclude>
131+
<exclude>META-INF/*.RSA</exclude>
132+
</excludes>
133+
</filter>
134+
</filters>
135+
</configuration>
136+
</execution>
137+
</executions>
138+
</plugin>
139+
</plugins>
140+
<pluginManagement>
141+
<plugins>
142+
<plugin>
143+
<artifactId>maven-clean-plugin</artifactId>
144+
<version>2.5</version>
145+
</plugin>
146+
<plugin>
147+
<artifactId>maven-deploy-plugin</artifactId>
148+
<version>2.8.1</version>
149+
</plugin>
150+
<plugin>
151+
<artifactId>maven-install-plugin</artifactId>
152+
<version>2.5.1</version>
153+
</plugin>
154+
<plugin>
155+
<artifactId>maven-jar-plugin</artifactId>
156+
<version>2.4</version>
157+
</plugin>
158+
<plugin>
159+
<artifactId>maven-javadoc-plugin</artifactId>
160+
<version>2.9.1</version>
161+
</plugin>
162+
<plugin>
163+
<artifactId>maven-resources-plugin</artifactId>
164+
<version>2.6</version>
165+
</plugin>
166+
<plugin>
167+
<artifactId>maven-site-plugin</artifactId>
168+
<version>3.3</version>
169+
</plugin>
170+
<plugin>
171+
<artifactId>maven-source-plugin</artifactId>
172+
<version>2.2.1</version>
173+
</plugin>
174+
<plugin>
175+
<artifactId>maven-surefire-plugin</artifactId>
176+
<version>2.17</version>
177+
</plugin>
178+
</plugins>
179+
</pluginManagement>
180+
</build>
181+
182+
</project>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.sample;
2+
3+
4+
import org.openjdk.jmh.annotations.*;
5+
import org.openjdk.jmh.runner.Runner;
6+
import org.openjdk.jmh.runner.RunnerException;
7+
import org.openjdk.jmh.runner.options.Options;
8+
import org.openjdk.jmh.runner.options.OptionsBuilder;
9+
10+
import java.time.Duration;
11+
import java.util.concurrent.Executors;
12+
import java.util.stream.IntStream;
13+
14+
@Fork(value = 1, jvmArgs = {"-Xms512m", "-Xmx1024m", "--enable-preview"})
15+
@BenchmarkMode({Mode.Throughput, Mode.AverageTime})
16+
@Warmup(time = 2)
17+
@State(Scope.Benchmark)
18+
@Timeout(time = 60)
19+
@Threads(2)
20+
public class LoomBenchmark {
21+
22+
public static void main(String[] args) throws RunnerException {
23+
Options opt = new OptionsBuilder()
24+
.include(LoomBenchmark.class.getSimpleName())
25+
.forks(1)
26+
.build();
27+
28+
new Runner(opt).run();
29+
}
30+
31+
@Benchmark
32+
public long platformThreadPool() {
33+
try (var executor = Executors.newCachedThreadPool()) {
34+
IntStream.range(0, 10_000).forEach(i -> executor.submit(() -> {
35+
Thread.sleep(Duration.ofSeconds(1));
36+
System.out.println(i);
37+
return i;
38+
}));
39+
}
40+
return System.currentTimeMillis();
41+
}
42+
43+
@Benchmark
44+
public long platformThreadPerTask() {
45+
try (var executor = Executors.newThreadPerTaskExecutor(Executors.defaultThreadFactory())) {
46+
IntStream.range(0, 10_000).forEach(i -> executor.submit(() -> {
47+
Thread.sleep(Duration.ofSeconds(1));
48+
System.out.println(i);
49+
return i;
50+
}));
51+
}
52+
return System.currentTimeMillis();
53+
}
54+
55+
@Benchmark
56+
public long virtualThreadPerTask() {
57+
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
58+
IntStream.range(0, 10_000).forEach(i -> executor.submit(() -> {
59+
Thread.sleep(Duration.ofSeconds(1));
60+
System.out.println(i);
61+
return i;
62+
}));
63+
}
64+
return System.currentTimeMillis();
65+
}
66+
}

0 commit comments

Comments
 (0)