Skip to content

Commit e31ecc8

Browse files
author
Abduqodiri Qurbonzoda
committed
Rename nativeIterationMode -> nativeFork, external -> perIteration, internal -> perBenchmark
1 parent 078ef8c commit e31ecc8

File tree

9 files changed

+47
-37
lines changed

9 files changed

+47
-37
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@ Available configuration options:
148148
* `mode`
149149
- "thrpt" (default) – measures number of benchmark function invocations per time
150150
- "avgt" – measures time per benchmark function invocation
151-
* `nativeIterationMode`
152-
- "internal" (default) – executes all iterations of a benchmark in the same process (one binary execution)
153-
- "external" – executes each iteration of a benchmark in a separate process, measures in cold Kotlin/Native runtime environment
151+
* `nativeFork`
152+
- "perBenchmark" (default) – executes all iterations of a benchmark in the same process (one binary execution)
153+
- "perIteration" – executes each iteration of a benchmark in a separate process, measures in cold Kotlin/Native runtime environment
154154
* `nativeGCCollectMode`
155155
- "auto" (default) – collects garbage at the moments choosen by K/N compiler, time for each iteration can be very
156156
different because of unpredictable GC calls.

examples/kotlin-multiplatform/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ benchmark {
7979
iterationTime = 300 // time in ms per iteration
8080
iterationTimeUnit = "ms" // time in ms per iteration
8181
advanced("forks", 1)
82-
advanced("nativeIterationMode", "external")
82+
advanced("nativeFork", "perIteration")
8383
advanced("nativeGCCollectMode", "iteration")
8484
}
8585

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ open class BenchmarkConfiguration(val extension: BenchmarksExtension, val name:
3434
advanced[name] = value
3535
}
3636

37-
val nativeIterationMode: String?
38-
get() = advanced["nativeIterationMode"] as? String
37+
val nativeFork: String?
38+
get() = advanced["nativeFork"] as? String
3939

4040
fun capitalizedName() = if (name == "main") "" else name.capitalize()
4141
fun prefixName(suffix: String) = if (name == "main") suffix else name + suffix.capitalize()

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ open class NativeBenchmarkExec() : DefaultTask() {
177177
val detailedConfigFiles = project.fileTree(benchsDescriptionDir).files.sortedBy { it.absolutePath }
178178
val runResults = mutableMapOf<String, String>()
179179

180-
val isInternalIterationMode = config.nativeIterationMode.let { it == null || it == "internal" }
180+
val forkPerBenchmark = config.nativeFork.let { it == null || it == "perBenchmark" }
181181

182182
detailedConfigFiles.forEach { runConfig ->
183183
val runConfigPath = runConfig.absolutePath
@@ -186,9 +186,9 @@ open class NativeBenchmarkExec() : DefaultTask() {
186186
val currentConfigDescription = lines[1]
187187

188188
// Execute benchmark
189-
if (isInternalIterationMode) {
189+
if (forkPerBenchmark) {
190190
val suiteResultsFile = createTempFile("bench", ".txt")
191-
execute(listOf(configFile.absolutePath, "--internal", benchProgressPath, runConfigPath, suiteResultsFile.absolutePath))
191+
execute(listOf(configFile.absolutePath, "--benchmark", benchProgressPath, runConfigPath, suiteResultsFile.absolutePath))
192192
val suiteResults = suiteResultsFile.readText()
193193
if (suiteResults.isNotEmpty())
194194
runResults[runConfigPath] = suiteResults

runtime/commonMain/src/kotlinx/benchmark/BenchmarkReportFormatter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private object JsonBenchmarkReportFormatter : BenchmarkReportFormatter() {
174174
"params" : {
175175
${result.params.entries.joinToString(separator = ",\n ") { "\"${it.key}\" : \"${it.value}\"" }}
176176
},
177-
"nativeIterationMode" : "${result.config.nativeIterationMode.toText()}",
177+
"nativeFork" : "${result.config.nativeFork.toText()}",
178178
"nativeGCCollectMode" : "${result.config.nativeGCCollectMode.toText()}",
179179
"primaryMetric" : {
180180
"score": ${result.score},

runtime/commonMain/src/kotlinx/benchmark/CommonBenchmarkAnnotations.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ expect enum class Mode {
2424
Throughput, AverageTime
2525
}
2626

27-
enum class NativeIterationMode {
28-
Internal, External
27+
enum class NativeFork {
28+
PerBenchmark, PerIteration
2929
}
3030

3131
enum class NativeGCCollectMode {
@@ -65,9 +65,9 @@ fun Mode.toText() = when (this) {
6565
}
6666

6767
@Suppress("REDUNDANT_ELSE_IN_WHEN")
68-
fun NativeIterationMode.toText() = when (this) {
69-
NativeIterationMode.External -> "external"
70-
NativeIterationMode.Internal -> "internal"
68+
fun NativeFork.toText() = when (this) {
69+
NativeFork.PerIteration -> "perIteration"
70+
NativeFork.PerBenchmark -> "perBenchmark"
7171
else -> throw UnsupportedOperationException("$this is not supported")
7272
}
7373

runtime/commonMain/src/kotlinx/benchmark/ExecutorConfiguration.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class BenchmarkConfiguration private constructor(
77
val iterationTimeUnit: BenchmarkTimeUnit,
88
val outputTimeUnit: BenchmarkTimeUnit,
99
val mode: Mode,
10-
val nativeIterationMode: NativeIterationMode,
10+
val nativeFork: NativeFork,
1111
val nativeGCCollectMode: NativeGCCollectMode) {
1212

1313
constructor(runner: RunnerConfiguration, suite: SuiteDescriptor<*>) : this(
@@ -17,14 +17,14 @@ class BenchmarkConfiguration private constructor(
1717
runner.iterationTimeUnit ?: suite.iterationTime.timeUnit,
1818
runner.outputTimeUnit ?: suite.outputTimeUnit,
1919
runner.mode ?: suite.mode,
20-
runner.nativeIterationMode ?: NativeIterationMode.Internal,
20+
runner.nativeFork ?: NativeFork.PerBenchmark,
2121
runner.nativeGCCollectMode ?: NativeGCCollectMode.Auto
2222
)
2323

2424
override fun toString() =
2525
"iterations=$iterations, warmups=$warmups, iterationTime=$iterationTime, " +
2626
"iterationTimeUnit=${iterationTimeUnit.toText()}, outputTimeUnit=${outputTimeUnit.toText()}, " +
27-
"mode=${mode.toText()}, nativeIterationMode=${nativeIterationMode.toText()}, " +
27+
"mode=${mode.toText()}, nativeFork=${nativeFork.toText()}, " +
2828
"nativeGCCollectMode=${nativeGCCollectMode.toText()}"
2929

3030
companion object {
@@ -41,7 +41,7 @@ class BenchmarkConfiguration private constructor(
4141
parseTimeUnit(getParameterValue("iterationTimeUnit")),
4242
parseTimeUnit(getParameterValue("outputTimeUnit")),
4343
getParameterValue("mode").toMode(),
44-
NativeIterationMode.valueOf(getParameterValue("nativeIterationMode").replaceFirstChar { it.uppercaseChar() }),
44+
NativeFork.valueOf(getParameterValue("nativeFork").replaceFirstChar { it.uppercaseChar() }),
4545
NativeGCCollectMode.valueOf(getParameterValue("nativeGCCollectMode").replaceFirstChar { it.uppercaseChar() })
4646
)
4747
}

runtime/commonMain/src/kotlinx/benchmark/RunnerConfiguration.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ class RunnerConfiguration(config: String) {
6868
"mode"
6969
) { Mode.valueOf(it) }
7070

71-
val nativeIterationMode = singleValueOrNull(
72-
"nativeIterationMode"
73-
) { NativeIterationMode.valueOf(it.replaceFirstChar { firstChar -> firstChar.uppercaseChar() }) }
71+
val nativeFork = singleValueOrNull(
72+
"nativeFork"
73+
) { NativeFork.valueOf(it.replaceFirstChar { firstChar -> firstChar.uppercaseChar() }) }
7474

7575
val nativeGCCollectMode = singleValueOrNull(
7676
"nativeGCCollectMode"
@@ -87,7 +87,7 @@ iterationTime: $iterationTime
8787
iterationTimeUnit: $iterationTimeUnit
8888
outputTimeUnit: $outputTimeUnit
8989
mode: $mode
90-
nativeIterationMode: $nativeIterationMode
90+
nativeFork: $nativeFork
9191
nativeGCCollectMode: $nativeGCCollectMode
9292
"""
9393
}

runtime/nativeMain/src/kotlinx/benchmark/native/NativeExecutor.kt

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,28 @@ class NativeExecutor(
8787
}
8888
}
8989

90-
private fun endExternalBenchmarksRun(benchmarks: List<BenchmarkDescriptor<Any?>>) {
90+
private fun endForkedIterationsRun(benchmarks: List<BenchmarkDescriptor<Any?>>) {
9191
val (configFileName, samplesFile) = additionalArguments
9292
val samples = samplesFile.readFile().split(", ").map { it.toDouble() }.toDoubleArray()
9393
val benchmarkRun = configFileName.parseBenchmarkConfig()
9494
val benchmark = benchmarks.getBenchmark(benchmarkRun.benchmarkName)
9595
saveBenchmarkResults(benchmark, benchmarkRun, samples)
9696
}
9797

98-
fun run(benchmark: BenchmarkDescriptor<Any?>, benchmarkRun: BenchmarkRun,
99-
externalIterationNumber: Int? = null, externalCyclesNumber: Int? = null): DoubleArray? {
98+
fun run(
99+
benchmark: BenchmarkDescriptor<Any?>,
100+
benchmarkRun: BenchmarkRun,
101+
currentIteration: Int? = null,
102+
cyclesPerIteration: Int? = null
103+
): DoubleArray? {
100104

101-
require((externalIterationNumber == null) == (externalCyclesNumber == null))
102-
require(benchmarkRun.config.nativeIterationMode == NativeIterationMode.External && externalIterationNumber != null
103-
|| benchmarkRun.config.nativeIterationMode == NativeIterationMode.Internal && externalIterationNumber == null)
105+
require((currentIteration == null) == (cyclesPerIteration == null)) {
106+
"Current iteration number must be provided if and only if the number of cycles per iteration is provided"
107+
}
108+
require(benchmarkRun.config.nativeFork == NativeFork.PerIteration && currentIteration != null
109+
|| benchmarkRun.config.nativeFork == NativeFork.PerBenchmark && currentIteration == null) {
110+
"Fork must be per benchmark or current iteration number must be provided, but not both at the same time"
111+
}
104112

105113
val id = id(benchmark.name, benchmarkRun.parameters)
106114
val suite = benchmark.suite
@@ -110,14 +118,14 @@ class NativeExecutor(
110118
benchmark.suite.setup(instance)
111119

112120
var exception: Throwable? = null
113-
val iterations = if (benchmarkRun.config.nativeIterationMode == NativeIterationMode.External) 1 else benchmarkRun.config.iterations
121+
val iterations = if (benchmarkRun.config.nativeFork == NativeFork.PerIteration) 1 else benchmarkRun.config.iterations
114122
val samples = try {
115123
// Execute warmup
116-
val cycles = externalCyclesNumber ?: warmup(suite.name, benchmarkRun.config, instance, benchmark)
124+
val cycles = cyclesPerIteration ?: warmup(suite.name, benchmarkRun.config, instance, benchmark)
117125
DoubleArray(iterations) { iteration ->
118126
val nanosecondsPerOperation = measure(instance, benchmark, cycles, benchmarkRun.config.nativeGCCollectMode)
119127
val text = nanosecondsPerOperation.nanosToText(benchmarkRun.config.mode, benchmarkRun.config.outputTimeUnit)
120-
val iterationNumber = externalIterationNumber ?: iteration
128+
val iterationNumber = currentIteration ?: iteration
121129
reporter.output(
122130
executionName,
123131
id,
@@ -185,10 +193,10 @@ class NativeExecutor(
185193
when (action) {
186194
"--list" -> outputBenchmarks(runnerConfiguration, benchmarks, start)
187195
"--store-results" -> storeResults(benchmarks, complete)
188-
"--internal" -> runBenchmark(benchmarks)
196+
"--benchmark" -> runBenchmark(benchmarks)
189197
"--iteration" -> runBenchmarkIteration(benchmarks)
190198
"--warmup" -> runBenchmarkWarmup(benchmarks)
191-
"--end-run" -> endExternalBenchmarksRun(benchmarks)
199+
"--end-run" -> endForkedIterationsRun(benchmarks)
192200
else -> throw IllegalArgumentException("Unknown action: $action.")
193201
}
194202
}
@@ -227,11 +235,13 @@ class NativeExecutor(
227235
benchmark: BenchmarkDescriptor<T>,
228236
currentIteration: Int? = null
229237
): Int {
230-
require(config.nativeIterationMode == NativeIterationMode.External && currentIteration != null
231-
|| config.nativeIterationMode == NativeIterationMode.Internal && currentIteration == null)
238+
require(config.nativeFork == NativeFork.PerIteration && currentIteration != null
239+
|| config.nativeFork == NativeFork.PerBenchmark && currentIteration == null) {
240+
"Fork must be per benchmark or current iteration number must be provided, but not both at the same time"
241+
}
232242

233243
var iterations = 0
234-
val warmupIterations = if (config.nativeIterationMode == NativeIterationMode.External) 1 else config.warmups
244+
val warmupIterations = if (config.nativeFork == NativeFork.PerIteration) 1 else config.warmups
235245
repeat(warmupIterations) { iteration ->
236246
val benchmarkNanos = config.iterationTime * config.iterationTimeUnit.toMultiplier()
237247
val executeFunction = benchmark.function

0 commit comments

Comments
 (0)