Skip to content

Commit 7950a87

Browse files
authored
Support debug build configuration for K/N (#225)
Adds a build type setting to the NativeBenchmarkTarget to provide the ability to test debug builds. Fixes #189
1 parent 875ec08 commit 7950a87

File tree

8 files changed

+97
-3
lines changed

8 files changed

+97
-3
lines changed

docs/configuration-options.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ The options listed in the following sections allow you to tailor the benchmark e
5959
| `advanced("nativeFork", "value")` | Executes iterations within the same process ("perBenchmark") or each iteration in a separate process ("perIteration"). | `"perBenchmark"`, `"perIteration"` | `"perBenchmark"` |
6060
| `advanced("nativeGCAfterIteration", value)` | Whether to trigger garbage collection after each iteration. | `true`, `false` | `false` |
6161

62+
By default, to run benchmarks, the library uses a release type of native binary, which is optimized one and without debug information.
63+
It is possibly to change the type to debug by setting it during benchmark targets configuration:
64+
65+
```Kotlin
66+
benchmark {
67+
targets {
68+
register("native") {
69+
this as NativeBenchmarkTarget
70+
buildType = NativeBuildType.DEBUG
71+
}
72+
}
73+
}
74+
```
75+
6276
### Kotlin/JVM
6377
| Option | Description | Possible Values | Default Value |
6478
|---------------------------------------------|------------------------------------------------------------|----------------------------------------|----------------|
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package kotlinx.benchmark.integration
2+
3+
import org.junit.Test
4+
5+
class KotlinNativeTest : GradleTest() {
6+
@Test
7+
fun debugBenchmarkTest() {
8+
project("kotlin-native", true).let { runner ->
9+
val target = "native"
10+
val capitalizedTarget = target.replaceFirstChar { it.uppercaseChar() }
11+
12+
runner.run(":${target}BenchmarkGenerate")
13+
runner.run(":compile${capitalizedTarget}BenchmarkKotlin${capitalizedTarget}")
14+
runner.run(":${capitalizedTarget}Benchmark")
15+
}
16+
}
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import org.jetbrains.kotlin.gradle.plugin.mpp.NativeBuildType
2+
import org.jetbrains.kotlin.konan.target.KonanTarget
3+
import org.jetbrains.kotlin.konan.target.HostManager
4+
5+
kotlin {
6+
if (HostManager.hostIsLinux) linuxX64('native')
7+
if (HostManager.hostIsMingw) mingwX64('native')
8+
if (HostManager.host == KonanTarget.MACOS_ARM64.INSTANCE) macosArm64('native')
9+
if (HostManager.host == KonanTarget.MACOS_X64.INSTANCE) macosX64('native')
10+
11+
sourceSets {
12+
commonMain {
13+
dependencies {
14+
implementation("org.jetbrains.kotlinx:kotlinx-benchmark-runtime:0.5.0-SNAPSHOT")
15+
}
16+
}
17+
nativeMain { }
18+
}
19+
}
20+
21+
benchmark {
22+
targets {
23+
register("native") {
24+
buildType = NativeBuildType.DEBUG
25+
}
26+
}
27+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.gradle.jvmargs=-Xmx2g -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package test
2+
3+
import kotlinx.benchmark.*
4+
import kotlin.math.*
5+
6+
@State(Scope.Benchmark)
7+
@Measurement(iterations = 3, time = 1, timeUnit = BenchmarkTimeUnit.SECONDS)
8+
@OutputTimeUnit(BenchmarkTimeUnit.MILLISECONDS)
9+
@BenchmarkMode(Mode.Throughput)
10+
open class CommonBenchmark {
11+
@Benchmark
12+
open fun mathBenchmark(): Double {
13+
return log(sqrt(3.0) * cos(3.0), 2.0)
14+
}
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package test
2+
3+
import kotlinx.benchmark.*
4+
import kotlin.native.Platform
5+
6+
@State(Scope.Benchmark)
7+
@Measurement(iterations = 3, time = 1, timeUnit = BenchmarkTimeUnit.SECONDS)
8+
@OutputTimeUnit(BenchmarkTimeUnit.MILLISECONDS)
9+
@BenchmarkMode(Mode.Throughput)
10+
open class DebugBenchmark {
11+
@Benchmark
12+
@OptIn(kotlin.experimental.ExperimentalNativeApi::class)
13+
open fun debugBenchmark(): String {
14+
return if (Platform.isDebugBinary) "Debug Benchmark"
15+
else error("Not a debug binary")
16+
}
17+
}

plugin/main/src/kotlinx/benchmark/gradle/BenchmarkConfiguration.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import kotlinx.benchmark.gradle.internal.KotlinxBenchmarkPluginInternalApi
44
import org.gradle.api.tasks.*
55
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmCompilation
66
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeCompilation
7+
import org.jetbrains.kotlin.gradle.plugin.mpp.NativeBuildType
78
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrCompilation
89

910
open class BenchmarkConfiguration
@@ -122,4 +123,6 @@ constructor(
122123
name: String,
123124
@property:KotlinxBenchmarkPluginInternalApi
124125
val compilation: KotlinNativeCompilation
125-
) : BenchmarkTarget(extension, name)
126+
) : BenchmarkTarget(extension, name) {
127+
var buildType: NativeBuildType = NativeBuildType.RELEASE
128+
}

plugin/main/src/kotlinx/benchmark/gradle/NativeMultiplatformTasks.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private fun Project.createNativeBenchmarkCompileTask(target: NativeBenchmarkTarg
8888
compilationTarget.apply {
8989
binaries {
9090
// The release build type is already optimized and non-debuggable.
91-
executable(benchmarkCompilation.name, listOf(RELEASE)) {
91+
executable(benchmarkCompilation.name, listOf(target.buildType)) {
9292
this.compilation = benchmarkCompilation
9393
this.outputDirectory = file("$benchmarkBuildDir/classes")
9494
// A link task's name is linkReleaseExecutable<Target>.
@@ -120,7 +120,7 @@ fun Project.createNativeBenchmarkExecTask(
120120
description = "Executes benchmark for '${target.name}'"
121121

122122
val binary =
123-
benchmarkCompilation.target.binaries.getExecutable(benchmarkCompilation.name, NativeBuildType.RELEASE)
123+
benchmarkCompilation.target.binaries.getExecutable(benchmarkCompilation.name, target.buildType)
124124
val linkTask = binary.linkTask
125125

126126
dependsOn(linkTask)

0 commit comments

Comments
 (0)