Skip to content

Commit e03221a

Browse files
committed
Benchmark for measuring scheduler overhead
1 parent 98b04d4 commit e03221a

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package benchmarks
2+
3+
import kotlinx.coroutines.experimental.launch
4+
import kotlinx.coroutines.experimental.scheduling.ForkedMarker
5+
import org.openjdk.jmh.annotations.*
6+
import java.util.concurrent.CyclicBarrier
7+
import java.util.concurrent.TimeUnit
8+
9+
/*
10+
* Benchmark to measure scheduling overhead in comparison with FJP.
11+
* LaunchBenchmark.massiveLaunch experimental avgt 30 187.342 ± 20.244 us/op
12+
* LaunchBenchmark.massiveLaunch fjp avgt 30 179.762 ± 3.931 us/op
13+
*/
14+
@Warmup(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
15+
@Measurement(iterations = 100000, time = 1, timeUnit = TimeUnit.SECONDS)
16+
@Fork(value = 3, jvmArgsAppend = ["-XX:+PreserveFramePointer"])
17+
@BenchmarkMode(Mode.AverageTime)
18+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
19+
@State(Scope.Benchmark)
20+
open class LaunchBenchmark : ParametrizedDispatcherBase() {
21+
22+
@Param("experimental")
23+
override var dispatcher: String = "fjp"
24+
25+
private val jobsToLaunch = 100
26+
private val submitters = 4
27+
28+
private val allLaunched = CyclicBarrier(submitters)
29+
private val stopBarrier = CyclicBarrier(submitters + 1)
30+
31+
@Benchmark
32+
fun massiveLaunch() {
33+
repeat(submitters) {
34+
launch(benchmarkContext + ForkedMarker) {
35+
// Wait until all cores are occupied
36+
allLaunched.await()
37+
allLaunched.reset()
38+
39+
(1..jobsToLaunch).map {
40+
launch(coroutineContext) {
41+
// do nothing
42+
}
43+
}.map { it.join() }
44+
45+
stopBarrier.await()
46+
}
47+
}
48+
49+
stopBarrier.await()
50+
stopBarrier.reset()
51+
}
52+
53+
}

core/kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/scheduling/CoroutineSchedulerStressTest.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ class CoroutineSchedulerStressTest : TestBase() {
3737

3838
for (i in 1..tasksNum) {
3939
dispatcher.dispatch(ctx, Runnable {
40-
val numbers = observedThreads.computeIfAbsent(Thread.currentThread(), { _ -> hashSetOf() })
40+
var numbers = observedThreads[Thread.currentThread()]
41+
if (numbers == null) {
42+
numbers = hashSetOf()
43+
observedThreads[Thread.currentThread()] = numbers
44+
}
45+
4146
require(numbers.add(i))
4247
if (processed.incrementAndGet() == tasksNum) {
4348
finishLatch.countDown()

0 commit comments

Comments
 (0)