Add SuspendBindingBenchmark

Closes #27
This commit is contained in:
Michael Bull 2021-02-10 23:35:44 +00:00
parent b2505667d1
commit e1fd6387e5
3 changed files with 99 additions and 11 deletions

View File

@ -36,7 +36,9 @@ kotlin {
val commonMain by getting {
dependencies {
implementation(project(":kotlin-result"))
implementation(project(":kotlin-result-coroutines"))
implementation("org.jetbrains.kotlinx:kotlinx.benchmark.runtime:${Versions.kotlinBenchmark}")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.kotlinCoroutines}")
}
}
}

View File

@ -1,21 +1,19 @@
package com.github.michaelbull.result
import kotlinx.benchmark.Benchmark
import kotlinx.benchmark.BenchmarkMode
import kotlinx.benchmark.BenchmarkTimeUnit
import kotlinx.benchmark.Blackhole
import kotlinx.benchmark.Mode
import kotlinx.benchmark.OutputTimeUnit
import kotlinx.benchmark.BenchmarkTimeUnit
import kotlinx.benchmark.Benchmark
import kotlinx.benchmark.Blackhole
import kotlinx.benchmark.State
import kotlinx.benchmark.Scope
import kotlinx.benchmark.State
@State(Scope.Benchmark)
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(BenchmarkTimeUnit.MILLISECONDS)
class BindingBenchmark {
private object Error
@Benchmark
fun bindingSuccess(blackhole: Blackhole) {
val result = binding<Int, Error> {
@ -60,9 +58,9 @@ class BindingBenchmark {
blackhole.consume(result)
}
private companion object {
private object Error
private fun provideX(): Result<Int, Error> = Ok(1)
private fun provideY(): Result<Int, Error> = Ok(2)
private fun provideZ(): Result<Int, Error> = Err(Error)
}
}

View File

@ -0,0 +1,88 @@
package com.github.michaelbull.result
import kotlinx.benchmark.Benchmark
import kotlinx.benchmark.BenchmarkMode
import kotlinx.benchmark.BenchmarkTimeUnit
import kotlinx.benchmark.Blackhole
import kotlinx.benchmark.Mode
import kotlinx.benchmark.OutputTimeUnit
import kotlinx.benchmark.Scope
import kotlinx.benchmark.State
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import com.github.michaelbull.result.coroutines.binding.binding as coroutineBinding
@State(Scope.Benchmark)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(BenchmarkTimeUnit.MILLISECONDS)
class SuspendBindingBenchmark {
@Benchmark
fun nonSuspendableBinding(blackhole: Blackhole) {
blackhole.consume(nonSuspend().get())
}
@Benchmark
fun suspendableBinding(blackhole: Blackhole) {
runBlocking {
blackhole.consume(withSuspend().get())
}
}
@Benchmark
fun asyncSuspendableBinding(blackhole: Blackhole) {
runBlocking {
blackhole.consume(withAsyncSuspend().get())
}
}
private object Error
private val time = 100L
private fun nonSuspend() = binding<Int, Error> {
val x = provideXBlocking().bind()
val y = provideYBlocking().bind()
x + y
}
private suspend fun withSuspend(): Result<Int, Error> {
return coroutineBinding {
val x = provideX().bind()
val y = provideY().bind()
x + y
}
}
private suspend fun withAsyncSuspend(): Result<Int, Error> {
return coroutineScope {
coroutineBinding {
val x = async { provideX().bind() }
val y = async { provideY().bind() }
x.await() + y.await()
}
}
}
private fun provideXBlocking(): Result<Int, Error> {
Thread.sleep(time)
return Ok(1)
}
private fun provideYBlocking(): Result<Int, Error> {
Thread.sleep(time)
return Ok(2)
}
private suspend fun provideX(): Result<Int, Error> {
delay(time)
return Ok(1)
}
private suspend fun provideY(): Result<Int, Error> {
delay(time)
return Ok(2)
}
}