@@ -40,26 +40,18 @@ private fun generateDescriptorFile(descriptor: ClassAnnotationsDescriptor, andro
4040 val fileSpecBuilder = FileSpec .builder(packageName, descriptorName)
4141 .addImport(" androidx.test.ext.junit.runners" , " AndroidJUnit4" )
4242 .addImport(" org.junit" , " Test" )
43+ .addImport(" org.junit" , " Before" )
44+ .addImport(" org.junit" , " After" )
4345 .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 " )
4648
4749 val typeSpecBuilder = TypeSpec .classBuilder(descriptorName)
4850 .addAnnotation(
4951 AnnotationSpec .builder(ClassName (" org.junit.runner" , " RunWith" ))
5052 .addMember(" %T::class" , ClassName (" androidx.test.ext.junit.runners" , " AndroidJUnit4" ))
5153 .build()
5254 )
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- )
6355
6456 addBenchmarkMethods(typeSpecBuilder, descriptor)
6557
@@ -102,11 +94,22 @@ private fun generateMeasurableMethod(
10294 propertyName : String ,
10395 typeSpecBuilder : TypeSpec .Builder
10496) {
97+
98+ val measurementAnnotation = descriptor.annotations.find { it.name == " kotlinx.benchmark.Measurement" }
99+ val iterations = measurementAnnotation?.parameters?.get(" iterations" ) as ? Int ? : 5
100+
105101 val methodSpecBuilder = FunSpec .builder(" benchmark_${descriptor.name} _${method.name} " )
106102 .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()
110113 typeSpecBuilder.addFunction(methodSpecBuilder.build())
111114}
112115
@@ -116,10 +119,20 @@ private fun generateNonMeasurableMethod(
116119 propertyName : String ,
117120 typeSpecBuilder : TypeSpec .Builder
118121) {
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+ }
123136}
124137
125138private fun updateAndroidDependencies (buildGradleFile : File , dependencies : List <Pair <String , String ?>>) {
0 commit comments