Skip to content

Commit

Permalink
Fix 'Type mismatch' error
Browse files Browse the repository at this point in the history
  • Loading branch information
kx412764776 committed Aug 25, 2024
1 parent 5828e33 commit 0583e41
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,8 @@ private fun Project.createAndroidBenchmarkExecTask(target: AndroidBenchmarkTarge
.redirectError(ProcessBuilder.Redirect.PIPE)
.start()

val outputGobbler = StreamGobbler(process.inputStream) { line ->
if (line.contains("Iteration") || line.contains("run finished")) {
println(line)
}
}

val errorGobbler = StreamGobbler(process.errorStream) { System.err.println(it) }
val outputGobbler = StreamGobbler(process.inputStream) { }
val errorGobbler = StreamGobbler(process.errorStream) { }

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

private fun captureLogcatOutput() {
try {
val logcatProcess = ProcessBuilder("adb", "logcat", "-v", "time")
val logcatProcess = ProcessBuilder("adb", "logcat", "TestRunner:D", "KotlinBenchmark:D", "*:S")
.redirectErrorStream(true)
.start()

val logcatGobbler = StreamGobbler(logcatProcess.inputStream) { line ->
when {
line.contains("started") ->
println(
"Android: " +
line.substringAfter("started: ")
.substringBefore("(")
.replace(Regex("\\[\\d+: "), "[")
)

line.contains("Iteration") -> println(line.substring(line.indexOf("Iteration")))
line.contains("run finished") -> println(line.substring(line.indexOf("run finished")))
line.contains("finished") -> println()
}
}

Expand Down
21 changes: 10 additions & 11 deletions plugin/main/src/kotlinx/benchmark/gradle/AndroidSourceGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ private fun generateDescriptorFile(descriptor: ClassAnnotationsDescriptor, andro
.addImport("org.junit.runner", "RunWith")
.addImport("androidx.benchmark", "BenchmarkState")
.addImport("androidx.benchmark", "ExperimentalBenchmarkStateApi")
.addImport("android.util", "Log")

if (descriptor.hasSetupOrTeardownMethods()) {
fileSpecBuilder
Expand Down Expand Up @@ -68,19 +69,14 @@ private fun generateParameterizedDescriptorFile(descriptor: ClassAnnotationsDesc
.addImport("androidx.benchmark", "BenchmarkState")
.addImport("androidx.benchmark", "ExperimentalBenchmarkStateApi")
.addImport("org.junit", "Test")
.addImport("android.util", "Log")

if (descriptor.hasSetupOrTeardownMethods()) {
fileSpecBuilder
.addImport("org.junit", "Before")
.addImport("org.junit", "After")
}

fileSpecBuilder.addAnnotation(
AnnotationSpec.builder(ClassName("org.junit.runner", "RunWith"))
.addMember("%T::class", ClassName("org.junit.runners", "Parameterized"))
.build()
)

// Generate constructor
val constructorSpec = FunSpec.constructorBuilder()
val paramFields = descriptor.getSpecificField(paramAnnotationFQN)
Expand All @@ -89,6 +85,11 @@ private fun generateParameterizedDescriptorFile(descriptor: ClassAnnotationsDesc
}

val typeSpecBuilder = TypeSpec.classBuilder(descriptorName)
.addAnnotation(
AnnotationSpec.builder(ClassName("org.junit.runner", "RunWith"))
.addMember("%T::class", ClassName("org.junit.runners", "Parameterized"))
.build()
)
.primaryConstructor(constructorSpec.build())
.addProperties(paramFields.map { param ->
PropertySpec.builder(param.name, getTypeName(param.type))
Expand All @@ -114,7 +115,7 @@ private fun generateParametersFunction(paramFields: List<FieldAnnotationsDescrip
val dataFunctionBuilder = FunSpec.builder("data")
.addAnnotation(JvmStatic::class)
.returns(
ClassName("java.util", "Collection")
ClassName("kotlin.collections", "Collection")
.parameterizedBy(
ClassName("kotlin", "Array")
.parameterizedBy(ANY)
Expand Down Expand Up @@ -222,8 +223,6 @@ private fun generateCommonMeasurableMethod(
.find { it.name == warmupAnnotationFQN }
?.parameters?.get("iterations") as? Int ?: 5

val methodName = "${descriptor.packageName}.${descriptor.name}.${method.name}"

val methodSpecBuilder = FunSpec.builder("benchmark_${descriptor.name}_${method.name}")
.addAnnotation(ClassName("org.junit", "Test"))
.addAnnotation(
Expand All @@ -243,13 +242,12 @@ private fun generateCommonMeasurableMethod(
"val state = %T(warmupCount = $warmupIterations, repeatCount = $measurementIterations)",
ClassName("androidx.benchmark", "BenchmarkState")
)
.addStatement("println(\"Android: $methodName\")")
.beginControlFlow("while (state.keepRunning())")
.addStatement("$propertyName.${method.name}()")
.endControlFlow()
.addStatement("val measurementResult = state.getMeasurementTimeNs()")
.beginControlFlow("measurementResult.forEachIndexed { index, time ->")
.addStatement("println(\"Iteration \${index + 1}: \$time ns\")")
.addStatement("Log.d(\"KotlinBenchmark\", \"Iteration \${index + 1}: \$time ns\")")
.endControlFlow()

typeSpecBuilder.addFunction(methodSpecBuilder.build())
Expand Down Expand Up @@ -307,6 +305,7 @@ private fun getTypeName(type: String): TypeName {
"char" -> Char::class.asTypeName()
"byte" -> Byte::class.asTypeName()
"short" -> Short::class.asTypeName()
"java.lang.String" -> String::class.asTypeName()
else -> ClassName.bestGuess(type)
}
}

0 comments on commit 0583e41

Please sign in to comment.