Add benchmarking framework

This commit is contained in:
Tristan Hamilton 2020-05-11 10:41:00 +01:00 committed by Michael Bull
parent fa8f58c0da
commit 0910e9ffe4
4 changed files with 126 additions and 0 deletions

View File

@ -14,12 +14,15 @@ plugins {
id("org.jetbrains.dokka") version "0.10.1"
id("com.github.ben-manes.versions") version "0.28.0"
id("net.researchgate.release") version "2.8.1"
id("kotlinx.benchmark") version "0.2.0-dev-7"
id("org.jetbrains.kotlin.plugin.allopen") version "1.3.70"
}
allprojects {
repositories {
mavenCentral()
jcenter()
maven("https://dl.bintray.com/kotlin/kotlinx")
}
tasks.withType<KotlinCompile> {
@ -30,6 +33,25 @@ allprojects {
}
}
allOpen {
annotation("org.openjdk.jmh.annotations.State")
annotation("org.openjdk.jmh.annotations.BenchmarkMode")
}
sourceSets.create("benchmark") {
java {
srcDir("src/benchmarks/kotlin")
}
}
val benchmarkImplementation by configurations
benchmark {
targets {
register("benchmark")
}
}
dependencies {
implementation(kotlin("stdlib"))
testImplementation("junit:junit:4.13")
@ -37,6 +59,9 @@ dependencies {
testImplementation(kotlin("test-annotations-common"))
testImplementation(kotlin("test-junit"))
testImplementation(kotlin("test"))
benchmarkImplementation(sourceSets.main.get().output + sourceSets.main.get().runtimeClasspath)
benchmarkImplementation("org.jetbrains.kotlinx:kotlinx.benchmark.runtime-jvm:0.2.0-dev-7")
}
tasks.withType<DependencyUpdatesTask> {

View File

@ -1,3 +1,10 @@
rootProject.name = "kotlin-result"
include("example")
pluginManagement {
repositories {
maven("https://dl.bintray.com/kotlin/kotlinx" )
gradlePluginPortal()
}
}

View File

@ -0,0 +1,48 @@
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

@ -0,0 +1,46 @@
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 BindingBenchmark {
private object Error
@Benchmark
fun success(blackhole: Blackhole) {
val result = binding<Int, Error> {
val x = provideX().bind()
val y = provideY().bind()
x + y
}
blackhole.consume(result)
}
@Benchmark
fun failure(blackhole: Blackhole) {
val result = binding<Int, Error> {
val x = provideX().bind()
val z = provideZ().bind()
x + z
}
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)
}
}