简单的排序将使@jlevy的解决方案更好。
另外,对于典型的生产应用程序,我认为50ms的阈值太低。
我们通常关心耗时超过1秒的任务。
项目/ 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(new Tuple2(ms, task.path))
task.project.logger.warn "${task.path} took ${ms}ms"
}
@Override
void buildFinished(BuildResult result) {
println "Task timings:"
def tmp = timings.toSorted(new Comparator>() {
@Override
int compare(Tuple2 o, Tuple2 t1) {
return o.first - t1.first
}
})
for (timing in tmp) {
if (timing.first >= 1000) {
printf "%ss %s\n", timing.first / 1000, timing.second
}
}
}
@Override
void buildStarted(Gradle gradle) {}
@Override
void projectsEvaluated(Gradle gradle) {}
@Override
void projectsLoaded(Gradle gradle) {}
@Override
void settingsEvaluated(Settings settings) {}
}
gradle.addListener new TimingsListener()
终端输出:
BUILD SUCCESSFUL in 14m 33s
948 actionable tasks: 419 executed, 476 from cache, 53 up-to-date
Task timings:
1.036s :cbl-config:mergeMyAppDebugResources
1.187s :express:bundleMyAppDebug
1.199s :country:testMyAppDebugUnitTest
1.214s :core-for-test:extractMyAppDebugAnnotations
1.242s :analytics:testMyAppDebugUnitTest
1.308s :express:extractMyAppDebugAnnotations
1.33s :availability:dataBindingExportBuildInfoMyAppDebug
1.357s :app:transformNativeLibsWithStripDebugSymbolForMyAppDebug
1.405s :hermes:generateMyAppDebugBuildConfig
1.56s :availability:testMyAppDebugUnitTest
1.65s :app:javaPreCompileMyAppDebugUnitTest
1.749s :chat:compileMyAppDebugJavaWithJavac
1.858s :cbl-config-for-test:compileMyAppDebugJavaWithJavac
2.027s :cbl-config:compileMyAppDebugJavaWithJavac
2.056s :analytics-for-test:compileMyAppDebugJavaWithJavac
2.447s :crypto:compileMyAppDebugJavaWithJavac
2.45s :crypto:testMyAppDebugUnitTest
2.47s :chat:javaPreCompileMyAppDebugUnitTest
2.639s :crypto-for-test:dataBindingExportBuildInfoMyAppDebug
2.683s :test-utils:compileMyAppDebugJavaWithJavac
3.056s :crypto:lintMyAppDebug
3.227s :app:transformNativeLibsWithMergeJniLibsForMyAppDebug
3.272s :express:testMyAppDebugUnitTest
3.394s :crypto:mergeMyAppDebugResources
3.426s :core:testMyAppDebugUnitTest
4.299s :multicity:testMyAppDebugUnitTest
4.333s :app:packageMyAppDebug
4.584s :availability-for-test:compileMyAppDebugJavaWithJavac
4.672s :app:transformResourcesWithMergeJavaResForMyAppDebug
4.786s :map:lintMyAppDebug
5.309s :country:lintMyAppDebug
5.332s :job:lintMyAppDebug
5.389s :map:testMyAppDebugUnitTest
6.04s :express:lintMyAppDebug
6.584s :hermes:lintMyAppDebug
6.707s :app:transformClassesWithMultidexlistForMyAppDebug
7.052s :multicity:lintMyAppDebug
8.044s :multicity:compileMyAppDebugJavaWithJavac
8.87s :app:transformDexArchiveWithDexMergerForMyAppDebug
9.371s :uikit:testMyAppDebugUnitTest
9.429s :availability:lintMyAppDebug
13.12s :app:compileMyAppDebugUnitTestKotlin
16.276s :hermes:testMyAppDebugUnitTest
16.898s :chat:testMyAppDebugUnitTest
17.174s :job:testMyAppDebugUnitTest
36.008s :grab-junior:testMyAppDebugUnitTest
96.88s :app:compileMyAppDebugJavaWithJavac
125.693s :app:lintMyAppDebug
145.538s :app:transformClassesWithDexBuilderForMyAppDebug
182.752s :app:testMyAppDebugUnitTest