diff --git a/benchmark/build.gradle.kts b/benchmark/build.gradle.kts new file mode 100644 index 0000000..96a7548 --- /dev/null +++ b/benchmark/build.gradle.kts @@ -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") + } + } + } +} diff --git a/benchmark/src/commonMain/kotlin/benchmark/FuzzyMatchBenchmark.kt b/benchmark/src/commonMain/kotlin/benchmark/FuzzyMatchBenchmark.kt new file mode 100644 index 0000000..456e52e --- /dev/null +++ b/benchmark/src/commonMain/kotlin/benchmark/FuzzyMatchBenchmark.kt @@ -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() + 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) } + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts index 8784c0c..4e5b05c 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -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() } }