Print in the console report showing amount of tasks used K2

^KT-57736 Fixed
This commit is contained in:
Yahor Berdnikau
2023-04-26 09:03:45 +02:00
committed by Space Team
parent 847544d99c
commit 982a4fbda0
5 changed files with 119 additions and 6 deletions
@@ -30,6 +30,7 @@ class TryK2IT : KGPBaseTest() {
build("--dry-run") {
assertOutputContainsExactTimes(EXPERIMENTAL_TRY_K2_WARNING_MESSAGE, 1)
assertOutputContains("No Kotlin compilation tasks have run")
}
}
}
@@ -53,6 +54,78 @@ class TryK2IT : KGPBaseTest() {
}
}
@DisplayName("JVM: build report is not printed")
@JvmGradlePluginTests
@GradleTest
fun buildReportNotPrinted(gradleVersion: GradleVersion) {
project(
"incrementalMultiproject",
gradleVersion,
buildOptions = defaultBuildOptions.copy(logLevel = LogLevel.WARN)
) {
build("build") {
assertOutputDoesNotContain(
"##### 'kotlin.experimental.tryK2' results (Kotlin/Native not checked) #####"
)
}
}
}
@DisplayName("JVM: report is printed at the end of the build")
@JvmGradlePluginTests
@GradleTest
fun buildReport(gradleVersion: GradleVersion) {
project(
"incrementalMultiproject",
gradleVersion,
buildOptions = defaultBuildOptions.copy(logLevel = LogLevel.WARN)
) {
enableTryK2()
build("build") {
assertOutputContains(
"""
|##### 'kotlin.experimental.tryK2' results (Kotlin/Native not checked) #####
|:lib:compileKotlin: 2.0 language version
|:app:compileKotlin: 2.0 language version
|##### 100% (2/2) tasks have compiled with Kotlin 2 #####
""".trimMargin().normalizeLineEndings()
)
}
}
}
@DisplayName("JVM: report is printed at the end of the build in case of compilation error")
@JvmGradlePluginTests
@GradleTest
fun buildReportOnCompilationError(gradleVersion: GradleVersion) {
project(
"incrementalMultiproject",
gradleVersion,
buildOptions = defaultBuildOptions.copy(logLevel = LogLevel.WARN)
) {
enableTryK2()
subProject("app").kotlinSourcesDir().resolve("foo/AA.kt").appendText(
"""
|
|ZZZZ
""".trimMargin()
)
buildAndFail("build", forceOutput = true) {
assertOutputContains(
"""
|##### 'kotlin.experimental.tryK2' results (Kotlin/Native not checked) #####
|:lib:compileKotlin: 2.0 language version
|:app:compileKotlin: 2.0 language version
|##### 100% (2/2) tasks have compiled with Kotlin 2 #####
""".trimMargin().normalizeLineEndings()
)
}
}
}
@DisplayName("MPP: language version default is changed to 2.0")
@MppGradlePluginTests
@GradleTest
@@ -11,9 +11,11 @@ enum class BuildReportType : Serializable {
FILE,
HTTP,
BUILD_SCAN,
SINGLE_FILE;
SINGLE_FILE,
TRY_K2_CONSOLE,
;
companion object {
const val serialVersionUID: Long = 0
const val serialVersionUID: Long = 1L
}
}
@@ -16,6 +16,7 @@ import org.jetbrains.kotlin.build.report.statistics.BuildFinishStatisticsData
import org.jetbrains.kotlin.build.report.statistics.CompileStatisticsData
import org.jetbrains.kotlin.build.report.statistics.BuildStartParameters
import org.jetbrains.kotlin.build.report.statistics.StatTag
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
import org.jetbrains.kotlin.gradle.report.data.BuildExecutionData
import org.jetbrains.kotlin.gradle.report.data.BuildOperationRecord
import org.jetbrains.kotlin.utils.addToStdlib.measureTimeMillisWithResult
@@ -92,6 +93,10 @@ class BuildReportsService {
MetricsWriter(singleOutputFile.absoluteFile).process(buildData, log)
}
if (reportingSettings.experimentalTryK2ConsoleOutput) {
reportTryK2ToConsole(buildData)
}
//It's expected that bad internet connection can cause a significant delay for big project
executorService.shutdown()
}
@@ -227,6 +232,32 @@ class BuildReportsService {
customValues++
}
private fun reportTryK2ToConsole(
data: BuildExecutionData
) {
val tasksData = data.buildOperationRecord
.filterIsInstance<TaskRecord>()
.filter {
// Filtering by only KGP tasks and by those that actually do compilation
it.isFromKotlinPlugin && it.kotlinLanguageVersion != null
}
log.warn("##### 'kotlin.experimental.tryK2' results (Kotlin/Native not checked) #####")
if (tasksData.isEmpty()) {
log.warn("No Kotlin compilation tasks have run")
log.warn("#####")
} else {
val tasksCountWithKotlin2 = tasksData.count {
it.kotlinLanguageVersion != null && it.kotlinLanguageVersion >= KotlinVersion.KOTLIN_2_0
}
val taskWithK2Percent = (tasksCountWithKotlin2 * 100) / tasksData.count()
val statsData = tasksData.map { it.path to it.kotlinLanguageVersion?.version }
statsData.forEach { record ->
log.warn("${record.first}: ${record.second} language version")
}
log.warn("##### $taskWithK2Percent% ($tasksCountWithKotlin2/${tasksData.count()}) tasks have compiled with Kotlin 2 #####")
}
}
private fun readableString(data: CompileStatisticsData): List<String> {
val readableString = StringBuilder()
if (data.nonIncrementalAttributes.isEmpty()) {
@@ -18,6 +18,7 @@ data class ReportingSettings(
val httpReportSettings: HttpReportSettings? = null,
val buildScanReportSettings: BuildScanSettings? = null,
val singleOutputFile: File? = null,
val experimentalTryK2ConsoleOutput: Boolean = false,
val includeCompilerArguments: Boolean = false,
) : Serializable {
companion object {
@@ -20,10 +20,15 @@ private val availableMetrics = BuildTime.values().map { it.name } + BuildPerform
internal fun reportingSettings(project: Project): ReportingSettings {
val properties = PropertiesProvider(project)
val buildReportOutputTypes = properties.buildReportOutputs.map {
BuildReportType.values().firstOrNull { brt -> brt.name == it.trim().toUpperCaseAsciiOnly() }
?: throw IllegalStateException("Unknown output type: $it")
}.toMutableList() //temporary solution. support old property
val experimentalTryK2Enabled = properties.kotlinExperimentalTryK2.get()
val buildReportOutputTypes = properties.buildReportOutputs
.map {
BuildReportType.values().firstOrNull { brt -> brt.name == it.trim().toUpperCaseAsciiOnly() }
?: throw IllegalStateException("Unknown output type: $it")
}
.plus(if (experimentalTryK2Enabled) listOf(BuildReportType.TRY_K2_CONSOLE) else emptyList())
.toMutableList() //temporary solution. support old property
val buildReportMode =
when {
buildReportOutputTypes.isEmpty() -> BuildReportMode.NONE
@@ -82,6 +87,7 @@ internal fun reportingSettings(project: Project): ReportingSettings {
buildReportOutputs = buildReportOutputTypes,
singleOutputFile = singleOutputFile ?: oldSingleBuildMetric,
includeCompilerArguments = properties.buildReportIncludeCompilerArguments,
experimentalTryK2ConsoleOutput = experimentalTryK2Enabled
)
}