Skip to content

Commit 76e5fc1

Browse files
committed
Add 'benchmarkRule' to call test
1 parent 98d5e13 commit 76e5fc1

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

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

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ fun Project.generateBenchmarkSourceFiles(
1010
classDescriptors: List<ClassAnnotationsDescriptor>,
1111
) {
1212

13-
val targetPath = "E:/Android/AndroidProjects/kotlin-qualification-task/composeApp"
13+
// TODO: Path needs to generate files
14+
val targetPath = "E:/Android/AndroidProjects/kotlin-qualification-task/microbenchmark"
1415
val androidTestDir = File(targetPath).resolve("src/androidTest/kotlin")
1516
if (!androidTestDir.exists()) {
1617
androidTestDir.mkdirs()
@@ -41,6 +42,7 @@ private fun generateDescriptorFile(descriptor: ClassAnnotationsDescriptor, andro
4142
.addImport("org.junit", "Test")
4243
.addImport("org.junit.runner", "RunWith")
4344
.addImport("androidx.benchmark.junit4", "BenchmarkRule")
45+
.addImport("androidx.benchmark.junit4", "measureRepeated")
4446

4547
val typeSpecBuilder = TypeSpec.classBuilder(descriptorName)
4648
.addAnnotation(
@@ -83,13 +85,43 @@ private fun addBenchmarkMethods(typeSpecBuilder: TypeSpec.Builder, descriptor: C
8385
method.annotations.any { annotation -> annotation.name == "kotlinx.benchmark.Param" }
8486
}
8587
.forEach { method ->
86-
val methodSpecBuilder = FunSpec.builder("benchmark_${descriptor.name}_${method.name}")
87-
.addAnnotation(ClassName("org.junit", "Test"))
88-
.addStatement("$propertyName.${method.name}()")
89-
typeSpecBuilder.addFunction(methodSpecBuilder.build())
88+
when {
89+
method.annotations.any { it.name == "kotlinx.benchmark.Setup" || it.name == "kotlinx.benchmark.TearDown" } -> {
90+
generateNonMeasurableMethod(descriptor, method, propertyName, typeSpecBuilder)
91+
}
92+
else -> {
93+
generateMeasurableMethod(descriptor, method, propertyName, typeSpecBuilder)
94+
}
95+
}
9096
}
9197
}
9298

99+
private fun generateMeasurableMethod(
100+
descriptor: ClassAnnotationsDescriptor,
101+
method: MethodAnnotationsDescriptor,
102+
propertyName: String,
103+
typeSpecBuilder: TypeSpec.Builder
104+
) {
105+
val methodSpecBuilder = FunSpec.builder("benchmark_${descriptor.name}_${method.name}")
106+
.addAnnotation(ClassName("org.junit", "Test"))
107+
.addStatement("benchmarkRule.measureRepeated {")
108+
.addStatement(" $propertyName.${method.name}()")
109+
.addStatement("}")
110+
typeSpecBuilder.addFunction(methodSpecBuilder.build())
111+
}
112+
113+
private fun generateNonMeasurableMethod(
114+
descriptor: ClassAnnotationsDescriptor,
115+
method: MethodAnnotationsDescriptor,
116+
propertyName: String,
117+
typeSpecBuilder: TypeSpec.Builder
118+
) {
119+
val methodSpecBuilder = FunSpec.builder("benchmark_${descriptor.name}_${method.name}")
120+
.addAnnotation(ClassName("org.junit", "Test"))
121+
.addStatement("$propertyName.${method.name}()")
122+
typeSpecBuilder.addFunction(methodSpecBuilder.build())
123+
}
124+
93125
private fun updateAndroidDependencies(buildGradleFile: File, dependencies: List<Pair<String, String?>>) {
94126
if (buildGradleFile.exists()) {
95127
val buildGradleContent = buildGradleFile.readText()

0 commit comments

Comments
 (0)