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 benchmarks 6 7 import benchmarks.akka.CORES_COUNT 8 import kotlinx.coroutines.* 9 import kotlinx.coroutines.scheduling.* 10 import org.openjdk.jmh.annotations.Param 11 import org.openjdk.jmh.annotations.Setup 12 import org.openjdk.jmh.annotations.TearDown 13 import java.io.Closeable 14 import java.util.concurrent.* 15 import kotlin.coroutines.CoroutineContext 16 17 /** 18 * Base class to use different [CoroutineContext] in benchmarks via [Param] in inheritors. 19 * Currently allowed values are "fjp" for [CommonPool] and ftp_n for [ThreadPoolDispatcher] with n threads. 20 */ 21 abstract class ParametrizedDispatcherBase : CoroutineScope { 22 23 abstract var dispatcher: String 24 override lateinit var coroutineContext: CoroutineContext 25 private var closeable: Closeable? = null 26 27 @Setup setupnull28 open fun setup() { 29 coroutineContext = when { 30 dispatcher == "fjp" -> ForkJoinPool.commonPool().asCoroutineDispatcher() 31 dispatcher == "scheduler" -> { 32 Dispatchers.Default 33 } 34 dispatcher.startsWith("ftp") -> { 35 newFixedThreadPoolContext(dispatcher.substring(4).toInt(), dispatcher).also { closeable = it } 36 } 37 else -> error("Unexpected dispatcher: $dispatcher") 38 } 39 } 40 41 @TearDown tearDownnull42 fun tearDown() { 43 closeable?.close() 44 } 45 46 } 47