Closed goncalossilva closed 1 year ago
Yeah the docs aren't explicit about how to do it, but you need to create a compilation for that sourceset.
kotlin {
targets {
js {
compilations.create("bench")
}
}
sourceSets {
val commonBench by creating {
dependsOn(commonMain)
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-benchmark-runtime:0.4.0")
}
}
val jsBench by existing {
dependsOn(commonBench)
dependsOn(jsMain)
}
}
}
benchmark {
targets {
register("jsBench")
}
}
Thanks! I'm unable to make this work:
> Cannot add a KotlinJsIrCompilation with name 'benchmark' as a KotlinJsIrCompilation with that name already exists.
If I remove js { compilations.create("benchmark") }
, then I get:
KotlinSourceSet with name 'jsBenchmark' not found.
If instead I remove register("jsBenchmark")
, I get:
Caused by: java.lang.IllegalStateException: Projects must be configuring
Copied more directly from my actual project which uses the name "bench", not "benchmark"; perhaps the latter conflicts with some things kotlinx-benchmark sets up itself. (You did remove register("js")
though, right?)
A little hint (I still cant get this working though), targets
in kotlin { }
has to go after sourceSets { }
That shouldn't be necessary. My actual project: https://github.com/ephemient/aoc2021/blob/main/kt/build.gradle.kts (contains a lot of extra stuff you don't care about, but it runs benchmarks on a single target's "bench" compilation/sourceset).
If I dont, I do get
KotlinSourceSet with name 'jvmBench' not found. // jsBenchmark in your (second) case
For those who followed @ephemient's instructions and are getting the following gradle error:
Project#afterEvaluate(Action) on project ':rest-ktor' cannot be executed in the current context.
You'll want to change the existing
to getting
from:
val jsBench by existing {
dependsOn(commonBench)
dependsOn(jsMain)
}
to:
val jsBench by getting {
dependsOn(commonBench)
dependsOn(jsMain)
}
The following commit shows how I got it working in my multi-platform project based on the responses from @ephemient.
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:
I've tried adding:
And then configuring targets as documented:
But this is clearly not the right way, since nothing happens when running (“Test events were not received”) and there is this warning:
Could this be documented?