@@ -39,6 +39,7 @@ private fun generateDescriptorFile(descriptor: ClassAnnotationsDescriptor, andro
3939 .addImport(" org.junit.runner" , " RunWith" )
4040 .addImport(" androidx.benchmark" , " BenchmarkState" )
4141 .addImport(" androidx.benchmark" , " ExperimentalBenchmarkStateApi" )
42+ .addImport(" android.util" , " Log" )
4243
4344 if (descriptor.hasSetupOrTeardownMethods()) {
4445 fileSpecBuilder
@@ -68,19 +69,14 @@ private fun generateParameterizedDescriptorFile(descriptor: ClassAnnotationsDesc
6869 .addImport(" androidx.benchmark" , " BenchmarkState" )
6970 .addImport(" androidx.benchmark" , " ExperimentalBenchmarkStateApi" )
7071 .addImport(" org.junit" , " Test" )
72+ .addImport(" android.util" , " Log" )
7173
7274 if (descriptor.hasSetupOrTeardownMethods()) {
7375 fileSpecBuilder
7476 .addImport(" org.junit" , " Before" )
7577 .addImport(" org.junit" , " After" )
7678 }
7779
78- fileSpecBuilder.addAnnotation(
79- AnnotationSpec .builder(ClassName (" org.junit.runner" , " RunWith" ))
80- .addMember(" %T::class" , ClassName (" org.junit.runners" , " Parameterized" ))
81- .build()
82- )
83-
8480 // Generate constructor
8581 val constructorSpec = FunSpec .constructorBuilder()
8682 val paramFields = descriptor.getSpecificField(paramAnnotationFQN)
@@ -89,6 +85,11 @@ private fun generateParameterizedDescriptorFile(descriptor: ClassAnnotationsDesc
8985 }
9086
9187 val typeSpecBuilder = TypeSpec .classBuilder(descriptorName)
88+ .addAnnotation(
89+ AnnotationSpec .builder(ClassName (" org.junit.runner" , " RunWith" ))
90+ .addMember(" %T::class" , ClassName (" org.junit.runners" , " Parameterized" ))
91+ .build()
92+ )
9293 .primaryConstructor(constructorSpec.build())
9394 .addProperties(paramFields.map { param ->
9495 PropertySpec .builder(param.name, getTypeName(param.type))
@@ -114,7 +115,7 @@ private fun generateParametersFunction(paramFields: List<FieldAnnotationsDescrip
114115 val dataFunctionBuilder = FunSpec .builder(" data" )
115116 .addAnnotation(JvmStatic ::class )
116117 .returns(
117- ClassName (" java.util " , " Collection" )
118+ ClassName (" kotlin.collections " , " Collection" )
118119 .parameterizedBy(
119120 ClassName (" kotlin" , " Array" )
120121 .parameterizedBy(ANY )
@@ -222,8 +223,6 @@ private fun generateCommonMeasurableMethod(
222223 .find { it.name == warmupAnnotationFQN }
223224 ?.parameters?.get(" iterations" ) as ? Int ? : 5
224225
225- val methodName = " ${descriptor.packageName} .${descriptor.name} .${method.name} "
226-
227226 val methodSpecBuilder = FunSpec .builder(" benchmark_${descriptor.name} _${method.name} " )
228227 .addAnnotation(ClassName (" org.junit" , " Test" ))
229228 .addAnnotation(
@@ -243,13 +242,12 @@ private fun generateCommonMeasurableMethod(
243242 " val state = %T(warmupCount = $warmupIterations , repeatCount = $measurementIterations )" ,
244243 ClassName (" androidx.benchmark" , " BenchmarkState" )
245244 )
246- .addStatement(" println(\" Android: $methodName \" )" )
247245 .beginControlFlow(" while (state.keepRunning())" )
248246 .addStatement(" $propertyName .${method.name} ()" )
249247 .endControlFlow()
250248 .addStatement(" val measurementResult = state.getMeasurementTimeNs()" )
251249 .beginControlFlow(" measurementResult.forEachIndexed { index, time ->" )
252- .addStatement(" println( \" Iteration \$ {index + 1}: \$ time ns\" )" )
250+ .addStatement(" Log.d( \" KotlinBenchmark \" , \" Iteration \$ {index + 1}: \$ time ns\" )" )
253251 .endControlFlow()
254252
255253 typeSpecBuilder.addFunction(methodSpecBuilder.build())
@@ -307,6 +305,7 @@ private fun getTypeName(type: String): TypeName {
307305 " char" -> Char ::class .asTypeName()
308306 " byte" -> Byte ::class .asTypeName()
309307 " short" -> Short ::class .asTypeName()
308+ " java.lang.String" -> String ::class .asTypeName()
310309 else -> ClassName .bestGuess(type)
311310 }
312311}
0 commit comments