diff --git a/benchmarks/build.gradle.kts b/benchmarks/build.gradle.kts index 9119257..e534563 100644 --- a/benchmarks/build.gradle.kts +++ b/benchmarks/build.gradle.kts @@ -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}") } } } diff --git a/benchmarks/src/commonMain/kotlin/com/github/michaelbull/result/BindingBenchmark.kt b/benchmarks/src/commonMain/kotlin/com/github/michaelbull/result/BindingBenchmark.kt index 37b21ff..2e7aef2 100644 --- a/benchmarks/src/commonMain/kotlin/com/github/michaelbull/result/BindingBenchmark.kt +++ b/benchmarks/src/commonMain/kotlin/com/github/michaelbull/result/BindingBenchmark.kt @@ -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 { @@ -60,9 +58,9 @@ class BindingBenchmark { blackhole.consume(result) } - private companion object { - private fun provideX(): Result = Ok(1) - private fun provideY(): Result = Ok(2) - private fun provideZ(): Result = Err(Error) - } + private object Error + + private fun provideX(): Result = Ok(1) + private fun provideY(): Result = Ok(2) + private fun provideZ(): Result = Err(Error) } diff --git a/benchmarks/src/jvmMain/kotlin/com/github/michaelbull/result/SuspendBindingBenchmark.kt b/benchmarks/src/jvmMain/kotlin/com/github/michaelbull/result/SuspendBindingBenchmark.kt new file mode 100644 index 0000000..30cddd9 --- /dev/null +++ b/benchmarks/src/jvmMain/kotlin/com/github/michaelbull/result/SuspendBindingBenchmark.kt @@ -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 { + val x = provideXBlocking().bind() + val y = provideYBlocking().bind() + x + y + } + + private suspend fun withSuspend(): Result { + return coroutineBinding { + val x = provideX().bind() + val y = provideY().bind() + x + y + } + } + + private suspend fun withAsyncSuspend(): Result { + return coroutineScope { + coroutineBinding { + val x = async { provideX().bind() } + val y = async { provideY().bind() } + x.await() + y.await() + } + } + } + + private fun provideXBlocking(): Result { + Thread.sleep(time) + return Ok(1) + } + + private fun provideYBlocking(): Result { + Thread.sleep(time) + return Ok(2) + } + + private suspend fun provideX(): Result { + delay(time) + return Ok(1) + } + + private suspend fun provideY(): Result { + delay(time) + return Ok(2) + } +}