[go: nahoru, domu]

Output total benchmark test run time in full json output

For each individual test, its total running time is displayed.

Bug: 133147694

Test: ./gradlew benchmark:cC

Change-Id: I9f2e41a2e832b68017854f96d0c837138fcf6394
diff --git a/benchmark/api/1.0.0-alpha04.txt b/benchmark/api/1.0.0-alpha04.txt
index 92ebcf3..268140e 100644
--- a/benchmark/api/1.0.0-alpha04.txt
+++ b/benchmark/api/1.0.0-alpha04.txt
@@ -23,7 +23,7 @@
   public final class BenchmarkState {
     method public boolean keepRunning();
     method public void pauseTiming();
-    method public static void reportData(String className, String testName, java.util.List<java.lang.Long> dataNs, @IntRange(from=0) int warmupIterations, @IntRange(from=0) long thermalThrottleSleepSeconds, @IntRange(from=1) int repeatIterations);
+    method public static void reportData(String className, String testName, long totalRunTimeNs, java.util.List<java.lang.Long> dataNs, @IntRange(from=0) int warmupIterations, @IntRange(from=0) long thermalThrottleSleepSeconds, @IntRange(from=1) int repeatIterations);
     method public void resumeTiming();
   }
 
diff --git a/benchmark/api/current.txt b/benchmark/api/current.txt
index 92ebcf3..268140e 100644
--- a/benchmark/api/current.txt
+++ b/benchmark/api/current.txt
@@ -23,7 +23,7 @@
   public final class BenchmarkState {
     method public boolean keepRunning();
     method public void pauseTiming();
-    method public static void reportData(String className, String testName, java.util.List<java.lang.Long> dataNs, @IntRange(from=0) int warmupIterations, @IntRange(from=0) long thermalThrottleSleepSeconds, @IntRange(from=1) int repeatIterations);
+    method public static void reportData(String className, String testName, long totalRunTimeNs, java.util.List<java.lang.Long> dataNs, @IntRange(from=0) int warmupIterations, @IntRange(from=0) long thermalThrottleSleepSeconds, @IntRange(from=1) int repeatIterations);
     method public void resumeTiming();
   }
 
diff --git a/benchmark/api/restricted_1.0.0-alpha04.txt b/benchmark/api/restricted_1.0.0-alpha04.txt
index 85f9d04..a4c1fd3 100644
--- a/benchmark/api/restricted_1.0.0-alpha04.txt
+++ b/benchmark/api/restricted_1.0.0-alpha04.txt
@@ -23,7 +23,7 @@
   public final class BenchmarkState {
     method public boolean keepRunning();
     method public void pauseTiming();
-    method public static void reportData(String className, String testName, java.util.List<java.lang.Long> dataNs, @IntRange(from=0) int warmupIterations, @IntRange(from=0) long thermalThrottleSleepSeconds, @IntRange(from=1) int repeatIterations);
+    method public static void reportData(String className, String testName, long totalRunTimeNs, java.util.List<java.lang.Long> dataNs, @IntRange(from=0) int warmupIterations, @IntRange(from=0) long thermalThrottleSleepSeconds, @IntRange(from=1) int repeatIterations);
     method public void resumeTiming();
   }
 
diff --git a/benchmark/api/restricted_current.txt b/benchmark/api/restricted_current.txt
index 85f9d04..a4c1fd3 100644
--- a/benchmark/api/restricted_current.txt
+++ b/benchmark/api/restricted_current.txt
@@ -23,7 +23,7 @@
   public final class BenchmarkState {
     method public boolean keepRunning();
     method public void pauseTiming();
-    method public static void reportData(String className, String testName, java.util.List<java.lang.Long> dataNs, @IntRange(from=0) int warmupIterations, @IntRange(from=0) long thermalThrottleSleepSeconds, @IntRange(from=1) int repeatIterations);
+    method public static void reportData(String className, String testName, long totalRunTimeNs, java.util.List<java.lang.Long> dataNs, @IntRange(from=0) int warmupIterations, @IntRange(from=0) long thermalThrottleSleepSeconds, @IntRange(from=1) int repeatIterations);
     method public void resumeTiming();
   }
 
diff --git a/benchmark/src/androidTest/java/androidx/benchmark/BenchmarkStateTest.kt b/benchmark/src/androidTest/java/androidx/benchmark/BenchmarkStateTest.kt
index 56da1d5..6485795 100644
--- a/benchmark/src/androidTest/java/androidx/benchmark/BenchmarkStateTest.kt
+++ b/benchmark/src/androidTest/java/androidx/benchmark/BenchmarkStateTest.kt
@@ -125,10 +125,11 @@
 
     @Test
     fun reportResult() {
-        BenchmarkState.reportData("className", "testName", listOf(100), 1, 0, 1)
+        BenchmarkState.reportData("className", "testName", 900000000, listOf(100), 1, 0, 1)
         val expectedReport = BenchmarkState.Report(
             className = "className",
             testName = "testName",
+            totalRunTimeNs = 900000000,
             data = listOf(100),
             repeatIterations = 1,
             thermalThrottleSleepSeconds = 0,
diff --git a/benchmark/src/androidTest/java/androidx/benchmark/ResultWriterTest.kt b/benchmark/src/androidTest/java/androidx/benchmark/ResultWriterTest.kt
index 6a86e92..20ed927 100644
--- a/benchmark/src/androidTest/java/androidx/benchmark/ResultWriterTest.kt
+++ b/benchmark/src/androidTest/java/androidx/benchmark/ResultWriterTest.kt
@@ -35,6 +35,7 @@
     private val reportA = BenchmarkState.Report(
         testName = "MethodA",
         className = "package.Class1",
+        totalRunTimeNs = 900000000,
         data = listOf(100, 101, 102),
         repeatIterations = 100000,
         thermalThrottleSleepSeconds = 90000000,
@@ -43,6 +44,7 @@
     private val reportB = BenchmarkState.Report(
         testName = "MethodB",
         className = "package.Class2",
+        totalRunTimeNs = 900000000,
         data = listOf(100, 101, 102),
         repeatIterations = 100000,
         thermalThrottleSleepSeconds = 90000000,
@@ -89,6 +91,7 @@
                     {
                         "name": "MethodA",
                         "className": "package.Class1",
+                        "totalRunTimeNs": 900000000,
                         "metrics": {
                             "timeNs": {
                                 "minimum": 100,
@@ -108,6 +111,7 @@
                     {
                         "name": "MethodB",
                         "className": "package.Class2",
+                        "totalRunTimeNs": 900000000,
                         "metrics": {
                             "timeNs": {
                                 "minimum": 100,
diff --git a/benchmark/src/main/java/androidx/benchmark/BenchmarkState.kt b/benchmark/src/main/java/androidx/benchmark/BenchmarkState.kt
index 9ac76a6..6a4a34d 100644
--- a/benchmark/src/main/java/androidx/benchmark/BenchmarkState.kt
+++ b/benchmark/src/main/java/androidx/benchmark/BenchmarkState.kt
@@ -76,6 +76,8 @@
     private var pausedDurationNs: Long = 0 // The duration of paused state in nano sec.
     private var thermalThrottleSleepSeconds: Long =
         0 // The duration of sleep due to thermal throttling.
+    private var totalRunTimeStartNs: Long = 0 // System.nanoTime() at start of benchmark.
+    private var totalRunTimeNs: Long = 0 // Total run time of a benchmark.
 
     private var repeatCount = 0
 
@@ -235,6 +237,7 @@
                 }
                 internalStats = Stats(results)
                 state = FINISHED
+                totalRunTimeNs = System.nanoTime() - totalRunTimeStartNs
                 return false
             }
         }
@@ -285,6 +288,10 @@
     internal fun keepRunningInternal(): Boolean {
         when (state) {
             NOT_STARTED -> {
+                if (totalRunTimeStartNs == 0L) {
+                    // This is the beginning of the benchmark, we remember it.
+                    totalRunTimeStartNs = System.nanoTime()
+                }
                 if (performThrottleChecks &&
                     !CpuInfo.locked &&
                     !AndroidBenchmarkRunner.sustainedPerformanceModeInUse &&
@@ -333,6 +340,7 @@
     internal data class Report(
         val className: String,
         val testName: String,
+        val totalRunTimeNs: Long,
         val data: List<Long>,
         val repeatIterations: Int,
         val thermalThrottleSleepSeconds: Long,
@@ -344,6 +352,7 @@
     internal fun getReport(testName: String, className: String) = Report(
         className = className,
         testName = testName,
+        totalRunTimeNs = totalRunTimeNs,
         data = results,
         repeatIterations = maxIterations,
         thermalThrottleSleepSeconds = thermalThrottleSleepSeconds,
@@ -422,6 +431,7 @@
          *
          * @param className Name of class the benchmark runs in
          * @param testName Name of the benchmark
+         * @param totalRunTimeNs The total run time of the benchmark
          * @param dataNs List of all measured results, in nanoseconds
          * @param warmupIterations Number of iterations of warmup before measurements started.
          *                         Should be no less than 0.
@@ -435,6 +445,7 @@
         fun reportData(
             className: String,
             testName: String,
+            totalRunTimeNs: Long,
             dataNs: List<Long>,
             @IntRange(from = 0) warmupIterations: Int,
             @IntRange(from = 0) thermalThrottleSleepSeconds: Long,
@@ -443,6 +454,7 @@
             val report = Report(
                 className = className,
                 testName = testName,
+                totalRunTimeNs = totalRunTimeNs,
                 data = dataNs,
                 repeatIterations = repeatIterations,
                 thermalThrottleSleepSeconds = thermalThrottleSleepSeconds,
diff --git a/benchmark/src/main/java/androidx/benchmark/ResultWriter.kt b/benchmark/src/main/java/androidx/benchmark/ResultWriter.kt
index e0b287e..879a053 100644
--- a/benchmark/src/main/java/androidx/benchmark/ResultWriter.kt
+++ b/benchmark/src/main/java/androidx/benchmark/ResultWriter.kt
@@ -91,6 +91,7 @@
         beginObject()
             .name("name").value(report.testName)
             .name("className").value(report.className)
+            .name("totalRunTimeNs").value(report.totalRunTimeNs)
             .name("metrics").metricsObject(report)
             .name("warmupIterations").value(report.warmupIterations)
             .name("repeatIterations").value(report.repeatIterations)