Skip to content

Commit 44e9b49

Browse files
committed
Benchmarks for "Shared mutable state and concurrency" section of the guide
1 parent 9ef792f commit 44e9b49

File tree

3 files changed

+323
-1
lines changed

3 files changed

+323
-1
lines changed

benchmarks/pom.xml

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2016-2017 JetBrains s.r.o.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<project
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
20+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
21+
22+
<modelVersion>4.0.0</modelVersion>
23+
24+
<parent>
25+
<groupId>org.jetbrains.kotlinx</groupId>
26+
<artifactId>kotlinx-coroutines</artifactId>
27+
<version>0.14</version>
28+
</parent>
29+
30+
<artifactId>benchmarks</artifactId>
31+
<version>0.14</version>
32+
<packaging>jar</packaging>
33+
34+
<dependencies>
35+
<dependency>
36+
<groupId>org.openjdk.jmh</groupId>
37+
<artifactId>jmh-core</artifactId>
38+
<version>${jmh.version}</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.jetbrains.kotlinx</groupId>
42+
<artifactId>kotlinx-coroutines-core</artifactId>
43+
<version>${project.version}</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.jetbrains.kotlinx</groupId>
47+
<artifactId>kotlinx-coroutines-core</artifactId>
48+
<version>${project.version}</version>
49+
<classifier>tests</classifier>
50+
</dependency>
51+
</dependencies>
52+
53+
<properties>
54+
<jmh.version>1.18</jmh.version>
55+
<javac.target>1.8</javac.target>
56+
<jmh.generator>default</jmh.generator>
57+
<uberjar.name>benchmarks</uberjar.name>
58+
</properties>
59+
60+
<build>
61+
<plugins>
62+
<!--
63+
1. Compile Kotlin sources first.
64+
-->
65+
66+
<plugin>
67+
<artifactId>kotlin-maven-plugin</artifactId>
68+
<groupId>org.jetbrains.kotlin</groupId>
69+
<!--
70+
Put an approriate Kotlin compiler version here.
71+
-->
72+
<version>${kotlin.version}</version>
73+
74+
<executions>
75+
<execution>
76+
<id>process-sources</id>
77+
<phase>generate-sources</phase>
78+
<goals>
79+
<goal>compile</goal>
80+
</goals>
81+
<configuration>
82+
<sourceDirs>
83+
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
84+
</sourceDirs>
85+
</configuration>
86+
</execution>
87+
88+
<execution>
89+
<id>process-test-sources</id>
90+
<phase>test-compile</phase>
91+
<goals>
92+
<goal>test-compile</goal>
93+
</goals>
94+
</execution>
95+
</executions>
96+
</plugin>
97+
98+
<!--
99+
2. Invoke JMH generators to produce benchmark code
100+
-->
101+
102+
<plugin>
103+
<groupId>org.codehaus.mojo</groupId>
104+
<artifactId>exec-maven-plugin</artifactId>
105+
<version>1.2.1</version>
106+
<executions>
107+
<execution>
108+
<phase>process-sources</phase>
109+
<goals>
110+
<goal>java</goal>
111+
</goals>
112+
<configuration>
113+
<includePluginDependencies>true</includePluginDependencies>
114+
<mainClass>org.openjdk.jmh.generators.bytecode.JmhBytecodeGenerator</mainClass>
115+
<arguments>
116+
<argument>${project.basedir}/target/classes/</argument>
117+
<argument>${project.basedir}/target/generated-sources/jmh/</argument>
118+
<argument>${project.basedir}/target/classes/</argument>
119+
<argument>${jmh.generator}</argument>
120+
</arguments>
121+
</configuration>
122+
</execution>
123+
</executions>
124+
<dependencies>
125+
<dependency>
126+
<groupId>org.openjdk.jmh</groupId>
127+
<artifactId>jmh-generator-bytecode</artifactId>
128+
<version>${jmh.version}</version>
129+
</dependency>
130+
</dependencies>
131+
</plugin>
132+
133+
<!--
134+
3. Add JMH generated code to the compile session.
135+
-->
136+
137+
<plugin>
138+
<groupId>org.codehaus.mojo</groupId>
139+
<artifactId>build-helper-maven-plugin</artifactId>
140+
<version>1.8</version>
141+
<executions>
142+
<execution>
143+
<id>add-source</id>
144+
<phase>process-sources</phase>
145+
<goals>
146+
<goal>add-source</goal>
147+
</goals>
148+
<configuration>
149+
<sources>
150+
<source>${project.basedir}/target/generated-sources/jmh</source>
151+
</sources>
152+
</configuration>
153+
</execution>
154+
</executions>
155+
</plugin>
156+
157+
<!--
158+
4. Compile JMH generated code.
159+
-->
160+
161+
<plugin>
162+
<groupId>org.apache.maven.plugins</groupId>
163+
<artifactId>maven-compiler-plugin</artifactId>
164+
<version>3.1</version>
165+
<configuration>
166+
<compilerVersion>${javac.target}</compilerVersion>
167+
<source>${javac.target}</source>
168+
<target>${javac.target}</target>
169+
<compilerArgument>-proc:none</compilerArgument>
170+
</configuration>
171+
</plugin>
172+
173+
<!--
174+
5. Package all the dependencies into the JAR
175+
-->
176+
177+
<plugin>
178+
<groupId>org.apache.maven.plugins</groupId>
179+
<artifactId>maven-shade-plugin</artifactId>
180+
<version>2.2</version>
181+
<executions>
182+
<execution>
183+
<phase>package</phase>
184+
<goals>
185+
<goal>shade</goal>
186+
</goals>
187+
<configuration>
188+
<finalName>${uberjar.name}</finalName>
189+
<transformers>
190+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
191+
<mainClass>org.openjdk.jmh.Main</mainClass>
192+
</transformer>
193+
</transformers>
194+
<filters>
195+
<filter>
196+
<!--
197+
Shading signed JARs will fail without this.
198+
http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar
199+
-->
200+
<artifact>*:*</artifact>
201+
<excludes>
202+
<exclude>META-INF/*.SF</exclude>
203+
<exclude>META-INF/*.DSA</exclude>
204+
<exclude>META-INF/*.RSA</exclude>
205+
</excludes>
206+
</filter>
207+
</filters>
208+
</configuration>
209+
</execution>
210+
</executions>
211+
</plugin>
212+
</plugins>
213+
</build>
214+
</project>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
package benchmarks
33+
34+
import org.openjdk.jmh.annotations.*
35+
import java.io.OutputStream
36+
import java.io.PrintStream
37+
import java.util.concurrent.TimeUnit
38+
39+
/*
40+
41+
Benchmark Mode Cnt Score Error Units
42+
GuideSyncBenchmark.sync01Problem avgt 15 11971.221 ± 1739.891 us/op
43+
GuideSyncBenchmark.sync02Volatile avgt 15 14936.828 ± 142.586 us/op
44+
GuideSyncBenchmark.sync03AtomicInt avgt 15 15505.607 ± 1434.846 us/op
45+
GuideSyncBenchmark.sync04ConfineFine avgt 15 1331453.593 ± 89298.871 us/op
46+
GuideSyncBenchmark.sync05ConfineCoarse avgt 15 2253.270 ± 425.033 us/op
47+
GuideSyncBenchmark.sync06Mutex avgt 15 1075086.511 ± 140589.883 us/op
48+
GuideSyncBenchmark.sync07Actor avgt 15 1075603.512 ± 203901.350 us/op
49+
50+
*/
51+
52+
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
53+
@Measurement(iterations = 5, time = 5, timeUnit = TimeUnit.SECONDS)
54+
@Fork(value = 3)
55+
@BenchmarkMode(Mode.AverageTime)
56+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
57+
@State(Scope.Benchmark)
58+
open class GuideSyncBenchmark {
59+
val oldOut = System.out
60+
61+
@Setup
62+
fun setup() {
63+
System.setOut(PrintStream(object : OutputStream() {
64+
override fun write(b: Int) {} // empty
65+
}))
66+
}
67+
68+
@TearDown
69+
fun tearDonw() {
70+
System.setOut(oldOut)
71+
}
72+
73+
@Benchmark
74+
fun sync01Problem() {
75+
guide.sync.example01.main(emptyArray())
76+
}
77+
78+
@Benchmark
79+
fun sync02Volatile() {
80+
guide.sync.example02.main(emptyArray())
81+
}
82+
83+
@Benchmark
84+
fun sync03AtomicInt() {
85+
guide.sync.example03.main(emptyArray())
86+
}
87+
88+
@Benchmark
89+
fun sync04ConfineFine() {
90+
guide.sync.example04.main(emptyArray())
91+
}
92+
93+
@Benchmark
94+
fun sync05ConfineCoarse() {
95+
guide.sync.example05.main(emptyArray())
96+
}
97+
98+
@Benchmark
99+
fun sync06Mutex() {
100+
guide.sync.example06.main(emptyArray())
101+
}
102+
103+
@Benchmark
104+
fun sync07Actor() {
105+
guide.sync.example07.main(emptyArray())
106+
}
107+
}

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
<module>ui/kotlinx-coroutines-javafx</module>
108108
<module>ui/kotlinx-coroutines-android</module>
109109
<module>site</module>
110+
<module>benchmarks</module>
110111
</modules>
111112

112113
<dependencies>
@@ -236,4 +237,4 @@
236237
</plugins>
237238
</pluginManagement>
238239
</build>
239-
</project>
240+
</project>

0 commit comments

Comments
 (0)