Change scope of elapsed time for subprocess

Ensure a more accurate elapsed time for the tests in the
subprocess. Exclude all our post processing of results
from it.

Test: local runs
Bug: 62356171
Change-Id: If53af1148a96ad9115c89afad2a8416a27b9d333
diff --git a/src/com/android/tradefed/testtype/SubprocessTfLauncher.java b/src/com/android/tradefed/testtype/SubprocessTfLauncher.java
index 50a1cf1..42cd03b 100644
--- a/src/com/android/tradefed/testtype/SubprocessTfLauncher.java
+++ b/src/com/android/tradefed/testtype/SubprocessTfLauncher.java
@@ -197,8 +197,9 @@
      *
      * @param listener the original {@link ITestInvocationListener} where to report results.
      * @param exception True if exception was raised inside the test.
+     * @param elapsedTime the time taken to run the tests.
      */
-    protected void postRun(ITestInvocationListener listener, boolean exception) {}
+    protected void postRun(ITestInvocationListener listener, boolean exception, long elapsedTime) {}
 
     /** Pipe to the subprocess the invocation-data so that it can use them if needed. */
     private void addInvocationData() {
@@ -229,6 +230,7 @@
         FileOutputStream stderr = null;
 
         boolean exception = false;
+        long startTime = 0l;
         try {
             stdoutFile = FileUtil.createTempFile("stdout_subprocess_", ".log");
             stderrFile = FileUtil.createTempFile("stderr_subprocess_", ".log");
@@ -244,7 +246,7 @@
                 mCmdArgs.add("--subprocess-report-file");
                 mCmdArgs.add(eventFile.getAbsolutePath());
             }
-
+            startTime = System.currentTimeMillis();
             CommandResult result = mRunUtil.runTimedCmd(mMaxTfRunTime, stdout,
                     stderr, mCmdArgs.toArray(new String[0]));
             // We possibly allow for a little more time if the thread is still processing events.
@@ -276,6 +278,7 @@
             exception = true;
             throw new RuntimeException(e);
         } finally {
+            long elapsedTime = System.currentTimeMillis() - startTime;
             StreamUtil.close(stdout);
             StreamUtil.close(stderr);
             logAndCleanFile(stdoutFile, listener);
@@ -286,7 +289,7 @@
             }
             StreamUtil.close(eventParser);
 
-            postRun(listener, exception);
+            postRun(listener, exception, elapsedTime);
 
             if (mTmpDir != null) {
                 FileUtil.recursiveDelete(mTmpDir);
diff --git a/src/com/android/tradefed/testtype/TfTestLauncher.java b/src/com/android/tradefed/testtype/TfTestLauncher.java
index 3ac2c0b..f04bd46 100644
--- a/src/com/android/tradefed/testtype/TfTestLauncher.java
+++ b/src/com/android/tradefed/testtype/TfTestLauncher.java
@@ -102,8 +102,6 @@
     private File mHprofFile = null;
     // A {@link File} pointing to the jacoco args jar file extracted from the resources
     private File mAgent = null;
-    // we track the elapsed time of the invocation to report it.
-    private long mStartTime = 0l;
 
     /** {@inheritDoc} */
     @Override
@@ -179,17 +177,13 @@
                             apk);
             mCmdArgs.add(apkPath);
         }
-
-        mStartTime = System.currentTimeMillis();
     }
 
-    /**
-     * {@inheritDoc}
-     */
+    /** {@inheritDoc} */
     @Override
-    protected void postRun(ITestInvocationListener listener, boolean exception) {
-        super.postRun(listener, exception);
-        reportMetrics(System.currentTimeMillis() - mStartTime, listener);
+    protected void postRun(ITestInvocationListener listener, boolean exception, long elapsedTime) {
+        super.postRun(listener, exception, elapsedTime);
+        reportMetrics(elapsedTime, listener);
         FileUtil.deleteFile(mAgent);
 
         // Evaluate coverage from the subprocess