Skip to content

Commit

Permalink
feat(perf): add a benchmark for Fuzzy#fuzzyMatchSimple
Browse files Browse the repository at this point in the history
  • Loading branch information
msfjarvis committed Jul 24, 2022
1 parent bd669a7 commit 323feb5
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
43 changes: 43 additions & 0 deletions benchmark/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTargetWithHostTests

plugins {
kotlin("multiplatform")
id("org.jetbrains.kotlinx.benchmark")
id("org.jetbrains.kotlin.plugin.allopen")
}

fun KotlinNativeTargetWithHostTests.configureTarget() = binaries {
executable { entryPoint = "main" }
}

allOpen { annotation("org.openjdk.jmh.annotations.State") }

benchmark { targets { register("jvm") } }

kotlin {
jvm()
js(LEGACY) {
browser()
nodejs()
binaries.executable()
compilations.all {
kotlinOptions {
moduleKind = "umd"
sourceMap = true
sourceMapEmbedSources = null
}
}
}
linuxX64 { configureTarget() }
mingwX64 { configureTarget() }
macosX64 { configureTarget() }

sourceSets {
sourceSets["commonMain"].apply {
dependencies {
implementation(project(":"))
implementation("org.jetbrains.kotlinx:kotlinx-benchmark-runtime:0.4.4")
}
}
}
}
38 changes: 38 additions & 0 deletions benchmark/src/commonMain/kotlin/benchmark/FuzzyMatchBenchmark.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package benchmark

import com.github.androidpasswordstore.sublimefuzzy.Fuzzy
import kotlin.random.Random
import kotlinx.benchmark.Benchmark
import kotlinx.benchmark.BenchmarkMode
import kotlinx.benchmark.BenchmarkTimeUnit
import kotlinx.benchmark.Measurement
import kotlinx.benchmark.Mode
import kotlinx.benchmark.OutputTimeUnit
import kotlinx.benchmark.Scope
import kotlinx.benchmark.Setup
import kotlinx.benchmark.State

@State(Scope.Benchmark)
@Measurement(iterations = 3, time = 1, timeUnit = BenchmarkTimeUnit.SECONDS)
@OutputTimeUnit(BenchmarkTimeUnit.MILLISECONDS)
@BenchmarkMode(Mode.AverageTime)
class FuzzyMatchBenchmark {
private val dataset = arrayListOf<String>()
private val allowedChars = ('A'..'Z') + ('a'..'z') + ('0'..'9')
private val random = Random(852369741)

@Setup
fun setup() {
dataset.clear()
repeat(2000) { n -> dataset.add(getRandomString(n)) }
}

private fun getRandomString(length: Int): String {
return (1..length).map { allowedChars.random(random) }.joinToString("")
}

@Benchmark
fun fuzzyMatch() {
dataset.forEach { item -> Fuzzy.fuzzyMatchSimple("asdf", item) }
}
}
17 changes: 14 additions & 3 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,33 @@ pluginManagement {
exclusiveContent {
forRepository(::gradlePluginPortal)
filter {
listOf("com.diffplug.spotless", "org.jetbrains.kotlin.multiplatform").forEach { plugin ->
includeModule(plugin, "${plugin}.gradle.plugin")
}
includeModule("org.jetbrains.kotlinx", "kotlinx-benchmark-plugin")
val plugins =
listOf(
"com.diffplug.spotless",
"org.jetbrains.kotlin.multiplatform",
"org.jetbrains.kotlin.plugin.allopen",
"org.jetbrains.kotlinx.benchmark",
)
plugins.forEach { plugin -> includeModule(plugin, "${plugin}.gradle.plugin") }
}
}
mavenCentral()
}

plugins {
kotlin("multiplatform") version "1.7.10"
id("org.jetbrains.dokka") version "1.7.10"
id("org.jetbrains.kotlin.plugin.allopen") version "1.7.10"
id("com.diffplug.spotless") version "6.8.0"
id("org.jetbrains.kotlinx.binary-compatibility-validator") version "0.11.0"
id("org.jetbrains.kotlinx.benchmark") version "0.4.4"
id("com.vanniktech.maven.publish.base") version "0.21.0"
}
}

include("benchmark")

include("sample")

dependencyResolutionManagement { repositories { mavenCentral() } }

0 comments on commit 323feb5

Please sign in to comment.