Skip to content

Add arrow Either to benchmark #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benchmarks/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ kotlin {
implementation(project(":kotlin-result-coroutines"))
implementation(libs.kotlin.benchmark.runtime)
implementation(libs.kotlin.coroutines.core)
implementation(libs.arrow)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.github.michaelbull.result

import arrow.core.raise.either
import arrow.core.Either
import arrow.core.flatMap
import arrow.core.left
import arrow.core.right
import kotlinx.benchmark.Benchmark
import kotlinx.benchmark.BenchmarkMode
import kotlinx.benchmark.BenchmarkTimeUnit
Expand Down Expand Up @@ -36,6 +41,28 @@ class BindingBenchmark {
blackhole.consume(result)
}

@Benchmark
fun arrowBindingSuccess(blackhole: Blackhole) {
val result: Either<Error, Int> = either {
val x = arrowProvideX().bind()
val y = arrowProvideY().bind()
x + y
}

blackhole.consume(result)
}

@Benchmark
fun arrowBindingFailure(blackhole: Blackhole) {
val result: Either<Error, Int> = either {
val x = arrowProvideX().bind()
val z = arrowProvideZ().bind()
x + z
}

blackhole.consume(result)
}

@Benchmark
fun andThenSuccess(blackhole: Blackhole) {
val result = provideX().andThen { x ->
Expand All @@ -58,9 +85,35 @@ class BindingBenchmark {
blackhole.consume(result)
}

@Benchmark
fun arrowFlatMapSuccess(blackhole: Blackhole) {
val result = arrowProvideX().flatMap { x ->
arrowProvideY().flatMap { y ->
(x + y).right()
}
}

blackhole.consume(result)
}

@Benchmark
fun arrowFlatMapFailure(blackhole: Blackhole) {
val result = arrowProvideX().flatMap { x ->
arrowProvideZ().flatMap { z ->
(x + z).right()
}
}

blackhole.consume(result)
}

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)

private fun arrowProvideX(): Either<Error, Int> = 1.right()
private fun arrowProvideY(): Either<Error, Int> = 2.right()
private fun arrowProvideZ(): Either<Error, Int> = Error.left()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.github.michaelbull.result

import arrow.core.Either
import arrow.core.raise.either
import arrow.core.right
import com.github.michaelbull.result.coroutines.coroutineBinding
import kotlinx.benchmark.Benchmark
import kotlinx.benchmark.BenchmarkMode
Expand All @@ -10,6 +13,7 @@ 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

Expand Down Expand Up @@ -37,6 +41,25 @@ class CoroutineBindingBenchmark {
}
}

@Benchmark
fun arrowNonSuspendableBinding(blackhole: Blackhole) {
blackhole.consume(nonSuspend().get())
}

@Benchmark
fun arrowSuspendableBinding(blackhole: Blackhole) {
runBlocking {
blackhole.consume(withSuspend().get())
}
}

@Benchmark
fun arrowAsyncSuspendableBinding(blackhole: Blackhole) {
runBlocking {
blackhole.consume(withAsyncSuspend().get())
}
}

private object Error

private val time = 100L
Expand Down Expand Up @@ -78,4 +101,44 @@ class CoroutineBindingBenchmark {
delay(time)
return Ok(2)
}

private fun arrowNonSuspend(): Either<Error, Int> = either {
val x = arrowProvideXBlocking().bind()
val y = arrowProvideYBlocking().bind()
x + y
}

private suspend fun arrowWithSuspend(): Either<Error, Int> = either {
val x = arrowProvideX().bind()
val y = arrowProvideY().bind()
x + y
}

private suspend fun arrowWithAsyncSuspend(): Either<Error, Int> = either {
coroutineScope {
val x = async { arrowProvideX().bind() }
val y = async { arrowProvideY().bind() }
x.await() + y.await()
}
}

private fun arrowProvideXBlocking(): Either<Error, Int> {
Thread.sleep(time)
return 1.right()
}

private fun arrowProvideYBlocking(): Either<Error, Int> {
Thread.sleep(time)
return 2.right()
}

private suspend fun arrowProvideX(): Either<Error, Int> {
delay(time)
return 1.right()
}

private suspend fun arrowProvideY(): Either<Error, Int> {
delay(time)
return 2.right()
}
}
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ ktor-serialization-jackson = { module = "io.ktor:ktor-serialization-jackson", ve
ktor-server-content-negotiation = { module = "io.ktor:ktor-server-content-negotiation", version.ref = "ktor" }
ktor-server-netty = { module = "io.ktor:ktor-server-netty", version.ref = "ktor" }

arrow = { module = "io.arrow-kt:arrow-core", version = "2.1.2" }

[plugins]
kotlin-allopen = { id = "org.jetbrains.kotlin.plugin.allopen", version.ref = "kotlin" }
kotlin-benchmark = { id = "org.jetbrains.kotlinx.benchmark", version.ref = "kotlin-benchmark" }
Expand Down