Skip to content

Commit b5e0a23

Browse files
committed
Remove platform specific flags from common code
1 parent 777fcc9 commit b5e0a23

File tree

11 files changed

+67
-77
lines changed

11 files changed

+67
-77
lines changed

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ open class BenchmarkConfiguration(val extension: BenchmarksExtension, val name:
3535
advanced[name] = value
3636
}
3737

38-
val nativeFork: String?
39-
get() = advanced["nativeFork"] as? String
40-
4138
fun capitalizedName() = if (name == "main") "" else name.capitalize()
4239
fun prefixName(suffix: String) = if (name == "main") suffix else name + suffix.capitalize()
4340
fun reportFileExt(): String = reportFormat?.toLowerCase() ?: "json"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ fun Project.createNativeBenchmarkExecTask(
121121
onlyIf { executableFile.exists() }
122122

123123
this.executable = executableFile
124-
this.nativeFork = config.nativeFork
124+
this.nativeFork = config.advanced["nativeFork"] as? String
125125
this.workingDir = target.workingDir
126126
this.benchProgressPath = createTempFile("bench", ".txt").absolutePath
127127

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ fun writeParameters(
123123
values.forEach { value -> appendLine("param:$param=$value") }
124124
}
125125
config.advanced.forEach { (param, value) ->
126-
appendLine("$param:$value")
126+
appendLine("advanced:$param=$value")
127127
}
128128
})
129129
return file

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,9 @@ private object JsonBenchmarkReportFormatter : BenchmarkReportFormatter() {
174174
"params" : {
175175
${result.params.entries.joinToString(separator = ",\n ") { "\"${it.key.escape()}\" : \"${it.value.escape()}\"" }}
176176
},
177-
"nativeFork" : "${result.config.nativeFork.toText()}",
178-
"nativeGCAfterIteration" : ${result.config.nativeGCAfterIteration},
177+
"advanced" : {
178+
${result.config.advanced.entries.joinToString(separator = ",\n ") { "\"${it.key.escape()}\" : \"${it.value.escape()}\"" }}
179+
},
179180
"primaryMetric" : {
180181
"score": ${result.score},
181182
"scoreError": ${result.error},

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ expect enum class Mode {
2323
Throughput, AverageTime
2424
}
2525

26-
enum class NativeFork {
27-
PerBenchmark, PerIteration
28-
}
29-
3026
@Target(AnnotationTarget.CLASS)
3127
expect annotation class OutputTimeUnit(val value: BenchmarkTimeUnit)
3228

@@ -59,13 +55,6 @@ fun Mode.toText() = when (this) {
5955
else -> throw UnsupportedOperationException("$this is not supported")
6056
}
6157

62-
@Suppress("REDUNDANT_ELSE_IN_WHEN")
63-
fun NativeFork.toText() = when (this) {
64-
NativeFork.PerIteration -> "perIteration"
65-
NativeFork.PerBenchmark -> "perBenchmark"
66-
else -> throw UnsupportedOperationException("$this is not supported")
67-
}
68-
6958
@Suppress("REDUNDANT_ELSE_IN_WHEN")
7059
fun BenchmarkTimeUnit.toMultiplier() = when (this) {
7160
BenchmarkTimeUnit.NANOSECONDS -> 1

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

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,59 @@ class BenchmarkConfiguration private constructor(
77
val iterationTimeUnit: BenchmarkTimeUnit,
88
val outputTimeUnit: BenchmarkTimeUnit,
99
val mode: Mode,
10-
val nativeFork: NativeFork,
11-
val nativeGCAfterIteration: Boolean) {
12-
10+
val advanced: Map<String, String>,
11+
) {
1312
constructor(runner: RunnerConfiguration, suite: SuiteDescriptor<*>) : this(
14-
runner.iterations ?: suite.iterations,
15-
runner.warmups ?: suite.warmups,
16-
runner.iterationTime ?: suite.iterationTime.value,
17-
runner.iterationTimeUnit ?: suite.iterationTime.timeUnit,
18-
runner.outputTimeUnit ?: suite.outputTimeUnit,
19-
runner.mode ?: suite.mode,
20-
runner.nativeFork ?: NativeFork.PerBenchmark,
21-
runner.nativeGCAfterIteration ?: false
13+
iterations = runner.iterations ?: suite.iterations,
14+
warmups = runner.warmups ?: suite.warmups,
15+
iterationTime = runner.iterationTime ?: suite.iterationTime.value,
16+
iterationTimeUnit = runner.iterationTimeUnit ?: suite.iterationTime.timeUnit,
17+
outputTimeUnit = runner.outputTimeUnit ?: suite.outputTimeUnit,
18+
mode = runner.mode ?: suite.mode,
19+
advanced = runner.advanced
2220
)
2321

2422
override fun toString() =
2523
"iterations=$iterations, warmups=$warmups, iterationTime=$iterationTime, " +
2624
"iterationTimeUnit=${iterationTimeUnit.toText()}, outputTimeUnit=${outputTimeUnit.toText()}, " +
27-
"mode=${mode.toText()}, nativeFork=${nativeFork.toText()}, " +
28-
"nativeGCAfterIteration=$nativeGCAfterIteration"
25+
"mode=${mode.toText()}" +
26+
advanced.entries.joinToString(prefix = ", ", separator = ", ") { "advanced:${it.key}=${it.value}" }
2927

3028
companion object {
3129
fun parse(description: String): BenchmarkConfiguration {
3230
val parameters = description.parseMap()
33-
3431
fun getParameterValue(key: String) =
3532
parameters[key] ?: throw NoSuchElementException("Parameter `$key` is required.")
3633

34+
val advanced = parameters
35+
.filter { it.key.startsWith("advanced:") }
36+
.entries
37+
.associate {
38+
val advancedKey = it.key.substringAfter(":")
39+
check(advancedKey.isNotEmpty()) { "Invalid advanced key - should not be empty" }
40+
advancedKey to it.value
41+
}
42+
3743
return BenchmarkConfiguration(
38-
getParameterValue("iterations").toInt(),
39-
getParameterValue("warmups").toInt(),
40-
getParameterValue("iterationTime").toLong(),
41-
parseTimeUnit(getParameterValue("iterationTimeUnit")),
42-
parseTimeUnit(getParameterValue("outputTimeUnit")),
43-
getParameterValue("mode").toMode(),
44-
NativeFork.valueOf(getParameterValue("nativeFork").replaceFirstChar { it.uppercaseChar() }),
45-
getParameterValue("nativeGCAfterIteration").toBooleanStrict()
44+
iterations = getParameterValue("iterations").toInt(),
45+
warmups = getParameterValue("warmups").toInt(),
46+
iterationTime = getParameterValue("iterationTime").toLong(),
47+
iterationTimeUnit = parseTimeUnit(getParameterValue("iterationTimeUnit")),
48+
outputTimeUnit = parseTimeUnit(getParameterValue("outputTimeUnit")),
49+
mode = getParameterValue("mode").toMode(),
50+
advanced = advanced
4651
)
4752
}
4853
}
4954
}
5055

5156
internal fun String.parseMap(): Map<String, String> =
52-
this.removeSurrounding("{", "}").split(", ").filter{ it.isNotEmpty() }.map {
53-
val keyValue = it.split("=")
54-
require(keyValue.size == 2) { "Wrong format of map string description!" }
55-
val (key, value) = keyValue
56-
key to value
57-
}.toMap()
57+
removeSurrounding("{", "}")
58+
.split(", ")
59+
.filter { it.isNotEmpty() }
60+
.associate {
61+
val keyValue = it.split("=")
62+
require(keyValue.size == 2) { "Wrong format of map string description!" }
63+
val (key, value) = keyValue
64+
key to value
65+
}

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

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,6 @@ class RunnerConfiguration(config: String) {
66
it.substringBefore(":")
77
}, { it.substringAfter(":", "") })
88

9-
/*
10-
init {
11-
println("Config:")
12-
println(config)
13-
14-
println("Values:")
15-
println(values)
16-
}
17-
*/
18-
199
val name = singleValue("name")
2010
val reportFile = singleValue("reportFile")
2111
val traceFormat = singleValue("traceFormat")
@@ -32,14 +22,7 @@ class RunnerConfiguration(config: String) {
3222
val warmups = singleValueOrNull("warmups") { it.toInt() }
3323
val iterationTime = singleValueOrNull("iterationTime") { it.toLong() }
3424
val iterationTimeUnit = singleValueOrNull("iterationTimeUnit") { parseTimeUnit(it) }
35-
36-
val jvmForks = singleValueOrNull("jvmForks").let { forks ->
37-
val legacy = singleValueOrNull("forks")
38-
if (legacy != null) {
39-
println("""Deprecation warning: configuration option "forks" was renamed to "jvmForks"""")
40-
}
41-
forks ?: legacy
42-
}
25+
val advanced = mapSingleValues("advanced", "=")
4326

4427
val outputTimeUnit = singleValueOrNull(
4528
"outputTimeUnit"
@@ -66,6 +49,13 @@ class RunnerConfiguration(config: String) {
6649
return values.groupBy({ it.substringBefore(delimiter) }, { it.substringAfter(delimiter) })
6750
}
6851

52+
private fun mapSingleValues(name: String, delimiter: String): Map<String, String> = values[name]
53+
?.associate {
54+
val splitted = it.split(delimiter)
55+
check(splitted.size == 2) { "Parameter name and value format is required for $name." }
56+
splitted[0] to splitted[1]
57+
} ?: emptyMap()
58+
6959
private fun listValues(name: String): List<String> {
7060
return this.values[name] ?: emptyList()
7161
}
@@ -74,12 +64,6 @@ class RunnerConfiguration(config: String) {
7464
"mode"
7565
) { it.toMode() }
7666

77-
val nativeFork = singleValueOrNull(
78-
"nativeFork"
79-
) { NativeFork.valueOf(it.replaceFirstChar { firstChar -> firstChar.uppercaseChar() }) }
80-
81-
val nativeGCAfterIteration = singleValueOrNull("nativeGCAfterIteration") { it.toBooleanStrict() }
82-
8367
override fun toString(): String {
8468
return """$name -> $reportFile ($traceFormat, $reportFormat)
8569
params: ${params.entries.joinToString(prefix = "{", postfix = "}") { "${it.key}: ${it.value}" }}
@@ -91,8 +75,7 @@ iterationTime: $iterationTime
9175
iterationTimeUnit: $iterationTimeUnit
9276
outputTimeUnit: $outputTimeUnit
9377
mode: $mode
94-
nativeFork: $nativeFork
95-
nativeGCAfterIteration: $nativeGCAfterIteration
78+
advanced: $advanced
9679
"""
9780
}
9881
}

runtime/jvmMain/src/kotlinx/benchmark/jvm/JvmBenchmarkRunner.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import java.io.*
1111
import java.lang.management.*
1212
import java.util.concurrent.*
1313

14-
1514
fun main(args: Array<String>) {
1615
val config = RunnerConfiguration(args[0].readFile())
1716

@@ -46,11 +45,11 @@ fun main(args: Array<String>) {
4645
if (jvmArgs.any { it.contains("libasyncProfiler") }) {
4746
jmhOptions.forks(0)
4847
} else {
49-
when (config.jvmForks) {
48+
when (val jvmForks = config.advanced["jvmForks"]) {
5049
null -> jmhOptions.forks(1)
5150
"definedByJmh" -> { /* do not override */ }
5251
else -> {
53-
val forks = config.jvmForks.toIntOrNull()?.takeIf { it >= 0 }
52+
val forks = jvmForks.toIntOrNull()?.takeIf { it >= 0 }
5453
?: throw IllegalArgumentException("jvmForks: expected a non-negative integer or \"definedByJmh\" string literal")
5554
jmhOptions.forks(forks)
5655
}

runtime/nativeMain/src/kotlinx/benchmark/NativeBenchmarkAnnotations.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ actual enum class Mode {
1717
Throughput, AverageTime
1818
}
1919

20+
enum class NativeFork {
21+
PerBenchmark, PerIteration
22+
}
23+
2024
@Target(AnnotationTarget.CLASS)
2125
actual annotation class OutputTimeUnit(actual val value: BenchmarkTimeUnit)
2226

runtime/nativeMain/src/kotlinx/benchmark/Utils.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ actual fun String.readFile(): String = buildString {
3838
val bufferLength = 64 * 1024
3939
val buffer = allocArray<ByteVar>(bufferLength)
4040
val line = fgets(buffer, bufferLength, file)?.toKString() // newline symbol is included
41-
if (line == null || line.isEmpty()) break
41+
if (line.isNullOrEmpty()) break
4242
append(line)
4343
}
4444
}
@@ -55,7 +55,7 @@ internal fun String.parseBenchmarkConfig(): NativeExecutor.BenchmarkRun {
5555

5656
val content = readFile()
5757
val lines = content.lines().filter { it.isNotEmpty() }
58-
require(lines.size == 3, { "Wrong format of detailed benchmark configuration file. "})
58+
require(lines.size == 3) { "Wrong format of detailed benchmark configuration file. " }
5959
val name = lines[0].getElement("benchmark")
6060
val configuration = BenchmarkConfiguration.parse(lines[1].getElement("configuration"))
6161
val parameters = lines[2].getElement("parameters").parseMap()

0 commit comments

Comments
 (0)