Skip to content

Instantly share code, notes, and snippets.

@jprinet
Last active October 30, 2025 10:15
Show Gist options
  • Select an option

  • Save jprinet/a2443ea477dada0c363e3e7fd2201778 to your computer and use it in GitHub Desktop.

Select an option

Save jprinet/a2443ea477dada0c363e3e7fd2201778 to your computer and use it in GitHub Desktop.
Capture execution time as custom value
import com.gradle.develocity.agent.gradle.scan.BuildScanConfiguration
import org.gradle.api.internal.project.ProjectInternal
import org.gradle.api.services.BuildService
import org.gradle.api.services.BuildServiceParameters
import org.gradle.tooling.events.FinishEvent
import org.gradle.tooling.events.OperationCompletionListener
import java.time.Instant
object Capture {
var startTime: Long = 0L
var endTime: Long = 0L
}
abstract class BuildTimeService : BuildService<BuildServiceParameters.None>, AutoCloseable, OperationCompletionListener {
private val startTime : Long = Instant.now().toEpochMilli()
override fun close() {
Capture.startTime = startTime
Capture.endTime = Instant.now().toEpochMilli()
}
override fun onFinish(ignored: FinishEvent){
// ignored
}
fun processResults(api: BuildScanConfiguration) {
println("startTime: ${Instant.ofEpochMilli(startTime)}")
api.value("Duration", "${Instant.now().toEpochMilli() - startTime}ms")
}
}
tasks.withType<JavaCompile> {
doLast {
Thread.sleep(3000)
}
}
develocity {
val buildTimeServiceProvider = gradle.sharedServices.registerIfAbsent("buildTimeService", BuildTimeService::class.java) {}
// We register a BuildEventsListenerRegistry in order to mark the BuildService as configuration cache compatible but is otherwise unused.
(project as ProjectInternal).services.get(BuildEventsListenerRegistry::class.java).onTaskCompletion(buildTimeServiceProvider)
buildScan {
Thread.sleep(3000)
buildFinished {
println("Start Time: ${Instant.ofEpochMilli(Capture.startTime)}")
println("End Time: ${Instant.ofEpochMilli(Capture.endTime)}")
buildScan.value("Duration", "${Capture.endTime - Capture.startTime}ms")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment