Merge Android 14 QPR2 to AOSP main

Bug: 319669529
Merged-In: I03b77226902eea6447c9f4bad5aad6fe4e1db671
Change-Id: I653e059ade85fab396f464061295c74e8944cb97
diff --git a/src/com/android/performance/tests/BootTimeTest.java b/src/com/android/performance/tests/BootTimeTest.java
index 1ef102b..ebc0fff 100644
--- a/src/com/android/performance/tests/BootTimeTest.java
+++ b/src/com/android/performance/tests/BootTimeTest.java
@@ -109,7 +109,7 @@
     private static final String LOGCAT_FILE = "Successive_reboots_logcat";
     private static final String LOGCAT_UNLOCK_FILE = "Successive_reboots_unlock_logcat";
     private static final String BOOT_COMPLETE_ACTION = "sys.boot_completed=1";
-    private static final String RUNNER = "android.support.test.runner.AndroidJUnitRunner";
+    private static final String RUNNER = "androidx.test.runner.AndroidJUnitRunner";
     private static final String PACKAGE_NAME = "com.android.boothelper";
     private static final String CLASS_NAME = "com.android.boothelper.BootHelperTest";
     private static final String SETUP_PIN_TEST = "setupLockScreenPin";
@@ -192,6 +192,8 @@
                     "Logging for this PID.*\\s+([0-9]+)$",
                     Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
 
+    private static final String METRIC_COUNT = "MetricCount";
+
     @Option(name = "test-run-name", description = "run name to report to result reporters")
     private String mTestRunName = BOOTTIME_TEST;
 
@@ -245,6 +247,12 @@
     private long mBootDelayTime = 2000;
 
     @Option(
+            name = "after-boot-delay",
+            isTimeVal = true,
+            description = "Time to wait immediately after the successive boots.")
+    private long mAfterBootDelayTime = 0;
+
+    @Option(
             name = "post-initial-boot-idle",
             isTimeVal = true,
             description =
@@ -334,6 +342,13 @@
             description = "Run logcat --statistics command and collect data")
     private boolean mCollectLogcat = false;
 
+    @Option(
+            name = "metric-prefix-pattern-for-count",
+            description =
+                    "A list of metric prefix pattern that will be used to count number of metrics"
+                            + " generated in the test")
+    private List<String> mMetricPrefixPatternForCount = new ArrayList<>();
+
     private IBuildInfo mBuildInfo;
     private IConfiguration mConfiguration;
     private TestInformation mTestInfo;
@@ -412,6 +427,9 @@
         mTestInfo = testInfo;
         long start = System.currentTimeMillis();
         listener.testRunStarted(mTestRunName, 1);
+        for (IMetricCollector collector : mCollectors) {
+            listener = collector.init(mInvocationContext, listener);
+        }
         try {
             try {
                 // Set the current date from the host in test device.
@@ -487,10 +505,13 @@
                         }
                         testSuccessiveBoots(true, listener);
                     } finally {
-                        try (InputStreamSource logcatData = mRebootLogcatReceiver.getLogcatData()) {
-                            listener.testLog(LOGCAT_UNLOCK_FILE, LogDataType.TEXT, logcatData);
+                        if (null != mRebootLogcatReceiver) {
+                            try (InputStreamSource logcatData =
+                                    mRebootLogcatReceiver.getLogcatData()) {
+                                listener.testLog(LOGCAT_UNLOCK_FILE, LogDataType.TEXT, logcatData);
+                            }
+                            mRebootLogcatReceiver.stop();
                         }
-                        mRebootLogcatReceiver.stop();
                         listener.testStarted(successiveBootUnlockTestId);
                         listener.testEnded(successiveBootUnlockTestId, successiveBootUnlockResult);
                     }
@@ -598,9 +619,6 @@
             throws DeviceNotAvailableException {
         CLog.v("Waiting for %d msecs before successive boots.", mBootDelayTime);
         getRunUtil().sleep(mBootDelayTime);
-        for (IMetricCollector collector : mCollectors) {
-            listener = collector.init(mInvocationContext, listener);
-        }
         for (int count = 0; count < mBootCount; count++) {
             getDevice().enableAdbRoot();
             // Property used for collecting the perfetto trace file on boot.
@@ -620,10 +638,14 @@
             double onlineTime = INVALID_TIME_DURATION;
             double bootTime = INVALID_TIME_DURATION;
             String testId = String.format("%s.%s$%d", BOOTTIME_TEST, BOOTTIME_TEST, (count + 1));
-            TestDescription successiveBootIterationTestId =
-                    new TestDescription(testId, String.format("%s", SUCCESSIVE_BOOT_TEST));
-            if (mBootTimePerIteration) {
-                listener.testStarted(successiveBootIterationTestId);
+            TestDescription successiveBootIterationTestId;
+            if (!dismissPin) {
+                successiveBootIterationTestId =
+                        new TestDescription(testId, String.format("%s", SUCCESSIVE_BOOT_TEST));
+            } else {
+                successiveBootIterationTestId =
+                        new TestDescription(
+                                testId, String.format("%s", SUCCESSIVE_BOOT_UNLOCK_TEST));
             }
             if (mGranularBootInfo || dismissPin) {
                 clearAndStartLogcat();
@@ -677,12 +699,17 @@
             if (mBootloaderInfo) analyzeBootloaderTimingInfo();
 
             if (dismissPin) {
+                getRunUtil().sleep(2000);
                 mRunner = createRemoteAndroidTestRunner(UNLOCK_PIN_TEST);
                 getDevice().runInstrumentationTests(mRunner, new CollectingTestListener());
-                // Wait for 15 secs after every unlock to make sure home screen is loaded
-                // and logs are printed
-                getRunUtil().sleep(15000);
             }
+
+            if (mBootTimePerIteration) {
+                listener.testStarted(successiveBootIterationTestId);
+            }
+
+            CLog.v("Waiting for %d msecs immediately after successive boot.", mAfterBootDelayTime);
+            getRunUtil().sleep(mAfterBootDelayTime);
             if (onlineTime != INVALID_TIME_DURATION) {
                 if (mBootInfo.containsKey(SUCCESSIVE_ONLINE)) {
                     mBootInfo.get(SUCCESSIVE_ONLINE).add(onlineTime);
@@ -739,6 +766,22 @@
                 if (!collectLogcatInfoResult.isEmpty()) {
                     iterationResult.putAll(collectLogcatInfoResult);
                 }
+                // If  metric-prefix-pattern-for-count is present, calculate the count
+                // of all metrics with the prefix pattern and add the count as a new metric to the
+                // iterationResult map.
+                if (!mMetricPrefixPatternForCount.isEmpty()) {
+                    for (String metricPrefixPattern : mMetricPrefixPatternForCount) {
+                        long metricCount =
+                                iterationResult.entrySet().stream()
+                                        .filter(
+                                                (entry) ->
+                                                        entry.getKey()
+                                                                .startsWith(metricPrefixPattern))
+                                        .count();
+                        iterationResult.put(
+                                metricPrefixPattern + METRIC_COUNT, Long.toString(metricCount));
+                    }
+                }
                 listener.testEnded(successiveBootIterationTestId, iterationResult);
             }
 
@@ -848,10 +891,13 @@
                 Matcher matcherPid = LOGCAT_STATISTICS_PID_PATTERN.matcher(outputList[i]);
                 pidFound = matcherPid.find();
                 if (!pidFound) continue;
-                CLog.d("logcat statistics pid %d output = %s", pid, outputList[i]);
+                CLog.d(
+                        "Process %s with pid %d : logcat statistics output = %s",
+                        processName, pid, outputList[i]);
                 results.put(
                         String.join(METRIC_KEY_SEPARATOR, LOGCAT_STATISTICS_SIZE, processName),
                         matcherPid.group(1).trim());
+                break;
             }
             if (!pidFound) {
                 // the process doesn't found in the logcat statistics output
@@ -860,7 +906,6 @@
                         processName, pid);
             }
         }
-
         return results;
     }