Skip to content

Commit 078ef8c

Browse files
author
Abduqodiri Qurbonzoda
committed
Use "internal" native invocation mode by default
1 parent c84c4f3 commit 078ef8c

File tree

5 files changed

+26
-11
lines changed

5 files changed

+26
-11
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,16 @@ Available configuration options:
145145
* `iterationTime` – time to run each iteration (measuring and warmup)
146146
* `iterationTimeUnit` – time unit for `iterationTime` (default is seconds)
147147
* `outputTimeUnit` – time unit for results output
148-
* `mode` – "thrpt" for measuring operations per time, or "avgt" for measuring time per operation
149-
* `nativeIterationMode` – "external" for iterating in gradle in order to get correct Kotlin/Native runtime input in measurement,
150-
"internal" can be used if it's known that measured code have no calls in K/N runtime that can influence on measurement unrepeatedly.
151-
* `nativeGCCollectMode` – "iteration" to collect garbage after each measuring iteration,
152-
"auto"(default) for collecting garbage at the moments choosen by K/N compiler, time for each iteration could be very
153-
different then because of unpredictable call of GC.
148+
* `mode`
149+
- "thrpt" (default) – measures number of benchmark function invocations per time
150+
- "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
154+
* `nativeGCCollectMode`
155+
- "auto" (default) – collects garbage at the moments choosen by K/N compiler, time for each iteration can be very
156+
different because of unpredictable GC calls.
157+
- "iteration" – collects garbage after each measuring iteration
154158
* `include("…")` – regular expression to include benchmarks with fully qualified names matching it, as a substring
155159
* `exclude("…")` – regular expression to exclude benchmarks with fully qualified names matching it, as a substring
156160
* `param("name", "value1", "value2")` – specify a parameter for a public mutable property `name` annotated with `@Param`

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", "internal")
82+
advanced("nativeIterationMode", "external")
8383
advanced("nativeGCCollectMode", "iteration")
8484
}
8585

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,16 @@ 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" }
181+
180182
detailedConfigFiles.forEach { runConfig ->
181183
val runConfigPath = runConfig.absolutePath
182184
val lines = runConfig.readLines()
183185
require(lines.size > 1) { "Wrong detailed configuration format" }
184186
val currentConfigDescription = lines[1]
185187

186188
// Execute benchmark
187-
if (config.nativeIterationMode == "internal") {
189+
if (isInternalIterationMode) {
188190
val suiteResultsFile = createTempFile("bench", ".txt")
189191
execute(listOf(configFile.absolutePath, "--internal", benchProgressPath, runConfigPath, suiteResultsFile.absolutePath))
190192
val suiteResults = suiteResultsFile.readText()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class BenchmarkConfiguration private constructor(
1717
runner.iterationTimeUnit ?: suite.iterationTime.timeUnit,
1818
runner.outputTimeUnit ?: suite.outputTimeUnit,
1919
runner.mode ?: suite.mode,
20-
runner.nativeIterationMode ?: NativeIterationMode.External,
20+
runner.nativeIterationMode ?: NativeIterationMode.Internal,
2121
runner.nativeGCCollectMode ?: NativeGCCollectMode.Auto
2222
)
2323

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ class NativeExecutor(
9797

9898
fun run(benchmark: BenchmarkDescriptor<Any?>, benchmarkRun: BenchmarkRun,
9999
externalIterationNumber: Int? = null, externalCyclesNumber: Int? = null): DoubleArray? {
100+
101+
require((externalIterationNumber == null) == (externalCyclesNumber == null))
102+
require(benchmarkRun.config.nativeIterationMode == NativeIterationMode.External && externalIterationNumber != null
103+
|| benchmarkRun.config.nativeIterationMode == NativeIterationMode.Internal && externalIterationNumber == null)
104+
100105
val id = id(benchmark.name, benchmarkRun.parameters)
101106
val suite = benchmark.suite
102107

@@ -222,11 +227,15 @@ class NativeExecutor(
222227
benchmark: BenchmarkDescriptor<T>,
223228
currentIteration: Int? = null
224229
): Int {
230+
require(config.nativeIterationMode == NativeIterationMode.External && currentIteration != null
231+
|| config.nativeIterationMode == NativeIterationMode.Internal && currentIteration == null)
232+
225233
var iterations = 0
226234
val warmupIterations = if (config.nativeIterationMode == NativeIterationMode.External) 1 else config.warmups
227235
repeat(warmupIterations) { iteration ->
228236
val benchmarkNanos = config.iterationTime * config.iterationTimeUnit.toMultiplier()
229237
val executeFunction = benchmark.function
238+
230239
if (config.nativeGCCollectMode == NativeGCCollectMode.Iteration)
231240
GC.collect()
232241
val startTime = getTimeNanos()
@@ -239,12 +248,12 @@ class NativeExecutor(
239248
}
240249
if (config.nativeGCCollectMode == NativeGCCollectMode.Iteration)
241250
GC.collect()
251+
242252
val time = endTime - startTime
243253
val metric = time.toDouble() / iterations // TODO: metric
244254
val sample = metric.nanosToText(config.mode, config.outputTimeUnit)
245255
val iterationNumber = currentIteration ?: iteration
246-
if (config.nativeIterationMode == NativeIterationMode.Internal || currentIteration != null)
247-
reporter.output(name, benchmark.name, "Warm-up #$iterationNumber: $sample")
256+
reporter.output(name, benchmark.name, "Warm-up #$iterationNumber: $sample")
248257
}
249258
return iterations
250259
}

0 commit comments

Comments
 (0)