All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.tinkoff.plugins.buildmetrics.gradle.metrics.internal.BuildOperationsMetricsFactory.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0-RC2
Show newest version
package ru.tinkoff.plugins.buildmetrics.gradle.metrics.internal

import org.gradle.initialization.ConfigureBuildBuildOperationType
import org.gradle.initialization.LoadBuildBuildOperationType
import org.gradle.internal.configurationcache.ConfigurationCacheLoadBuildOperationType
import org.gradle.internal.configurationcache.ConfigurationCacheStoreBuildOperationType
import org.gradle.internal.operations.BuildOperationCategory
import org.gradle.internal.scripts.CompileScriptBuildOperationType
import org.gradle.internal.taskgraph.CalculateTreeTaskGraphBuildOperationType
import org.gradle.tooling.provider.model.internal.QueryToolingModelBuildOperationType
import ru.tinkoff.plugins.buildmetrics.api.builds.internal.BuildOperationData
import ru.tinkoff.plugins.buildmetrics.api.builds.internal.BuildOperationDataListener
import ru.tinkoff.plugins.buildmetrics.api.factories.Factory
import ru.tinkoff.plugins.buildmetrics.api.labels.Label
import ru.tinkoff.plugins.buildmetrics.api.metrics.Metric
import ru.tinkoff.plugins.buildmetrics.utils.serialTime
import ru.tinkoff.plugins.buildmetrics.utils.wallClockTime

/**
 * Metrics:
 * - gradle_build_operations_count;
 * - gradle_load_configuration_cache_bytes;
 * - gradle_store_configuration_cache_bytes;
 * - gradle_build_model_duration_ms;
 * - gradle_calculate_tree_task_graph_duration_ms;
 * - gradle_configure_build_duration_ms;
 * - gradle_load_build_duration_ms;
 * - gradle_load_configuration_cache_duration_ms;
 * - gradle_store_configuration_cache_duration_ms;
 * - gradle_run_main_tasks_duration_ms;
 * - gradle_build_script_compile_duration_ms
 *    - gradle_build_script_language - label
 * - gradle_build_script_compile_serial_duration_ms
 *    - gradle_build_script_language - label
 */
class BuildOperationsMetricsFactory : Factory.Metrics, BuildOperationDataListener {

    private class FactoryData {

        var buildOperationsCount: Int = 0
            private set

        var loadConfigurationCacheBytes: Long = 0L
            private set

        var storeConfigurationCacheBytes: Long = 0L
            private set

        val buildModelDurations: MutableList> = mutableListOf()

        val calculateTreeTaskGraphDurations: MutableList> = mutableListOf()

        val configureBuildDurations: MutableList> = mutableListOf()

        val loadBuildDurations: MutableList> = mutableListOf()

        val loadConfigurationCacheDurations: MutableList> = mutableListOf()

        val storeConfigurationCacheDurations: MutableList> = mutableListOf()

        val runMainTasksDurations: MutableList> = mutableListOf()

        val buildScriptCompileTimeLine: MutableMap>> = mutableMapOf()

        fun onBuildOperationData(data: BuildOperationData) {
            ++buildOperationsCount
            when (val details = data.details) {
                is QueryToolingModelBuildOperationType.Details -> {
                    buildModelDurations.add(data.startTime to data.endTime)
                }
                is CalculateTreeTaskGraphBuildOperationType.Details -> {
                    calculateTreeTaskGraphDurations.add(data.startTime to data.endTime)
                }
                is ConfigureBuildBuildOperationType.Details -> {
                    configureBuildDurations.add(data.startTime to data.endTime)
                }
                is LoadBuildBuildOperationType.Details -> {
                    loadBuildDurations.add(data.startTime to data.endTime)
                }
                is ConfigurationCacheLoadBuildOperationType.Details -> {
                    loadConfigurationCacheDurations.add(data.startTime to data.endTime)
                }
                is ConfigurationCacheStoreBuildOperationType.Details -> {
                    storeConfigurationCacheDurations.add(data.startTime to data.endTime)
                }
                is CompileScriptBuildOperationType.Details -> {
                    val language = details.language
                    buildScriptCompileTimeLine[language] = buildScriptCompileTimeLine
                        .getOrPut(language, defaultValue = ::mutableListOf)
                        .apply { add(data.startTime to data.endTime) }
                }
            }
            when (val result = data.result) {
                is ConfigurationCacheLoadBuildOperationType.Result -> {
                    loadConfigurationCacheBytes += result.cacheEntrySize
                }
                is ConfigurationCacheStoreBuildOperationType.Result -> {
                    storeConfigurationCacheBytes += result.cacheEntrySize
                }
            }
            if (data.metadata == BuildOperationCategory.RUN_MAIN_TASKS) {
                runMainTasksDurations.add(data.startTime to data.endTime)
            }
        }
    }

    @Transient
    private var factoryData: FactoryData = FactoryData()

    override fun reinitialize() {
        factoryData = FactoryData()
    }

    override fun onBuildOperationData(data: BuildOperationData) {
        factoryData.onBuildOperationData(data = data)
    }

    override fun create(): List> {
        return listOf(
            Metric(
                name = "gradle_build_operations_count",
                value = factoryData.buildOperationsCount,
            ),
            Metric(
                name = "gradle_load_configuration_cache_bytes",
                value = factoryData.loadConfigurationCacheBytes,
            ),
            Metric(
                name = "gradle_store_configuration_cache_bytes",
                value = factoryData.storeConfigurationCacheBytes,
            ),
            Metric(
                name = "gradle_build_model_duration_ms",
                value = factoryData.buildModelDurations.wallClockTime(),
            ),
            Metric(
                name = "gradle_calculate_tree_task_graph_duration_ms",
                value = factoryData.calculateTreeTaskGraphDurations.wallClockTime(),
            ),
            Metric(
                name = "gradle_configure_build_duration_ms",
                value = factoryData.configureBuildDurations.wallClockTime(),
            ),
            Metric(
                name = "gradle_load_build_duration_ms",
                value = factoryData.loadBuildDurations.wallClockTime(),
            ),
            Metric(
                name = "gradle_load_configuration_cache_duration_ms",
                value = factoryData.loadConfigurationCacheDurations.wallClockTime(),
            ),
            Metric(
                name = "gradle_store_configuration_cache_duration_ms",
                value = factoryData.storeConfigurationCacheDurations.wallClockTime(),
            ),
            Metric(
                name = "gradle_run_main_tasks_duration_ms",
                value = factoryData.runMainTasksDurations.wallClockTime(),
            ),
        ) + buildScriptCompileMetrics(factoryData.buildScriptCompileTimeLine)
    }

    private fun buildScriptCompileMetrics(buildScriptCompileTimeLine: Map>>): List> {
        return buildScriptCompileTimeLine.flatMap { (lang, timeline) ->
            val labels = listOf(Label(name = "gradle_build_script_language", value = lang))
            listOf(
                Metric(
                    name = "gradle_build_script_compile_duration_ms",
                    value = timeline.wallClockTime(),
                    labels = labels
                ),
                Metric(
                    name = "gradle_build_script_compile_serial_duration_ms",
                    value = timeline.serialTime(),
                    labels = labels
                )
            )
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy