ru.tinkoff.plugins.buildmetrics.gradle.metrics.internal.BuildCacheMetricsFactory.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of build-metrics-gradle Show documentation
Show all versions of build-metrics-gradle Show documentation
Extension for tinkoff build metrics gradle plugin.
package ru.tinkoff.plugins.buildmetrics.gradle.metrics.internal
import org.gradle.api.internal.tasks.execution.ExecuteTaskBuildOperationType
import org.gradle.caching.internal.operations.BuildCacheRemoteLoadBuildOperationType
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.metrics.Metric
import ru.tinkoff.plugins.buildmetrics.gradle.internal.utils.ConfigurationCacheData
import ru.tinkoff.plugins.buildmetrics.gradle.internal.utils.TasksData
/**
* Metrics:
* - gradle_configuration_cache_reused;
* - gradle_tasks_build_cache_ratio;
* - gradle_tasks_local_build_cache_ratio;
* - gradle_tasks_local_build_cache_hit_count;
* - gradle_tasks_local_build_cache_miss_count;
* - gradle_tasks_remote_build_cache_ratio;
* - gradle_tasks_remote_build_cache_hit_count;
* - gradle_tasks_remote_build_cache_miss_count;
*/
class BuildCacheMetricsFactory : Factory.Metrics, BuildOperationDataListener {
private class FactoryData(
private val tasksDataBuilder: TasksData.Builder = TasksData.Builder(),
private val configurationCacheDataBuilder: ConfigurationCacheData.Builder = ConfigurationCacheData.Builder(),
) {
val executeTasksOperations: MutableSet = mutableSetOf()
val loadRemoteBuildCacheHitParentOperations: MutableSet = mutableSetOf()
val loadRemoteBuildCacheMissParentOperations: MutableSet = mutableSetOf()
fun onBuildOperationData(data: BuildOperationData) {
val details = data.details
val result = data.result
if (details is ExecuteTaskBuildOperationType.Details && result is ExecuteTaskBuildOperationType.Result) {
executeTasksOperations.add(data.id)
} else if (result is BuildCacheRemoteLoadBuildOperationType.Result) {
if (result.isHit) {
loadRemoteBuildCacheHitParentOperations.add(data.parentId)
} else {
loadRemoteBuildCacheMissParentOperations.add(data.parentId)
}
}
tasksDataBuilder.onBuildOperationData(data = data)
configurationCacheDataBuilder.onBuildOperationData(data = data)
}
fun tasksRemoteCacheHitCount(): Int = loadRemoteBuildCacheHitParentOperations.filter { id ->
executeTasksOperations.contains(id)
}.size
fun tasksRemoteCacheMissCount(): Int = loadRemoteBuildCacheMissParentOperations.filter { id ->
executeTasksOperations.contains(id)
}.size
fun tasksData(): TasksData = tasksDataBuilder.build()
fun configurationCacheData(): ConfigurationCacheData = configurationCacheDataBuilder.build()
}
@Transient
private var factoryData: FactoryData = FactoryData()
override fun reinitialize() {
factoryData = FactoryData()
}
override fun onBuildOperationData(data: BuildOperationData) {
factoryData.onBuildOperationData(data = data)
}
override fun create(): List> {
val tasksData = factoryData.tasksData()
val configurationCacheData = factoryData.configurationCacheData()
val requestedFromCacheTasksCount = tasksData.requestedFromCacheTasksCount
val fromCacheTasksCount = tasksData.fromCacheTasksCount
val tasksBuildCacheRatio = cacheRatio(value = fromCacheTasksCount, total = requestedFromCacheTasksCount)
val tasksRemoteCacheHitCount = factoryData.tasksRemoteCacheHitCount()
val tasksRemoteCacheMissCount = factoryData.tasksRemoteCacheMissCount()
val tasksRemoteCacheRatio = cacheRatio(
value = tasksRemoteCacheHitCount,
total = tasksRemoteCacheHitCount + tasksRemoteCacheMissCount
)
val tasksLocalCacheHitCount = fromCacheTasksCount - tasksRemoteCacheHitCount
val tasksLocalCacheMissCount = requestedFromCacheTasksCount - tasksLocalCacheHitCount
val tasksLocalCacheRatio = cacheRatio(
value = tasksLocalCacheHitCount,
total = tasksLocalCacheHitCount + tasksLocalCacheMissCount
)
return listOf(
Metric(name = "gradle_configuration_cache_reused", value = configurationCacheData.reused),
Metric(name = "gradle_tasks_build_cache_ratio", value = tasksBuildCacheRatio),
Metric(name = "gradle_tasks_local_build_cache_ratio", value = tasksLocalCacheRatio),
Metric(name = "gradle_tasks_local_build_cache_hit_count", value = tasksLocalCacheHitCount),
Metric(name = "gradle_tasks_local_build_cache_miss_count", value = tasksLocalCacheMissCount),
Metric(name = "gradle_tasks_remote_build_cache_ratio", value = tasksRemoteCacheRatio),
Metric(name = "gradle_tasks_remote_build_cache_hit_count", value = tasksRemoteCacheHitCount),
Metric(name = "gradle_tasks_remote_build_cache_miss_count", value = tasksRemoteCacheMissCount),
)
}
private fun cacheRatio(value: Int, total: Int): Double = if (total > 0) {
value.toDouble() / total.toDouble()
} else {
0.0
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy