kotlinx-benchmark icon indicating copy to clipboard operation
kotlinx-benchmark copied to clipboard

Examples don't work for Kotlin Multiplatform project

Open aSemy opened this issue 3 years ago • 1 comments
trafficstars

I've tried copying the examples

  • https://github.com/Kotlin/kotlinx-benchmark/blob/24fb001e8cae88befd5b352b0e281517fe595f67/examples/kotlin-multiplatform/build.gradle
  • https://github.com/Kotlin/kotlinx-benchmark/blob/24fb001e8cae88befd5b352b0e281517fe595f67/examples/kotlin-kts/build.gradle.kts

but I can't make any good progress, I immediately come across fatal errors.

Cannot find a benchmark compilation 'main', ignoring.
* Exception is:
org.gradle.api.ProjectConfigurationException: A problem occurred configuring project ':modules:client-tests'

Caused by: java.lang.IllegalStateException: Could not create domain object 'main' (BenchmarkTarget)

Caused by: java.lang.ClassCastException: class kotlinx.benchmark.gradle.BenchmarkTarget cannot be cast to class kotlinx.benchmark.gradle.JvmBenchmarkTarget (kotlinx.benchmark.gradle.BenchmarkTarget and kotlinx.benchmark.gradle.JvmBenchmarkTarget are in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @6e21e3fb)
  • Kotlin 1.7.10
  • Kotlinx Benchmark 0.4.4
import kotlinx.benchmark.gradle.JvmBenchmarkTarget

plugins {
    buildsrc.convention.`kotlin-multiplatform` // convention plugin that applies kotlin("multiplatform") 1.7.10

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

kotlin {
    jvm()

    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(kotlin("reflect"))

                implementation("org.jetbrains.kotlinx:kotlinx-benchmark-runtime:0.4.4")
            }
        }

        val commonTest by getting {
            dependencies {
                implementation(projects.modules.mockk)
            }
        }

        val jvmMain by getting {
            dependencies {
            }
        }

        val jvmTest by getting {
            dependencies {
                implementation(buildsrc.config.Deps.Libs.kotlinTestJunit()) {
                    exclude(group = "junit", module = "junit")
                }

                implementation(buildsrc.config.Deps.Libs.slfj)
                implementation(buildsrc.config.Deps.Libs.logback)

                implementation(buildsrc.config.Deps.Libs.junitJupiter)
            }
        }
    }
}

benchmark {
    configurations {
        named("main") {
            iterationTime = 60
            iterationTimeUnit = "sec"
            iterations = 2
            warmups = 1
        }
    }
    targets {
        register("main") {
            this as JvmBenchmarkTarget
            jmhVersion = "1.33"
        }
    }
}

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

aSemy avatar Jul 31 '22 19:07 aSemy

@aSemy the following commit shows how I got it working in my multi-platform project. It is a minimal commit with just the changes. Make sure you read the README.md file in the commit, especially if you only have common benchmarks and no JVM-specific benchmarks.

https://github.com/tree-ware/tree-ware-kotlin-core/commit/25d284167ae9b0c4d55ea546d45b7e4ec395c2f1

The benchmark tasks then show up in the Gradle panel on the right (in IntelliJ IDEA) in their own category of tasks:

Screen Shot 2022-12-04 at 12 30 38 PM

deepak-nulu avatar Dec 04 '22 20:12 deepak-nulu