gpt4 book ai didi

gradle - 在 gradle 脚本中跟踪每个任务的执行时间?

转载 作者:IT老高 更新时间:2023-10-28 20:41:50 25 4
gpt4 key购买 nike

在 gradle 构建脚本中跟踪任务执行时间的最优雅方法是什么?在最佳情况下,将时间直接记录到任务名称的同一行或下一行:

:buildSrc:testClasses (0.518 secs)
:fooBar (28.652 secs)

最佳答案

只是详细说明 Peter Niederwieser's answer :我们想做同样的事情,以及在结束时报告时间构建,所以缓慢的步骤是显而易见的(当他们放慢构建速度时,适当的各方会感到一种小小的但健康的羞耻感!)。

BUILD SUCCESSFUL

Total time: 1 mins 37.973 secs
Task timings:
579ms :myproject-foo:clean
15184ms :myproject-bar:clean
2839ms :myproject-bar:compileJava
10157ms :myproject-bar:jar
456ms :myproject-foo:compileJava
391ms :myproject-foo:libs
101ms :myproject-foo:jar
316ms :myproject-bar:compileTestJava
364ms :myproject-foo:compileTestJava
53353ms :myproject-foo:test
2146ms :myproject-bar:test
8348ms :www/node:npmInstall
687ms :www/node:npmTest

可以将类似于以下代码的内容放入顶层 build.gradle 以报告执行期间或完成后的时间。

// Log timings per task.
class TimingsListener implements TaskExecutionListener, BuildListener {
private Clock clock
private timings = []

@Override
void beforeExecute(Task task) {
clock = new org.gradle.util.Clock()
}

@Override
void afterExecute(Task task, TaskState taskState) {
def ms = clock.timeInMs
timings.add([ms, task.path])
task.project.logger.warn "${task.path} took ${ms}ms"
}

@Override
void buildFinished(BuildResult result) {
println "Task timings:"
for (timing in timings) {
if (timing[0] >= 50) {
printf "%7sms %s\n", timing
}
}
}

@Override
void buildStarted(Gradle gradle) {}

@Override
void projectsEvaluated(Gradle gradle) {}

@Override
void projectsLoaded(Gradle gradle) {}

@Override
void settingsEvaluated(Settings settings) {}
}

gradle.addListener new TimingsListener()

关于gradle - 在 gradle 脚本中跟踪每个任务的执行时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13031538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com