Skip to content

Commit 80ea397

Browse files
committed
Fix 'Type mismatch' error
1 parent 66252c6 commit 80ea397

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

plugin/main/src/kotlinx/benchmark/gradle/AndroidMultiplatformTasks.kt

+12-8
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,8 @@ private fun Project.createAndroidBenchmarkExecTask(target: AndroidBenchmarkTarge
155155
.redirectError(ProcessBuilder.Redirect.PIPE)
156156
.start()
157157

158-
val outputGobbler = StreamGobbler(process.inputStream) { line ->
159-
if (line.contains("Iteration") || line.contains("run finished")) {
160-
println(line)
161-
}
162-
}
163-
164-
val errorGobbler = StreamGobbler(process.errorStream) { System.err.println(it) }
158+
val outputGobbler = StreamGobbler(process.inputStream) { }
159+
val errorGobbler = StreamGobbler(process.errorStream) { }
165160

166161
outputGobbler.start()
167162
errorGobbler.start()
@@ -184,14 +179,23 @@ private fun Project.createAndroidBenchmarkExecTask(target: AndroidBenchmarkTarge
184179

185180
private fun captureLogcatOutput() {
186181
try {
187-
val logcatProcess = ProcessBuilder("adb", "logcat", "-v", "time")
182+
val logcatProcess = ProcessBuilder("adb", "logcat", "TestRunner:D", "KotlinBenchmark:D", "*:S")
188183
.redirectErrorStream(true)
189184
.start()
190185

191186
val logcatGobbler = StreamGobbler(logcatProcess.inputStream) { line ->
192187
when {
188+
line.contains("started") ->
189+
println(
190+
"Android: " +
191+
line.substringAfter("started: ")
192+
.substringBefore("(")
193+
.replace(Regex("\\[\\d+: "), "[")
194+
)
195+
193196
line.contains("Iteration") -> println(line.substring(line.indexOf("Iteration")))
194197
line.contains("run finished") -> println(line.substring(line.indexOf("run finished")))
198+
line.contains("finished") -> println()
195199
}
196200
}
197201

plugin/main/src/kotlinx/benchmark/gradle/AndroidSourceGenerator.kt

+10-11
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)