Kapt: Log annotation processor initialization time

This commit is contained in:
Yan Zhulanow
2018-10-16 22:37:01 +03:00
parent 6d017f07ad
commit 0ae5d1c08f
@@ -15,10 +15,13 @@ import org.jetbrains.kotlin.kapt3.base.util.KaptBaseError
import org.jetbrains.kotlin.kapt3.base.util.isJava9OrLater import org.jetbrains.kotlin.kapt3.base.util.isJava9OrLater
import org.jetbrains.kotlin.kapt3.base.util.measureTimeMillisWithResult import org.jetbrains.kotlin.kapt3.base.util.measureTimeMillisWithResult
import java.io.File import java.io.File
import javax.annotation.processing.ProcessingEnvironment
import javax.annotation.processing.Processor import javax.annotation.processing.Processor
import javax.annotation.processing.RoundEnvironment import javax.annotation.processing.RoundEnvironment
import javax.lang.model.SourceVersion
import javax.lang.model.element.TypeElement import javax.lang.model.element.TypeElement
import javax.tools.JavaFileObject import javax.tools.JavaFileObject
import kotlin.system.measureTimeMillis
import com.sun.tools.javac.util.List as JavacList import com.sun.tools.javac.util.List as JavacList
fun KaptContext.doAnnotationProcessing( fun KaptContext.doAnnotationProcessing(
@@ -69,11 +72,7 @@ fun KaptContext.doAnnotationProcessing(
logger.info("Annotation processor stats:") logger.info("Annotation processor stats:")
wrappedProcessors.forEach { processor -> wrappedProcessors.forEach { processor ->
val rounds = processor.rounds logger.info(processor.renderSpentTime())
val roundMs = rounds.joinToString { "$it ms" }
val totalMs = rounds.sum()
logger.info("${processor.name}: ${rounds.size} rounds ($roundMs), $totalMs ms in total")
} }
filer.displayState() filer.displayState()
@@ -89,19 +88,51 @@ fun KaptContext.doAnnotationProcessing(
} }
private class ProcessorWrapper(private val delegate: Processor) : Processor by delegate { private class ProcessorWrapper(private val delegate: Processor) : Processor by delegate {
val rounds = mutableListOf<Long>() private var initTime: Long = 0
private val roundTime = mutableListOf<Long>()
val name: String
get() = delegate.javaClass.simpleName
override fun process(annotations: MutableSet<out TypeElement>?, roundEnv: RoundEnvironment?): Boolean { override fun process(annotations: MutableSet<out TypeElement>?, roundEnv: RoundEnvironment?): Boolean {
val (time, result) = measureTimeMillisWithResult { val (time, result) = measureTimeMillisWithResult {
delegate.process(annotations, roundEnv) delegate.process(annotations, roundEnv)
} }
rounds += time roundTime += time
return result return result
} }
override fun init(processingEnv: ProcessingEnvironment?) {
initTime += measureTimeMillis {
delegate.init(processingEnv)
}
}
override fun getSupportedOptions(): MutableSet<String> {
val (time, result) = measureTimeMillisWithResult { delegate.supportedOptions }
initTime += time
return result
}
override fun getSupportedSourceVersion(): SourceVersion {
val (time, result) = measureTimeMillisWithResult { delegate.supportedSourceVersion }
initTime += time
return result
}
override fun getSupportedAnnotationTypes(): MutableSet<String> {
val (time, result) = measureTimeMillisWithResult { delegate.supportedAnnotationTypes }
initTime += time
return result
}
fun renderSpentTime(): String {
val processorName = delegate.javaClass.simpleName
val totalTime = initTime + roundTime.sum()
return "$processorName: " +
"total: $totalTime ms, " +
"init: $initTime ms, " +
"${roundTime.size} round(s): ${roundTime.joinToString { "$it ms" }}"
}
} }
fun KaptContext.parseJavaFiles(javaSourceFiles: List<File>): JavacList<JCTree.JCCompilationUnit> { fun KaptContext.parseJavaFiles(javaSourceFiles: List<File>): JavacList<JCTree.JCCompilationUnit> {