From 0910e9ffe477a1daafbe1dd3fede452f2ae60bca Mon Sep 17 00:00:00 2001 From: Tristan Hamilton Date: Mon, 11 May 2020 10:41:00 +0100 Subject: [PATCH] Add benchmarking framework --- build.gradle.kts | 25 ++++++++++ settings.gradle.kts | 7 +++ .../michaelbull/result/AndThenBenchmark.kt | 48 +++++++++++++++++++ .../michaelbull/result/BindingBenchmark.kt | 46 ++++++++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 src/benchmark/kotlin/com/github/michaelbull/result/AndThenBenchmark.kt create mode 100644 src/benchmark/kotlin/com/github/michaelbull/result/BindingBenchmark.kt diff --git a/build.gradle.kts b/build.gradle.kts index 6f72fce..ecad742 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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 { @@ -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 { diff --git a/settings.gradle.kts b/settings.gradle.kts index 1049800..a123210 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,3 +1,10 @@ rootProject.name = "kotlin-result" include("example") + +pluginManagement { + repositories { + maven("https://dl.bintray.com/kotlin/kotlinx" ) + gradlePluginPortal() + } +} diff --git a/src/benchmark/kotlin/com/github/michaelbull/result/AndThenBenchmark.kt b/src/benchmark/kotlin/com/github/michaelbull/result/AndThenBenchmark.kt new file mode 100644 index 0000000..a5be611 --- /dev/null +++ b/src/benchmark/kotlin/com/github/michaelbull/result/AndThenBenchmark.kt @@ -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 = Ok(1) + private fun provideY(): Result = Ok(2) + private fun provideZ(): Result = Err(Error) + } +} diff --git a/src/benchmark/kotlin/com/github/michaelbull/result/BindingBenchmark.kt b/src/benchmark/kotlin/com/github/michaelbull/result/BindingBenchmark.kt new file mode 100644 index 0000000..36da1c6 --- /dev/null +++ b/src/benchmark/kotlin/com/github/michaelbull/result/BindingBenchmark.kt @@ -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 { + val x = provideX().bind() + val y = provideY().bind() + x + y + } + + blackhole.consume(result) + } + + @Benchmark + fun failure(blackhole: Blackhole) { + val result = binding { + val x = provideX().bind() + val z = provideZ().bind() + x + z + } + + blackhole.consume(result) + } + + @State(Scope.Thread) + private companion object { + private fun provideX(): Result = Ok(1) + private fun provideY(): Result = Ok(2) + private fun provideZ(): Result = Err(Error) + } +}