Skip to content

Commit 6fd0042

Browse files
committed
Initial work on params
1 parent 5522561 commit 6fd0042

File tree

8 files changed

+68
-36
lines changed

8 files changed

+68
-36
lines changed

examples/java/build.gradle

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ dependencies {
88
}
99

1010
benchmark {
11+
configurations {
12+
main {
13+
iterationTime = 300
14+
iterationTimeUnit = "ms"
15+
}
16+
singleParam {
17+
iterationTime = 300
18+
iterationTimeUnit = "ms"
19+
param("stringValue", "C", "D")
20+
param("intValue", 1, 10)
21+
}
22+
}
1123
targets {
1224
register("main") {
1325
jmhVersion = "1.21"
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
package test;
22

3-
import org.openjdk.jmh.annotations.Benchmark;
4-
import org.openjdk.jmh.annotations.Scope;
5-
import org.openjdk.jmh.annotations.State;
3+
import org.openjdk.jmh.annotations.*;
64

75
@State(Scope.Benchmark)
6+
@Fork(1)
87
public class SampleJavaBenchmark {
8+
@Param({"A", "B"})
9+
String stringValue;
10+
11+
@Param({"1", "2"})
12+
int intValue;
13+
914
@Benchmark
1015
public String stringBuilder() {
1116
StringBuilder stringBuilder = new StringBuilder();
1217
stringBuilder.append(10);
18+
stringBuilder.append(stringValue);
19+
stringBuilder.append(intValue);
1320
return stringBuilder.toString();
1421
}
1522
}

plugin/main/src/org/jetbrains/gradle/benchmarks/BenchmarkConfiguration.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.*
55

66
open class BenchmarkConfiguration(val extension: BenchmarksExtension, val name: String) {
77
var iterations: Int? = null
8+
var warmups: Int? = null
89
var iterationTime: Long? = null
910
var iterationTimeUnit: String? = null
1011
var mode: String? = null
1112
var outputTimeUnit: String? = null
12-
13+
1314
var includes: MutableList<String> = mutableListOf()
1415
var excludes: MutableList<String> = mutableListOf()
15-
var params: MutableMap<String, Any?> = mutableMapOf()
16+
var params: MutableMap<String, MutableList<Any?>> = mutableMapOf()
1617

1718
fun include(pattern: String) {
1819
includes.add(pattern)
@@ -22,8 +23,9 @@ open class BenchmarkConfiguration(val extension: BenchmarksExtension, val name:
2223
excludes.add(pattern)
2324
}
2425

25-
fun param(name: String, value: Any?) {
26-
params[name] = value
26+
fun param(name: String, vararg value: Any?) {
27+
val values = params.getOrPut(name) { mutableListOf() }
28+
values.addAll(value)
2729
}
2830

2931
fun capitalizedName() = if (name == "main") "" else name.capitalize()
@@ -34,11 +36,6 @@ open class BenchmarkTarget(val extension: BenchmarksExtension, val name: String)
3436
var workingDir: String? = null
3537
}
3638

37-
class BenchmarkConfigurationDefaults {
38-
var iterations = 10 // times
39-
var iterationTime = 1L // seconds
40-
}
41-
4239
abstract class JvmBenchmarkTarget(
4340
extension: BenchmarksExtension,
4441
name: String

plugin/main/src/org/jetbrains/gradle/benchmarks/JsNodeTasks.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ fun Project.createJsBenchmarkExecTask(
5454
arguments("-n", target.name)
5555
arguments("-r", reportFile.toString())
5656
config.iterations?.let { arguments("-i", it.toString()) }
57+
config.warmups?.let { arguments("-w", it.toString()) }
5758
config.iterationTime?.let { arguments("-it", it.toString()) }
5859
config.iterationTimeUnit?.let { arguments("-itu", it) }
5960
config.outputTimeUnit?.let { arguments("-otu", it) }
@@ -65,8 +66,8 @@ fun Project.createJsBenchmarkExecTask(
6566
config.excludes.forEach {
6667
arguments("-E", it)
6768
}
68-
config.params.forEach {
69-
arguments("-P", "\"${it.key}=${it.value}\"")
69+
config.params.forEach { (param, values) ->
70+
values.forEach { value -> arguments("-P", "\"$param=$value\"") }
7071
}
7172

7273
dependsOn("${target.name}${BenchmarksPlugin.BENCHMARK_DEPENDENCIES_SUFFIX}")

plugin/main/src/org/jetbrains/gradle/benchmarks/JvmTasks.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ fun Project.createJvmBenchmarkExecTask(
111111
args("-n", target.name)
112112
args("-r", reportFile.toString())
113113
config.iterations?.let { args("-i", it.toString()) }
114+
config.warmups?.let { args("-w", it.toString()) }
114115
config.iterationTime?.let { args("-it", it.toString()) }
115116
config.iterationTimeUnit?.let { args("-itu", it) }
116117
config.outputTimeUnit?.let { args("-otu", it) }
@@ -122,8 +123,8 @@ fun Project.createJvmBenchmarkExecTask(
122123
config.excludes.forEach {
123124
args("-E", it)
124125
}
125-
config.params.forEach {
126-
args("-P", "\"${it.key}=${it.value}\"")
126+
config.params.forEach { (param, values) ->
127+
values.forEach { value -> args("-P", "\"$param=$value\"") }
127128
}
128129

129130
dependsOn("${target.name}${BenchmarksPlugin.BENCHMARK_COMPILE_SUFFIX}")

plugin/main/src/org/jetbrains/gradle/benchmarks/NativeMultiplatformTasks.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ fun Project.createNativeBenchmarkExecTask(
115115
args("-n", target.name)
116116
args("-r", reportFile.toString())
117117
config.iterations?.let { args("-i", it.toString()) }
118+
config.warmups?.let { args("-w", it.toString()) }
118119
config.iterationTime?.let { args("-it", it.toString()) }
119120
config.iterationTimeUnit?.let { args("-itu", it) }
120121
config.outputTimeUnit?.let { args("-otu", it) }
@@ -126,8 +127,8 @@ fun Project.createNativeBenchmarkExecTask(
126127
config.excludes.forEach {
127128
args("-E", it)
128129
}
129-
config.params.forEach {
130-
args("-P", "\"${it.key}=${it.value}\"")
130+
config.params.forEach { (param, values) ->
131+
values.forEach { value -> args("-P", "\"$param=$value\"") }
131132
}
132133

133134
dependsOn(linkTask)

runtime/commonMain/src/org/jetbrains/gradle/benchmarks/RunnerConfiguration.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class RunnerConfiguration : CommandLineInterface("Client") {
1111
"-P",
1212
"<name>=<value>",
1313
"Specifies parameter with the given name and value"
14-
).toKeyValuePair().storeToMap()
14+
).toKeyValuePair().storeToMultimap()
1515

1616
val include by onFlagValue("-I", "<pattern>", "Include benchmarks matching the given pattern").storeToList()
1717
val exclude by onFlagValue("-E", "<pattern>", "Include benchmarks matching the given pattern").storeToList()
@@ -55,16 +55,16 @@ class RunnerConfiguration : CommandLineInterface("Client") {
5555

5656
private fun Event<String>.toKeyValuePair() =
5757
map {
58-
val parts = it.split('=', limit = 2)
58+
val parts = it.removeSurrounding("\"").split('=', limit = 2)
5959
if (parts.size == 1)
6060
parts[0] to ""
6161
else
6262
parts[0] to parts[1]
6363
}
6464

65-
private fun Event<Pair<String, String>>.storeToMap(): ArgumentValue<Map<String, String>> {
66-
val map = hashMapOf<String, String>()
67-
add { (key, value) -> map[key] = value }
65+
private fun Event<Pair<String, String>>.storeToMultimap(): ArgumentValue<Map<String, List<String>>> {
66+
val map = hashMapOf<String, MutableList<String>>()
67+
add { (key, value) -> map.getOrPut(key) { mutableListOf() }.add(value) }
6868
return ArgumentStorage(map)
6969
}
7070

@@ -82,7 +82,7 @@ class RunnerConfiguration : CommandLineInterface("Client") {
8282

8383
class LateInitArgumentStorage<T : Any>() : ArgumentValue<T> {
8484
private lateinit var value: T
85-
85+
8686
override fun getValue(thisRef: Any?, prop: Any?): T =
8787
value
8888

@@ -91,7 +91,6 @@ class RunnerConfiguration : CommandLineInterface("Client") {
9191
}
9292
}
9393

94-
9594
override fun toString(): String {
9695
return """$name -> $reportFile ($traceFormat)
9796
params: ${params.entries.joinToString(prefix = "{", postfix = "}") { "${it.key}: ${it.value}" }}

runtime/jvmMain/src/org/jetbrains/gradle/benchmarks/jvm/JvmBenchmarkRunner.kt

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@ fun main(args: Array<String>) {
2727
params.mode?.let {
2828
jmhOptions.mode(it)
2929
}
30-
31-
params.include.forEach {
30+
31+
params.include.forEach {
3232
jmhOptions.include(it)
3333
}
34-
params.exclude.forEach {
34+
params.exclude.forEach {
3535
jmhOptions.exclude(it)
3636
}
3737

38+
params.params.forEach { (key, value) ->
39+
jmhOptions.param(key, *value.toTypedArray())
40+
}
41+
3842
val reporter = BenchmarkProgress.create(params.traceFormat)
3943
val output = JmhOutputFormat(reporter, params.name)
4044
try {
@@ -55,9 +59,9 @@ fun main(args: Array<String>) {
5559

5660
class JmhOutputFormat(private val reporter: BenchmarkProgress, private val suiteName: String) :
5761
PrintOutputFormat(System.out) {
58-
62+
5963
internal var lastBenchmarkStart = ""
60-
64+
6165
override fun startRun() {
6266
reporter.startSuite(suiteName)
6367
}
@@ -67,22 +71,32 @@ class JmhOutputFormat(private val reporter: BenchmarkProgress, private val suite
6771
}
6872

6973
override fun startBenchmark(benchParams: BenchmarkParams) {
70-
val benchmarkFQN = benchParams.benchmark
71-
reporter.startBenchmark(suiteName, benchmarkFQN)
72-
lastBenchmarkStart = benchmarkFQN
74+
val benchmarkId = getBenchmarkId(benchParams)
75+
reporter.startBenchmark(suiteName, benchmarkId)
76+
lastBenchmarkStart = benchmarkId
7377
}
7478

7579
override fun endBenchmark(result: BenchmarkResult?) {
7680
if (result != null) {
77-
val benchmarkFQN = result.params.benchmark
81+
val benchmarkId = getBenchmarkId(result.params)
7882
val value = result.primaryResult
7983
val message = value.extendedInfo().trim()
80-
reporter.endBenchmark(suiteName, benchmarkFQN, BenchmarkProgress.FinishStatus.Success, message)
84+
reporter.endBenchmark(suiteName, benchmarkId, BenchmarkProgress.FinishStatus.Success, message)
8185
} else {
8286
reporter.endBenchmarkException(suiteName, lastBenchmarkStart, "<ERROR>", "")
8387
}
8488
}
8589

90+
private fun getBenchmarkId(params: BenchmarkParams): String {
91+
val benchmarkName = params.benchmark
92+
val paramKeys = params.paramsKeys
93+
val benchmarkId = if (paramKeys.isEmpty())
94+
benchmarkName
95+
else
96+
benchmarkName + paramKeys.joinToString(prefix = " | ") { "$it=${params.getParam(it)}" }
97+
return benchmarkId
98+
}
99+
86100
override fun iteration(benchParams: BenchmarkParams, params: IterationParams, iteration: Int) {}
87101

88102
override fun iterationResult(
@@ -98,7 +112,7 @@ class JmhOutputFormat(private val reporter: BenchmarkProgress, private val suite
98112
}
99113
flush()
100114
}
101-
115+
102116
override fun println(s: String) {
103117
if (!s.startsWith("#"))
104118
reporter.output(suiteName, lastBenchmarkStart, s)

0 commit comments

Comments
 (0)