Skip to content

Commit 142f797

Browse files
authored
Introduce jvmBenchmarks into kotlinx-coroutines-core (#3946)
It allows us to benchmark `internal` API unavailable otherwise and allows us to backport K2 changes properly. Only benchmarks that somehow leverage `internal` API are moved in the new source-set
1 parent ff95ab7 commit 142f797

File tree

9 files changed

+61
-25
lines changed

9 files changed

+61
-25
lines changed

kotlinx-coroutines-core/build.gradle

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5+
plugins {
6+
id 'org.jetbrains.kotlinx.benchmark' version '0.4.9'
7+
}
8+
59
apply plugin: 'org.jetbrains.kotlin.multiplatform'
610
apply plugin: 'org.jetbrains.dokka'
711

@@ -167,6 +171,13 @@ kotlin {
167171

168172
// For animal sniffer
169173
withJava()
174+
compilations.create('benchmark') { associateWith(compilations.main) }
175+
}
176+
}
177+
178+
benchmark {
179+
targets {
180+
register("jvmBenchmark")
170181
}
171182
}
172183

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## kotlinx-coroutines-core benchmarks
2+
3+
This source-set contains benchmarks that leverage `internal` API (e.g. `suspendCancellableCoroutineReusable`)
4+
and thus cannot be written in `benchmarks` module.
5+
6+
This is an interim solution unless we introduce clear separation of responsibilities in benchmark modules
7+
and decide on their usability.
8+
9+
10+
### Usage
11+
12+
```
13+
./gradlew :kotlinx-coroutines-core:jvmBenchmarkBenchmarkJar
14+
java -jar kotlinx-coroutines-core/build/benchmarks/jvmBenchmark/jars/kotlinx-coroutines-core-jvmBenchmark-jmh-1.7.2-SNAPSHOT-JMH.jar
15+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.coroutines
6+
7+
import java.util.concurrent.*
8+
9+
public fun doGeomDistrWork(work: Int) {
10+
// We use geometric distribution here. We also checked on macbook pro 13" (2017) that the resulting work times
11+
// are distributed geometrically, see https://github.com/Kotlin/kotlinx.coroutines/pull/1464#discussion_r355705325
12+
val p = 1.0 / work
13+
val r = ThreadLocalRandom.current()
14+
while (true) {
15+
if (r.nextDouble() < p) break
16+
}
17+
}

benchmarks/src/jmh/kotlin/benchmarks/SemaphoreBenchmark.kt renamed to kotlinx-coroutines-core/jvmBenchmark/kotlin/kotlinx/coroutines/SemaphoreBenchmark.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
/*
2-
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
2+
* Copyright 2016-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5-
package benchmarks
5+
package kotlinx.coroutines
66

7-
import benchmarks.common.*
87
import kotlinx.coroutines.*
98
import kotlinx.coroutines.channels.*
109
import kotlinx.coroutines.scheduling.*
@@ -80,8 +79,7 @@ open class SemaphoreBenchmark {
8079
}
8180

8281
enum class SemaphoreBenchDispatcherCreator(val create: (parallelism: Int) -> CoroutineDispatcher) {
83-
// FORK_JOIN({ parallelism -> ForkJoinPool(parallelism).asCoroutineDispatcher() }),
84-
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
82+
FORK_JOIN({ parallelism -> ForkJoinPool(parallelism).asCoroutineDispatcher() }),
8583
DEFAULT({ parallelism -> ExperimentalCoroutineDispatcher(corePoolSize = parallelism, maxPoolSize = parallelism) })
8684
}
8785

benchmarks/src/jmh/kotlin/benchmarks/ChannelProducerConsumerBenchmark.kt renamed to kotlinx-coroutines-core/jvmBenchmark/kotlin/kotlinx/coroutines/channels/ChannelProducerConsumerBenchmark.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
/*
2-
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
2+
* Copyright 2016-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5-
package benchmarks
5+
package kotlinx.coroutines.channels
66

7-
import benchmarks.common.*
87
import kotlinx.coroutines.*
98
import kotlinx.coroutines.channels.Channel
109
import kotlinx.coroutines.scheduling.*
1110
import kotlinx.coroutines.selects.select
1211
import org.openjdk.jmh.annotations.*
1312
import java.lang.Integer.max
13+
import java.util.concurrent.ForkJoinPool
1414
import java.util.concurrent.Phaser
1515
import java.util.concurrent.TimeUnit
1616

@@ -136,8 +136,7 @@ open class ChannelProducerConsumerBenchmark {
136136
}
137137

138138
enum class DispatcherCreator(val create: (parallelism: Int) -> CoroutineDispatcher) {
139-
//FORK_JOIN({ parallelism -> ForkJoinPool(parallelism).asCoroutineDispatcher() }),
140-
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
139+
FORK_JOIN({ parallelism -> ForkJoinPool(parallelism).asCoroutineDispatcher() }),
141140
DEFAULT({ parallelism -> ExperimentalCoroutineDispatcher(corePoolSize = parallelism, maxPoolSize = parallelism) })
142141
}
143142

benchmarks/src/jmh/kotlin/benchmarks/tailcall/SelectBenchmark.kt renamed to kotlinx-coroutines-core/jvmBenchmark/kotlin/kotlinx/coroutines/channels/SelectBenchmark.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2-
* Copyright 2016-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
2+
* Copyright 2016-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5-
package benchmarks.tailcall
5+
package kotlinx.coroutines.channels
66

77
import kotlinx.coroutines.*
88
import kotlinx.coroutines.channels.*

benchmarks/src/jmh/kotlin/benchmarks/tailcall/SimpleChannel.kt renamed to kotlinx-coroutines-core/jvmBenchmark/kotlin/kotlinx/coroutines/channels/SimpleChannel.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
/*
2-
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
2+
* Copyright 2016-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5-
package benchmarks.tailcall
5+
package kotlinx.coroutines.channels
66

77
import kotlinx.coroutines.*
88
import kotlin.coroutines.*
99
import kotlin.coroutines.intrinsics.*
1010

11-
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
1211
public abstract class SimpleChannel {
1312
companion object {
1413
const val NULL_SURROGATE: Int = -1
@@ -83,13 +82,11 @@ class CancellableChannel : SimpleChannel() {
8382
}
8483

8584
class CancellableReusableChannel : SimpleChannel() {
86-
@Suppress("INVISIBLE_MEMBER")
8785
override suspend fun suspendReceive(): Int = suspendCancellableCoroutineReusable {
8886
consumer = it.intercepted()
8987
COROUTINE_SUSPENDED
9088
}
9189

92-
@Suppress("INVISIBLE_MEMBER")
9390
override suspend fun suspendSend(element: Int) = suspendCancellableCoroutineReusable<Unit> {
9491
enqueuedValue = element
9592
producer = it.intercepted()

benchmarks/src/jmh/kotlin/benchmarks/tailcall/SimpleChannelBenchmark.kt renamed to kotlinx-coroutines-core/jvmBenchmark/kotlin/kotlinx/coroutines/channels/SimpleChannelBenchmark.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2-
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
2+
* Copyright 2016-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5-
package benchmarks.tailcall
5+
package kotlinx.coroutines.channels
66

77
import kotlinx.coroutines.*
88
import org.openjdk.jmh.annotations.*

benchmarks/src/jmh/kotlin/benchmarks/flow/TakeWhileBenchmark.kt renamed to kotlinx-coroutines-core/jvmBenchmark/kotlin/kotlinx/coroutines/flow/TakeWhileBenchmark.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
/*
2-
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
2+
* Copyright 2016-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5-
@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
6-
7-
package benchmarks.flow
5+
package kotlinx.coroutines.flow
86

97
import kotlinx.coroutines.*
10-
import kotlinx.coroutines.flow.*
118
import kotlinx.coroutines.flow.internal.*
9+
import kotlinx.coroutines.flow.internal.AbortFlowException
10+
import kotlinx.coroutines.flow.internal.unsafeFlow
1211
import org.openjdk.jmh.annotations.*
13-
import java.util.concurrent.TimeUnit
12+
import java.util.concurrent.*
1413

1514
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
1615
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)

0 commit comments

Comments
 (0)