Track execution time per task in gradle script?

后端 未结 8 2125
深忆病人
深忆病人 2021-01-30 06:37

What is the most elegant way to track the execution times on how long a task took in a gradle build script? In an optimal case log the time directly same or next line to the tas

相关标签:
8条回答
  • 2021-01-30 07:22

    I created a plugin since passy/build-time-tracker-plugin is no longer actively maintained. Mine prints ASCII bar charts too, and comes with customization options.

    https://github.com/asarkar/build-time-tracker

    == Build time summary ==
     :commons:extractIncludeProto | 4.000s | 14% | ████
           :commons:compileKotlin | 2.000s |  7% | ██
             :commons:compileJava | 6.000s | 21% | ██████
    :service-client:compileKotlin | 1.000s |  4% | █
            :webapp:compileKotlin | 1.000s |  4% | █
         :webapp:dockerBuildImage | 4.000s | 14% | ████
          :webapp:dockerPushImage | 4.000s | 14% | ████
    
    0 讨论(0)
  • 2021-01-30 07:25

    Simple sorting would make @jlevy's solution even better.
    Also, for a typical production apps, I think the threshold of 50ms is too low.
    We usually care about tasks that take more than X second.
    project/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<Integer, String>(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<Tuple2<Integer, String>>() {
                @Override
                int compare(Tuple2<Integer, String> o, Tuple2<Integer, String> 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()
    

    Terminal output:

    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  :aaefawef:testMyAppDebugUnitTest
    96.88s  :app:compileMyAppDebugJavaWithJavac
    125.693s  :app:lintMyAppDebug
    145.538s  :app:transformClassesWithDexBuilderForMyAppDebug
    182.752s  :app:testMyAppDebugUnitTest
    
    0 讨论(0)
提交回复
热议问题