Skip to content

Commit 8262249

Browse files
alphahomichaelbull
authored andcommitted
Add arrow-kt Either to benchmarks
Closes #129
1 parent b863c42 commit 8262249

File tree

4 files changed

+123
-3
lines changed

4 files changed

+123
-3
lines changed

benchmarks/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ kotlin {
2323
implementation(project(":kotlin-result-coroutines"))
2424
implementation(libs.kotlin.benchmark.runtime)
2525
implementation(libs.kotlin.coroutines.core)
26+
implementation(libs.arrow)
2627
}
2728
}
2829
}

benchmarks/src/commonMain/kotlin/com/github/michaelbull/result/BindingBenchmark.kt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.github.michaelbull.result
22

3+
import arrow.core.raise.either
4+
import arrow.core.Either
5+
import arrow.core.flatMap
6+
import arrow.core.left
7+
import arrow.core.right
38
import kotlinx.benchmark.Benchmark
49
import kotlinx.benchmark.BenchmarkMode
510
import kotlinx.benchmark.BenchmarkTimeUnit
@@ -36,6 +41,28 @@ class BindingBenchmark {
3641
blackhole.consume(result)
3742
}
3843

44+
@Benchmark
45+
fun arrowBindingSuccess(blackhole: Blackhole) {
46+
val result: Either<Error, Int> = either {
47+
val x = arrowProvideX().bind()
48+
val y = arrowProvideY().bind()
49+
x + y
50+
}
51+
52+
blackhole.consume(result)
53+
}
54+
55+
@Benchmark
56+
fun arrowBindingFailure(blackhole: Blackhole) {
57+
val result: Either<Error, Int> = either {
58+
val x = arrowProvideX().bind()
59+
val z = arrowProvideZ().bind()
60+
x + z
61+
}
62+
63+
blackhole.consume(result)
64+
}
65+
3966
@Benchmark
4067
fun andThenSuccess(blackhole: Blackhole) {
4168
val result = provideX().andThen { x ->
@@ -58,9 +85,35 @@ class BindingBenchmark {
5885
blackhole.consume(result)
5986
}
6087

88+
@Benchmark
89+
fun arrowFlatMapSuccess(blackhole: Blackhole) {
90+
val result = arrowProvideX().flatMap { x ->
91+
arrowProvideY().flatMap { y ->
92+
(x + y).right()
93+
}
94+
}
95+
96+
blackhole.consume(result)
97+
}
98+
99+
@Benchmark
100+
fun arrowFlatMapFailure(blackhole: Blackhole) {
101+
val result = arrowProvideX().flatMap { x ->
102+
arrowProvideZ().flatMap { z ->
103+
(x + z).right()
104+
}
105+
}
106+
107+
blackhole.consume(result)
108+
}
109+
61110
private object Error
62111

63112
private fun provideX(): Result<Int, Error> = Ok(1)
64113
private fun provideY(): Result<Int, Error> = Ok(2)
65114
private fun provideZ(): Result<Int, Error> = Err(Error)
115+
116+
private fun arrowProvideX(): Either<Error, Int> = 1.right()
117+
private fun arrowProvideY(): Either<Error, Int> = 2.right()
118+
private fun arrowProvideZ(): Either<Error, Int> = Error.left()
66119
}

benchmarks/src/jvmMain/kotlin/com/github/michaelbull/result/CoroutineBindingBenchmark.kt

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.github.michaelbull.result
22

3+
import arrow.core.Either
4+
import arrow.core.raise.either
5+
import arrow.core.right
36
import com.github.michaelbull.result.coroutines.coroutineBinding
47
import kotlinx.benchmark.Benchmark
58
import kotlinx.benchmark.BenchmarkMode
@@ -10,6 +13,7 @@ import kotlinx.benchmark.OutputTimeUnit
1013
import kotlinx.benchmark.Scope
1114
import kotlinx.benchmark.State
1215
import kotlinx.coroutines.async
16+
import kotlinx.coroutines.coroutineScope
1317
import kotlinx.coroutines.delay
1418
import kotlinx.coroutines.runBlocking
1519

@@ -37,6 +41,25 @@ class CoroutineBindingBenchmark {
3741
}
3842
}
3943

44+
@Benchmark
45+
fun arrowNonSuspendableBinding(blackhole: Blackhole) {
46+
blackhole.consume(nonSuspend().get())
47+
}
48+
49+
@Benchmark
50+
fun arrowSuspendableBinding(blackhole: Blackhole) {
51+
runBlocking {
52+
blackhole.consume(withSuspend().get())
53+
}
54+
}
55+
56+
@Benchmark
57+
fun arrowAsyncSuspendableBinding(blackhole: Blackhole) {
58+
runBlocking {
59+
blackhole.consume(withAsyncSuspend().get())
60+
}
61+
}
62+
4063
private object Error
4164

4265
private val time = 100L
@@ -78,4 +101,44 @@ class CoroutineBindingBenchmark {
78101
delay(time)
79102
return Ok(2)
80103
}
104+
105+
private fun arrowNonSuspend(): Either<Error, Int> = either {
106+
val x = arrowProvideXBlocking().bind()
107+
val y = arrowProvideYBlocking().bind()
108+
x + y
109+
}
110+
111+
private suspend fun arrowWithSuspend(): Either<Error, Int> = either {
112+
val x = arrowProvideX().bind()
113+
val y = arrowProvideY().bind()
114+
x + y
115+
}
116+
117+
private suspend fun arrowWithAsyncSuspend(): Either<Error, Int> = either {
118+
coroutineScope {
119+
val x = async { arrowProvideX().bind() }
120+
val y = async { arrowProvideY().bind() }
121+
x.await() + y.await()
122+
}
123+
}
124+
125+
private fun arrowProvideXBlocking(): Either<Error, Int> {
126+
Thread.sleep(time)
127+
return 1.right()
128+
}
129+
130+
private fun arrowProvideYBlocking(): Either<Error, Int> {
131+
Thread.sleep(time)
132+
return 2.right()
133+
}
134+
135+
private suspend fun arrowProvideX(): Either<Error, Int> {
136+
delay(time)
137+
return 1.right()
138+
}
139+
140+
private suspend fun arrowProvideY(): Either<Error, Int> {
141+
delay(time)
142+
return 2.right()
143+
}
81144
}

gradle/libs.versions.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,24 @@ maven-publish-plugin = "0.34.0"
66

77
# benchmarks subproject
88
kotlin-benchmark = "0.4.14"
9+
arrow = "2.1.2"
910

1011
# example subproject
1112
ktor = "3.2.3"
1213
logback = "1.5.18"
1314

1415

1516
[libraries]
16-
kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
1717
kotlin-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlin-coroutines" }
1818
kotlin-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlin-coroutines" }
19+
20+
# buildSrc subproject
21+
kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
1922
maven-publish-plugin = { module = "com.vanniktech.maven.publish:com.vanniktech.maven.publish.gradle.plugin", version.ref = "maven-publish-plugin" }
2023

2124
# benchmarks subproject
2225
kotlin-benchmark-runtime = { module = "org.jetbrains.kotlinx:kotlinx-benchmark-runtime", version.ref = "kotlin-benchmark" }
26+
arrow = { module = "io.arrow-kt:arrow-core", version.ref = "arrow" }
2327

2428
# example subproject
2529
kotlin-stdlib-jdk8 = { module = "org.jetbrains.kotlin:kotlin-stdlib-jdk8", version.ref = "kotlin" }
@@ -29,10 +33,9 @@ ktor-server-content-negotiation = { module = "io.ktor:ktor-server-content-negoti
2933
ktor-server-netty = { module = "io.ktor:ktor-server-netty", version.ref = "ktor" }
3034
logback = { module = "ch.qos.logback:logback-classic", version.ref = "logback" }
3135

32-
3336
[plugins]
3437
versions = { id = "com.github.ben-manes.versions", version.ref = "versions-plugin" }
3538

36-
# benchmarks
39+
# benchmarks subproject
3740
kotlin-allopen = { id = "org.jetbrains.kotlin.plugin.allopen", version.ref = "kotlin" }
3841
kotlin-benchmark = { id = "org.jetbrains.kotlinx.benchmark", version.ref = "kotlin-benchmark" }

0 commit comments

Comments
 (0)