@@ -40,26 +40,18 @@ private fun generateDescriptorFile(descriptor: ClassAnnotationsDescriptor, andro
40
40
val fileSpecBuilder = FileSpec .builder(packageName, descriptorName)
41
41
.addImport(" androidx.test.ext.junit.runners" , " AndroidJUnit4" )
42
42
.addImport(" org.junit" , " Test" )
43
+ .addImport(" org.junit" , " Before" )
44
+ .addImport(" org.junit" , " After" )
43
45
.addImport(" org.junit.runner" , " RunWith" )
44
- .addImport(" androidx.benchmark.junit4 " , " BenchmarkRule " )
45
- .addImport(" androidx.benchmark.junit4 " , " measureRepeated " )
46
+ .addImport(" androidx.benchmark" , " BenchmarkState " )
47
+ .addImport(" androidx.benchmark" , " ExperimentalBenchmarkStateApi " )
46
48
47
49
val typeSpecBuilder = TypeSpec .classBuilder(descriptorName)
48
50
.addAnnotation(
49
51
AnnotationSpec .builder(ClassName (" org.junit.runner" , " RunWith" ))
50
52
.addMember(" %T::class" , ClassName (" androidx.test.ext.junit.runners" , " AndroidJUnit4" ))
51
53
.build()
52
54
)
53
- .addProperty(
54
- PropertySpec .builder(" benchmarkRule" , ClassName (" androidx.benchmark.junit4" , " BenchmarkRule" ))
55
- .addAnnotation(
56
- AnnotationSpec .builder(ClassName (" org.junit" , " Rule" ))
57
- .useSiteTarget(AnnotationSpec .UseSiteTarget .GET )
58
- .build()
59
- )
60
- .initializer(" BenchmarkRule()" )
61
- .build()
62
- )
63
55
64
56
addBenchmarkMethods(typeSpecBuilder, descriptor)
65
57
@@ -102,11 +94,22 @@ private fun generateMeasurableMethod(
102
94
propertyName : String ,
103
95
typeSpecBuilder : TypeSpec .Builder
104
96
) {
97
+
98
+ val measurementAnnotation = descriptor.annotations.find { it.name == " kotlinx.benchmark.Measurement" }
99
+ val iterations = measurementAnnotation?.parameters?.get(" iterations" ) as ? Int ? : 5
100
+
105
101
val methodSpecBuilder = FunSpec .builder(" benchmark_${descriptor.name} _${method.name} " )
106
102
.addAnnotation(ClassName (" org.junit" , " Test" ))
107
- .addStatement(" benchmarkRule.measureRepeated {" )
108
- .addStatement(" $propertyName .${method.name} ()" )
109
- .addStatement(" }" )
103
+ .addAnnotation(
104
+ AnnotationSpec .builder(ClassName (" kotlin" , " OptIn" ))
105
+ .addMember(" %T::class" , ClassName (" androidx.benchmark" , " ExperimentalBenchmarkStateApi" ))
106
+ .build()
107
+ )
108
+ // TODO: Add warmupCount and repeatCount parameters
109
+ .addStatement(" val state = %T(warmupCount = 5, repeatCount = $iterations )" , ClassName (" androidx.benchmark" , " BenchmarkState" ))
110
+ .beginControlFlow(" while (state.keepRunning())" )
111
+ .addStatement(" $propertyName .${method.name} ()" )
112
+ .endControlFlow()
110
113
typeSpecBuilder.addFunction(methodSpecBuilder.build())
111
114
}
112
115
@@ -116,10 +119,20 @@ private fun generateNonMeasurableMethod(
116
119
propertyName : String ,
117
120
typeSpecBuilder : TypeSpec .Builder
118
121
) {
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())
122
+ when (method.annotations.first().name) {
123
+ " kotlinx.benchmark.Setup" -> {
124
+ val methodSpecBuilder = FunSpec .builder(" benchmark_${descriptor.name} _setUp" )
125
+ .addAnnotation(ClassName (" org.junit" , " Before" ))
126
+ .addStatement(" $propertyName .${method.name} ()" )
127
+ typeSpecBuilder.addFunction(methodSpecBuilder.build())
128
+ }
129
+ " kotlinx.benchmark.TearDown" -> {
130
+ val methodSpecBuilder = FunSpec .builder(" benchmark_${descriptor.name} _tearDown" )
131
+ .addAnnotation(ClassName (" org.junit" , " After" ))
132
+ .addStatement(" $propertyName .${method.name} ()" )
133
+ typeSpecBuilder.addFunction(methodSpecBuilder.build())
134
+ }
135
+ }
123
136
}
124
137
125
138
private fun updateAndroidDependencies (buildGradleFile : File , dependencies : List <Pair <String , String ?>>) {
0 commit comments