Allow execution time of the slowest tests to be printed

Change-Id: Idaa6f1906599259d4a0389a59b931748d044d576
diff --git a/build.gradle b/build.gradle
index 4feb96d..15a05ca 100644
--- a/build.gradle
+++ b/build.gradle
@@ -1717,7 +1717,6 @@
                 }
             }
         } else {
-            dependsOn "${taskName}_dexmerger"
             task "${taskName}_smali"(type: SmaliTask) {
                 source = smaliFiles
                 destination = intermediateFile
@@ -1919,6 +1918,9 @@
     }
 }
 
+def testTimes = [:]
+def numberOfTestTimesToPrint = 40
+
 test {
     if (project.hasProperty('generate_golden_files_to')) {
         systemProperty 'generate_golden_files_to', project.property('generate_golden_files_to')
@@ -1963,10 +1965,26 @@
         systemProperty 'desugar_jdk_libs', project.property('desugar_jdk_libs')
     }
 
+    if (project.hasProperty('print_times') || project.hasProperty('one_line_per_test')) {
+        afterTest { desc, result ->
+            def executionTime = (result.endTime - result.startTime) / 1000
+            testTimes["${desc.name} [${desc.className}]"] = executionTime
+        }
+        afterSuite { desc, result ->
+          // parent is null if all tests are done.
+          if (desc.parent == null) {
+            def sortedTimes = testTimes.sort({e1, e2 -> e2.value <=> e1.value})
+            sortedTimes.eachWithIndex{key, value, i ->
+                if (i < numberOfTestTimesToPrint) println "$key: $value"}
+          }
+        }
+    }
+
     if (project.hasProperty('one_line_per_test')) {
         beforeTest { desc ->
             println "Start executing test ${desc.name} [${desc.className}]"
         }
+
         afterTest { desc, result ->
             if (result.resultType == TestResult.ResultType.FAILURE) {
                 printStackTrace(result)
diff --git a/tools/test.py b/tools/test.py
index 6734106..6f3fe83 100755
--- a/tools/test.py
+++ b/tools/test.py
@@ -167,6 +167,9 @@
       help='Use alternative desugared library configuration.')
   result.add_option('--desugared-library', '--desugared_library',
       help='Build and use desugared library from GitHub.')
+  result.add_option('--print-times', '--print_times',
+      help='Print the execution time of the slowest tests..',
+      default=False, action='store_true')
   return result.parse_args()
 
 def archive_failures():
@@ -383,6 +386,8 @@
   # Legacy testing populates the runtimes based on dex_vm.
   vms_to_test = [options.dex_vm] if options.dex_vm != "all" else ALL_ART_VMS
 
+  if options.print_times:
+    gradle_args.append('-Pprint_times=true')
   for art_vm in vms_to_test:
     vm_suffix = "_" + options.dex_vm_kind if art_vm != "default" else ""
     runtimes = ['dex-' + art_vm]