Support dumping stats for number of generated sources by annotation processors

^KT-51132 In Progress
This commit is contained in:
Udi Cohen
2022-02-02 15:59:44 -05:00
committed by teamcity
parent 621f14c5bb
commit eb3b9f148d
@@ -152,6 +152,10 @@ private fun dumpProcessorTiming(wrappedProcessors: List<ProcessorWrapper>, apRep
wrappedProcessors.forEach { processor ->
appendLine(processor.renderSpentTime())
}
appendLine("Generated files report:")
wrappedProcessors.forEach { processor ->
appendLine(processor.renderGenerations())
}
})
}
@@ -182,12 +186,14 @@ private fun reportIfRunningNonIncrementally(
private class ProcessorWrapper(private val delegate: IncrementalProcessor) : Processor by delegate {
private var initTime: Long = 0
private val roundTime = mutableListOf<Long>()
private val sourcesGenerated = mutableListOf<Int>()
override fun process(annotations: MutableSet<out TypeElement>, roundEnv: RoundEnvironment): Boolean {
val (time, result) = measureTimeMillisWithResult {
delegate.process(annotations, roundEnv)
}
updateGenerationStats(roundEnv)
roundTime += time
return result
}
@@ -225,6 +231,38 @@ private class ProcessorWrapper(private val delegate: IncrementalProcessor) : Pro
"init: $initTime ms, " +
"${roundTime.size} round(s): ${roundTime.joinToString { "$it ms" }}"
}
fun renderGenerations(): String {
val processorName = delegate.processorName
return "$processorName: " +
"total sources: ${sourcesGenerated.sum()}, " +
"sources per round: ${sourcesGenerated.joinToString()}"
}
private fun updateGenerationStats(roundEnv: RoundEnvironment) {
var numSourcesGenerated = -1
try {
val procEnv = roundEnv::class.java.getDeclaredField("processingEnv")
procEnv.isAccessible = true
val proEnvObj = procEnv.get(roundEnv) as ProcessingEnvironment
val filerField = JavacProcessingEnvironment::class.java.getDeclaredField("filer")
filerField.isAccessible = true
val filerObj = filerField.get(proEnvObj)
val genSourceNameField = JavacFiler::class.java.getDeclaredField("generatedSourceNames")
genSourceNameField.isAccessible = true
val genSourceNameObj = genSourceNameField.get(filerObj)
@Suppress("UNCHECKED_CAST")
val sources: Set<String>? = genSourceNameObj as? Set<String>
numSourcesGenerated = sources?.size ?: -1
} catch (e: Exception) {
// Not much we can do
}
sourcesGenerated.add(numSourcesGenerated)
}
}
fun KaptContext.parseJavaFiles(javaSourceFiles: List<File>): JavacList<JCTree.JCCompilationUnit> {