Merge {AndThen,Binding}Benchmarks

jmh-visualiser displays them comparatively when they are in the same class.
This commit is contained in:
Michael Bull 2020-05-11 11:54:50 +01:00
parent 0910e9ffe4
commit 4b745d8717
2 changed files with 26 additions and 50 deletions

View File

@ -1,48 +0,0 @@
package com.github.michaelbull.result
import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.BenchmarkMode
import org.openjdk.jmh.annotations.Mode
import org.openjdk.jmh.annotations.OutputTimeUnit
import org.openjdk.jmh.annotations.Scope
import org.openjdk.jmh.annotations.State
import org.openjdk.jmh.infra.Blackhole
import java.util.concurrent.TimeUnit
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
class AndThenBenchmark {
private object Error
@Benchmark
fun success(blackhole: Blackhole) {
val result =
provideX().andThen { first ->
provideY().andThen { second ->
Ok(first + second)
}
}
blackhole.consume(result)
}
@Benchmark
fun failure(blackhole: Blackhole) {
val result =
provideX().andThen { first ->
provideZ().andThen { second ->
Ok(first + second)
}
}
blackhole.consume(result)
}
@State(Scope.Thread)
private companion object {
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

@ -16,7 +16,7 @@ class BindingBenchmark {
private object Error
@Benchmark
fun success(blackhole: Blackhole) {
fun bindingSuccess(blackhole: Blackhole) {
val result = binding<Int, Error> {
val x = provideX().bind()
val y = provideY().bind()
@ -27,7 +27,7 @@ class BindingBenchmark {
}
@Benchmark
fun failure(blackhole: Blackhole) {
fun bindingFailure(blackhole: Blackhole) {
val result = binding<Int, Error> {
val x = provideX().bind()
val z = provideZ().bind()
@ -37,6 +37,30 @@ class BindingBenchmark {
blackhole.consume(result)
}
@Benchmark
fun andThenSuccess(blackhole: Blackhole) {
val result =
provideX().andThen { first ->
provideY().andThen { second ->
Ok(first + second)
}
}
blackhole.consume(result)
}
@Benchmark
fun andThenFailure(blackhole: Blackhole) {
val result =
provideX().andThen { first ->
provideZ().andThen { second ->
Ok(first + second)
}
}
blackhole.consume(result)
}
@State(Scope.Thread)
private companion object {
private fun provideX(): Result<Int, Error> = Ok(1)