-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathParametrizedDispatcherBase.kt
47 lines (40 loc) · 1.5 KB
/
ParametrizedDispatcherBase.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package benchmarks
import benchmarks.akka.CORES_COUNT
import kotlinx.coroutines.*
import kotlinx.coroutines.scheduling.*
import org.openjdk.jmh.annotations.Param
import org.openjdk.jmh.annotations.Setup
import org.openjdk.jmh.annotations.TearDown
import java.io.Closeable
import java.util.concurrent.*
import kotlin.coroutines.CoroutineContext
/**
* Base class to use different [CoroutineContext] in benchmarks via [Param] in inheritors.
* Currently allowed values are "fjp" for [CommonPool] and ftp_n for [ThreadPoolDispatcher] with n threads.
*/
abstract class ParametrizedDispatcherBase : CoroutineScope {
abstract var dispatcher: String
override lateinit var coroutineContext: CoroutineContext
private var closeable: Closeable? = null
@Setup
@UseExperimental(InternalCoroutinesApi::class)
open fun setup() {
coroutineContext = when {
dispatcher == "fjp" -> ForkJoinPool.commonPool().asCoroutineDispatcher()
dispatcher == "scheduler" -> {
ExperimentalCoroutineDispatcher(CORES_COUNT).also { closeable = it }
}
dispatcher.startsWith("ftp") -> {
newFixedThreadPoolContext(dispatcher.substring(4).toInt(), dispatcher).also { closeable = it }
}
else -> error("Unexpected dispatcher: $dispatcher")
}
}
@TearDown
fun tearDown() {
closeable?.close()
}
}