Skip to content

Commit 57b6e64

Browse files
committed
Use retrieved iteration data to generate file for android target
1 parent b21b5d7 commit 57b6e64

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

examples/kotlin-multiplatform/src/androidMain/kotlin/AndroidTestBenchmark.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import kotlinx.benchmark.*
44
import kotlin.math.*
55

66
@State(Scope.Benchmark)
7+
@Warmup(iterations = 3, time = 1, timeUnit = BenchmarkTimeUnit.SECONDS)
8+
@Measurement(iterations = 3, time = 1, timeUnit = BenchmarkTimeUnit.SECONDS)
79
class AndroidTestBenchmark {
810
private var data = 0.0
911

examples/kotlin-multiplatform/src/commonMain/kotlin/CommonBenchmark.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import kotlinx.benchmark.*
44
import kotlin.math.*
55

66
@State(Scope.Benchmark)
7+
@Warmup(iterations = 3, time = 1, timeUnit = BenchmarkTimeUnit.SECONDS)
78
@Measurement(iterations = 3, time = 1, timeUnit = BenchmarkTimeUnit.SECONDS)
89
@OutputTimeUnit(BenchmarkTimeUnit.MILLISECONDS)
910
@BenchmarkMode(Mode.AverageTime)

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ private fun addBenchmarkMethods(typeSpecBuilder: TypeSpec.Builder, descriptor: C
8181
method.annotations.any { it.name == "kotlinx.benchmark.Setup" || it.name == "kotlinx.benchmark.TearDown" } -> {
8282
generateNonMeasurableMethod(descriptor, method, propertyName, typeSpecBuilder)
8383
}
84+
8485
else -> {
8586
generateMeasurableMethod(descriptor, method, propertyName, typeSpecBuilder)
8687
}
@@ -95,8 +96,12 @@ private fun generateMeasurableMethod(
9596
typeSpecBuilder: TypeSpec.Builder
9697
) {
9798

98-
val measurementAnnotation = descriptor.annotations.find { it.name == "kotlinx.benchmark.Measurement" }
99-
val iterations = measurementAnnotation?.parameters?.get("iterations") as? Int ?: 5
99+
val measurementIterations = descriptor.annotations
100+
.find { it.name == "kotlinx.benchmark.Measurement" }
101+
?.parameters?.get("iterations") as? Int ?: 5
102+
val warmupIterations = descriptor.annotations
103+
.find { it.name == "kotlinx.benchmark.Warmup" }
104+
?.parameters?.get("iterations") as? Int ?: 5
100105

101106
val methodSpecBuilder = FunSpec.builder("benchmark_${descriptor.name}_${method.name}")
102107
.addAnnotation(ClassName("org.junit", "Test"))
@@ -106,7 +111,10 @@ private fun generateMeasurableMethod(
106111
.build()
107112
)
108113
// TODO: Add warmupCount and repeatCount parameters
109-
.addStatement("val state = %T(warmupCount = 5, repeatCount = $iterations)", ClassName("androidx.benchmark", "BenchmarkState"))
114+
.addStatement(
115+
"val state = %T(warmupCount = $warmupIterations, repeatCount = $measurementIterations)",
116+
ClassName("androidx.benchmark", "BenchmarkState")
117+
)
110118
.beginControlFlow("while (state.keepRunning())")
111119
.addStatement("$propertyName.${method.name}()")
112120
.endControlFlow()
@@ -126,6 +134,7 @@ private fun generateNonMeasurableMethod(
126134
.addStatement("$propertyName.${method.name}()")
127135
typeSpecBuilder.addFunction(methodSpecBuilder.build())
128136
}
137+
129138
"kotlinx.benchmark.TearDown" -> {
130139
val methodSpecBuilder = FunSpec.builder("benchmark_${descriptor.name}_tearDown")
131140
.addAnnotation(ClassName("org.junit", "After"))
@@ -152,10 +161,12 @@ private fun updateAndroidDependencies(buildGradleFile: File, dependencies: List<
152161
val updatedAndroidBlockContent = if (androidBlockContent.contains("dependencies {")) {
153162
val dependenciesBlockStart = androidBlockContent.indexOf("dependencies {")
154163
val dependenciesBlockEnd = androidBlockContent.indexOf("}", dependenciesBlockStart) + 1
155-
val dependenciesBlockContent = androidBlockContent.substring(dependenciesBlockStart, dependenciesBlockEnd)
164+
val dependenciesBlockContent =
165+
androidBlockContent.substring(dependenciesBlockStart, dependenciesBlockEnd)
156166

157167
val newDependenciesString = newDependencies.joinToString("\n ") { (dependency, version) ->
158-
version?.let { """androidTestImplementation("$dependency:$version")""" } ?: """androidTestImplementation(files("$dependency"))"""
168+
version?.let { """androidTestImplementation("$dependency:$version")""" }
169+
?: """androidTestImplementation(files("$dependency"))"""
159170
}
160171
androidBlockContent.replace(
161172
dependenciesBlockContent,
@@ -166,12 +177,14 @@ private fun updateAndroidDependencies(buildGradleFile: File, dependencies: List<
166177
)
167178
} else {
168179
val newDependenciesString = newDependencies.joinToString("\n ") { (dependency, version) ->
169-
version?.let { """androidTestImplementation("$dependency:$version")""" } ?: """androidTestImplementation(files("$dependency"))"""
180+
version?.let { """androidTestImplementation("$dependency:$version")""" }
181+
?: """androidTestImplementation(files("$dependency"))"""
170182
}
171183
androidBlockContent.replace("{", "{\n dependencies {\n $newDependenciesString\n }\n")
172184
}
173185

174-
val updatedBuildGradleContent = buildGradleContent.replace(androidBlockContent, updatedAndroidBlockContent)
186+
val updatedBuildGradleContent =
187+
buildGradleContent.replace(androidBlockContent, updatedAndroidBlockContent)
175188
buildGradleFile.writeText(updatedBuildGradleContent)
176189
}
177190
}

0 commit comments

Comments
 (0)