@@ -10,7 +10,8 @@ fun Project.generateBenchmarkSourceFiles(
10
10
classDescriptors : List <ClassAnnotationsDescriptor >,
11
11
) {
12
12
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"
14
15
val androidTestDir = File (targetPath).resolve(" src/androidTest/kotlin" )
15
16
if (! androidTestDir.exists()) {
16
17
androidTestDir.mkdirs()
@@ -41,6 +42,7 @@ private fun generateDescriptorFile(descriptor: ClassAnnotationsDescriptor, andro
41
42
.addImport(" org.junit" , " Test" )
42
43
.addImport(" org.junit.runner" , " RunWith" )
43
44
.addImport(" androidx.benchmark.junit4" , " BenchmarkRule" )
45
+ .addImport(" androidx.benchmark.junit4" , " measureRepeated" )
44
46
45
47
val typeSpecBuilder = TypeSpec .classBuilder(descriptorName)
46
48
.addAnnotation(
@@ -83,13 +85,43 @@ private fun addBenchmarkMethods(typeSpecBuilder: TypeSpec.Builder, descriptor: C
83
85
method.annotations.any { annotation -> annotation.name == " kotlinx.benchmark.Param" }
84
86
}
85
87
.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
+ }
90
96
}
91
97
}
92
98
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
+
93
125
private fun updateAndroidDependencies (buildGradleFile : File , dependencies : List <Pair <String , String ?>>) {
94
126
if (buildGradleFile.exists()) {
95
127
val buildGradleContent = buildGradleFile.readText()
0 commit comments