Migrate pkvm_perf_test.py test_compilation_in_android function to AOSP

testCompilationInAndroid will find the elapsed time when executing
compliation in Android. The result will be collected as a metric.

Bug: 236799228
Test: atest ComposBenchmarkApp
Change-Id: I0a42979339626c44369dd0e19d293dfa8f954014
diff --git a/compos/benchmark/src/java/com/android/compos/benchmark/ComposBenchmark.java b/compos/benchmark/src/java/com/android/compos/benchmark/ComposBenchmark.java
index 4aba3b7..e4fb5ff 100644
--- a/compos/benchmark/src/java/com/android/compos/benchmark/ComposBenchmark.java
+++ b/compos/benchmark/src/java/com/android/compos/benchmark/ComposBenchmark.java
@@ -33,10 +33,16 @@
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.sql.Timestamp;
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
 import java.time.Duration;
+import java.util.Date;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+
 @RunWith(JUnit4.class)
 public class ComposBenchmark {
     private static final String TAG = "ComposBenchmark";
@@ -122,4 +128,58 @@
         mInstrumentation.sendStatus(0, bundle);
     }
 
+    private Timestamp getLatestDex2oatSuccessTime()
+            throws  InterruptedException, IOException, ParseException {
+
+        final String command = "logcat -d -e dex2oat";
+        String output = executeCommand(command);
+        String latestTime = "";
+
+        for (String line : output.split("[\r\n]+")) {
+            Pattern pattern = Pattern.compile("dex2oat64: dex2oat took");
+            Matcher matcher = pattern.matcher(line);
+            if (matcher.find()) {
+                latestTime = line.substring(0, 18);
+            }
+        }
+
+        DateFormat formatter = new SimpleDateFormat("MM-dd hh:mm:ss.SSS");
+        Date date = formatter.parse(latestTime);
+        Timestamp timeStampDate = new Timestamp(date.getTime());
+
+        return timeStampDate;
+    }
+
+    @Test
+    public void testCompilationInAndroid()
+            throws InterruptedException, IOException, ParseException {
+
+        final String command = "/apex/com.android.art/bin/odrefresh --force-compile";
+
+        Long[] compileSecArray = new Long[ROUND_COUNT];
+
+        for (int round = 0; round < ROUND_COUNT; ++round) {
+            Timestamp beforeCompileLatestTime = getLatestDex2oatSuccessTime();
+            Long compileStartTime = System.nanoTime();
+            String output = executeCommand(command);
+            Long compileEndTime = System.nanoTime();
+            Long compileSec = Duration.ofNanos(compileEndTime - compileStartTime).getSeconds();
+            Timestamp afterCompileLatestTime = getLatestDex2oatSuccessTime();
+
+            assertTrue(beforeCompileLatestTime.before(afterCompileLatestTime));
+
+            compileSecArray[round] = compileSec;
+        }
+
+        Long compileSecSum = 0L;
+        for (Long num: compileSecArray) {
+            compileSecSum += num;
+        }
+
+        Bundle bundle = new Bundle();
+        bundle.putLong("compliation_in_android_elapse_second",
+                compileSecSum / compileSecArray.length);
+        mInstrumentation.sendStatus(0, bundle);
+    }
+
 }