Skip to content

Commit

Permalink
Support debug build configuration for K/N (Kotlin#225)
Browse files Browse the repository at this point in the history
Adds a build type setting to the NativeBenchmarkTarget to provide the ability to test debug builds.
Fixes Kotlin#189
  • Loading branch information
PavelPunegov authored Jun 10, 2024
1 parent 875ec08 commit 7950a87
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 3 deletions.
14 changes: 14 additions & 0 deletions docs/configuration-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ The options listed in the following sections allow you to tailor the benchmark e
| `advanced("nativeFork", "value")` | Executes iterations within the same process ("perBenchmark") or each iteration in a separate process ("perIteration"). | `"perBenchmark"`, `"perIteration"` | `"perBenchmark"` |
| `advanced("nativeGCAfterIteration", value)` | Whether to trigger garbage collection after each iteration. | `true`, `false` | `false` |

By default, to run benchmarks, the library uses a release type of native binary, which is optimized one and without debug information.
It is possibly to change the type to debug by setting it during benchmark targets configuration:

```Kotlin
benchmark {
targets {
register("native") {
this as NativeBenchmarkTarget
buildType = NativeBuildType.DEBUG
}
}
}
```

### Kotlin/JVM
| Option | Description | Possible Values | Default Value |
|---------------------------------------------|------------------------------------------------------------|----------------------------------------|----------------|
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package kotlinx.benchmark.integration

import org.junit.Test

class KotlinNativeTest : GradleTest() {
@Test
fun debugBenchmarkTest() {
project("kotlin-native", true).let { runner ->
val target = "native"
val capitalizedTarget = target.replaceFirstChar { it.uppercaseChar() }

runner.run(":${target}BenchmarkGenerate")
runner.run(":compile${capitalizedTarget}BenchmarkKotlin${capitalizedTarget}")
runner.run(":${capitalizedTarget}Benchmark")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import org.jetbrains.kotlin.gradle.plugin.mpp.NativeBuildType
import org.jetbrains.kotlin.konan.target.KonanTarget
import org.jetbrains.kotlin.konan.target.HostManager

kotlin {
if (HostManager.hostIsLinux) linuxX64('native')
if (HostManager.hostIsMingw) mingwX64('native')
if (HostManager.host == KonanTarget.MACOS_ARM64.INSTANCE) macosArm64('native')
if (HostManager.host == KonanTarget.MACOS_X64.INSTANCE) macosX64('native')

sourceSets {
commonMain {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-benchmark-runtime:0.5.0-SNAPSHOT")
}
}
nativeMain { }
}
}

benchmark {
targets {
register("native") {
buildType = NativeBuildType.DEBUG
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.gradle.jvmargs=-Xmx2g -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package test

import kotlinx.benchmark.*
import kotlin.math.*

@State(Scope.Benchmark)
@Measurement(iterations = 3, time = 1, timeUnit = BenchmarkTimeUnit.SECONDS)
@OutputTimeUnit(BenchmarkTimeUnit.MILLISECONDS)
@BenchmarkMode(Mode.Throughput)
open class CommonBenchmark {
@Benchmark
open fun mathBenchmark(): Double {
return log(sqrt(3.0) * cos(3.0), 2.0)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package test

import kotlinx.benchmark.*
import kotlin.native.Platform

@State(Scope.Benchmark)
@Measurement(iterations = 3, time = 1, timeUnit = BenchmarkTimeUnit.SECONDS)
@OutputTimeUnit(BenchmarkTimeUnit.MILLISECONDS)
@BenchmarkMode(Mode.Throughput)
open class DebugBenchmark {
@Benchmark
@OptIn(kotlin.experimental.ExperimentalNativeApi::class)
open fun debugBenchmark(): String {
return if (Platform.isDebugBinary) "Debug Benchmark"
else error("Not a debug binary")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import kotlinx.benchmark.gradle.internal.KotlinxBenchmarkPluginInternalApi
import org.gradle.api.tasks.*
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmCompilation
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeCompilation
import org.jetbrains.kotlin.gradle.plugin.mpp.NativeBuildType
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrCompilation

open class BenchmarkConfiguration
Expand Down Expand Up @@ -122,4 +123,6 @@ constructor(
name: String,
@property:KotlinxBenchmarkPluginInternalApi
val compilation: KotlinNativeCompilation
) : BenchmarkTarget(extension, name)
) : BenchmarkTarget(extension, name) {
var buildType: NativeBuildType = NativeBuildType.RELEASE
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private fun Project.createNativeBenchmarkCompileTask(target: NativeBenchmarkTarg
compilationTarget.apply {
binaries {
// The release build type is already optimized and non-debuggable.
executable(benchmarkCompilation.name, listOf(RELEASE)) {
executable(benchmarkCompilation.name, listOf(target.buildType)) {
this.compilation = benchmarkCompilation
this.outputDirectory = file("$benchmarkBuildDir/classes")
// A link task's name is linkReleaseExecutable<Target>.
Expand Down Expand Up @@ -120,7 +120,7 @@ fun Project.createNativeBenchmarkExecTask(
description = "Executes benchmark for '${target.name}'"

val binary =
benchmarkCompilation.target.binaries.getExecutable(benchmarkCompilation.name, NativeBuildType.RELEASE)
benchmarkCompilation.target.binaries.getExecutable(benchmarkCompilation.name, target.buildType)
val linkTask = binary.linkTask

dependsOn(linkTask)
Expand Down

0 comments on commit 7950a87

Please sign in to comment.