@@ -39,6 +39,7 @@ private fun generateDescriptorFile(descriptor: ClassAnnotationsDescriptor, andro
39
39
.addImport(" org.junit.runner" , " RunWith" )
40
40
.addImport(" androidx.benchmark" , " BenchmarkState" )
41
41
.addImport(" androidx.benchmark" , " ExperimentalBenchmarkStateApi" )
42
+ .addImport(" android.util" , " Log" )
42
43
43
44
if (descriptor.hasSetupOrTeardownMethods()) {
44
45
fileSpecBuilder
@@ -68,19 +69,14 @@ private fun generateParameterizedDescriptorFile(descriptor: ClassAnnotationsDesc
68
69
.addImport(" androidx.benchmark" , " BenchmarkState" )
69
70
.addImport(" androidx.benchmark" , " ExperimentalBenchmarkStateApi" )
70
71
.addImport(" org.junit" , " Test" )
72
+ .addImport(" android.util" , " Log" )
71
73
72
74
if (descriptor.hasSetupOrTeardownMethods()) {
73
75
fileSpecBuilder
74
76
.addImport(" org.junit" , " Before" )
75
77
.addImport(" org.junit" , " After" )
76
78
}
77
79
78
- fileSpecBuilder.addAnnotation(
79
- AnnotationSpec .builder(ClassName (" org.junit.runner" , " RunWith" ))
80
- .addMember(" %T::class" , ClassName (" org.junit.runners" , " Parameterized" ))
81
- .build()
82
- )
83
-
84
80
// Generate constructor
85
81
val constructorSpec = FunSpec .constructorBuilder()
86
82
val paramFields = descriptor.getSpecificField(paramAnnotationFQN)
@@ -89,6 +85,11 @@ private fun generateParameterizedDescriptorFile(descriptor: ClassAnnotationsDesc
89
85
}
90
86
91
87
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
+ )
92
93
.primaryConstructor(constructorSpec.build())
93
94
.addProperties(paramFields.map { param ->
94
95
PropertySpec .builder(param.name, getTypeName(param.type))
@@ -114,7 +115,7 @@ private fun generateParametersFunction(paramFields: List<FieldAnnotationsDescrip
114
115
val dataFunctionBuilder = FunSpec .builder(" data" )
115
116
.addAnnotation(JvmStatic ::class )
116
117
.returns(
117
- ClassName (" java.util " , " Collection" )
118
+ ClassName (" kotlin.collections " , " Collection" )
118
119
.parameterizedBy(
119
120
ClassName (" kotlin" , " Array" )
120
121
.parameterizedBy(ANY )
@@ -222,8 +223,6 @@ private fun generateCommonMeasurableMethod(
222
223
.find { it.name == warmupAnnotationFQN }
223
224
?.parameters?.get(" iterations" ) as ? Int ? : 5
224
225
225
- val methodName = " ${descriptor.packageName} .${descriptor.name} .${method.name} "
226
-
227
226
val methodSpecBuilder = FunSpec .builder(" benchmark_${descriptor.name} _${method.name} " )
228
227
.addAnnotation(ClassName (" org.junit" , " Test" ))
229
228
.addAnnotation(
@@ -243,13 +242,12 @@ private fun generateCommonMeasurableMethod(
243
242
" val state = %T(warmupCount = $warmupIterations , repeatCount = $measurementIterations )" ,
244
243
ClassName (" androidx.benchmark" , " BenchmarkState" )
245
244
)
246
- .addStatement(" println(\" Android: $methodName \" )" )
247
245
.beginControlFlow(" while (state.keepRunning())" )
248
246
.addStatement(" $propertyName .${method.name} ()" )
249
247
.endControlFlow()
250
248
.addStatement(" val measurementResult = state.getMeasurementTimeNs()" )
251
249
.beginControlFlow(" measurementResult.forEachIndexed { index, time ->" )
252
- .addStatement(" println( \" Iteration \$ {index + 1}: \$ time ns\" )" )
250
+ .addStatement(" Log.d( \" KotlinBenchmark \" , \" Iteration \$ {index + 1}: \$ time ns\" )" )
253
251
.endControlFlow()
254
252
255
253
typeSpecBuilder.addFunction(methodSpecBuilder.build())
@@ -307,6 +305,7 @@ private fun getTypeName(type: String): TypeName {
307
305
" char" -> Char ::class .asTypeName()
308
306
" byte" -> Byte ::class .asTypeName()
309
307
" short" -> Short ::class .asTypeName()
308
+ " java.lang.String" -> String ::class .asTypeName()
310
309
else -> ClassName .bestGuess(type)
311
310
}
312
311
}
0 commit comments