Exercise: Coroutine time measurement
In order to measure a block execution time, you defined the following function, that can measure real time on production, and virtual time in unit tests:
suspend fun measureCoroutine(
body: suspend () -> Unit
): Duration {
val dispatcher = coroutineContext[ContinuationInterceptor]
return if (dispatcher is TestDispatcher) {
val before = dispatcher.scheduler.currentTime
body()
val after = dispatcher.scheduler.currentTime
after - before
} else {
measureTimeMillis {
body()
}
}.milliseconds
}
However, you found that it is not very convenient to use because it lacks a contract. Define a contract to make the following code compile:
runTest {
val result: String
val duration = measureCoroutine {
delay(1000)
result = "OK"
}
println(duration) // 1000 ms
println(result) // OK
}
runBlocking {
val result: String
val duration = measureCoroutine {
delay(1000)
result = "OK"
}
println(duration) // 1000 ms
println(result) // OK
}
This problem can either be solved in the below playground or you can clone kotlin-exercises project and solve it locally. In the project, you can find code template for this exercise in advanced/contract/MesureCoroutineTime.kt. You can find there starting code and example usage.
Once you are done with the exercise, you can check your solution here.
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.TestDispatcher
import kotlinx.coroutines.test.runTest
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
import kotlin.coroutines.ContinuationInterceptor
import kotlin.coroutines.coroutineContext
import kotlin.system.measureTimeMillis
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
suspend fun measureCoroutine(
body: suspend () -> Unit
): Duration {
val dispatcher = coroutineContext[ContinuationInterceptor]
return if (dispatcher is TestDispatcher) {
val before = dispatcher.scheduler.currentTime
body()
val after = dispatcher.scheduler.currentTime
after - before
} else {
measureTimeMillis {
body()
}
}.milliseconds
}
suspend fun main() {
runTest {
val result: String
val duration = measureCoroutine {
delay(1000)
result = "OK"
}
println(duration) // 1000 ms
println(result) // OK
}
runBlocking {
val result: String
val duration = measureCoroutine {
delay(1000)
result = "OK"
}
println(duration) // 1000 ms
println(result) // OK
}
}
Marcin Moskala is a highly experienced developer and Kotlin instructor as the founder of Kt. Academy, an official JetBrains partner specializing in Kotlin training, Google Developers Expert, known for his significant contributions to the Kotlin community. Moskala is the author of several widely recognized books, including "Effective Kotlin," "Kotlin Coroutines," "Functional Kotlin," "Advanced Kotlin," "Kotlin Essentials," and "Android Development with Kotlin."
Beyond his literary achievements, Moskala is the author of the largest Medium publication dedicated to Kotlin. As a respected speaker, he has been invited to share his insights at numerous programming conferences, including events such as Droidcon and the prestigious Kotlin Conf, the premier conference dedicated to the Kotlin programming language.