跟踪Gradle脚本中每个任务的执行时间?

追踪Gradle构build脚本中的任务执行时间的最优雅方式是什么? 在最佳情况下,将时间直接logging到任务名称的相同或下一行

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

最干净的解决scheme是实现一个TaskExecutionListener (我敢肯定,你可以处理该部分),并注册与gradle.taskGraph.addTaskExecutionListener

只是要详细说明另一个答案:我们希望在构build结束时也做同样的事情,并报告时间,所以缓慢的步骤是显而易见的(适当的一方当他们放慢速度时会感到一点小小的羞愧build立!)。

 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() 

我知道这是一个古老的问题,但我发现一个很酷的插件,任务计时。 这就像@jlevy答案,但有一些更多的选项可用: https : //github.com/passy/build-time-tracker-plugin

Pascal Hartig的这个插件不断logging您的构build时间,并提供CSV和条形图摘要。 开发人员推荐它随着时间的推移监视您的构build时间,而不是--profile ,它为您提供当前构build的快照。

这是我目前使用它的方式:

 buildscript { repositories { mavenCentral() } dependencies { classpath "net.rdrei.android.buildtimetracker:gradle-plugin:0.7.+" } } apply plugin: "build-time-tracker" buildtimetracker { reporters { summary { ordered false threshold 50 barstyle 'unicode' } } } 

这是jlevy上面的回答的一个变种,已经被修改以去除已被弃用的可公开访问的gradle Clock类的使用。

 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执行期间或完成后报告时间。

 import java.util.concurrent.TimeUnit // Log timings per task. class TimingsListener implements TaskExecutionListener, BuildListener { private long startTime private timings = [] @Override void beforeExecute(Task task) { startTime = System.nanoTime() } @Override void afterExecute(Task task, TaskState taskState) { def ms = TimeUnit.MILLISECONDS.convert(System.nanoTime() - startTime, TimeUnit.NANOSECONDS); 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()