Run compilation only in client early termination tests

Executes only model compilation in NNClientEarlyTerminationTest to
increase the possibility of the driver having an open channel with the
client (compilation is executed asynchronously).

Test: build_and_run_benchmark.sh client-early-termination-stress
Change-Id: I50f26a6230f62ac7c7919e8088932e16dda1866f
diff --git a/crashtest/src/com/android/nn/crashtest/MainActivity.java b/crashtest/src/com/android/nn/crashtest/MainActivity.java
index e7a0b4d..80c6cb2 100644
--- a/crashtest/src/com/android/nn/crashtest/MainActivity.java
+++ b/crashtest/src/com/android/nn/crashtest/MainActivity.java
@@ -255,7 +255,7 @@
         coordinator.startTest(RunModelsInParallel.class,
                 RunModelsInParallel.intentInitializer(testList, threadCount,
                         Duration.ofMinutes(testDurationMinutes),
-                        testName, acceleratorName, false),
+                        testName, acceleratorName, false, false),
                 testCompletionListener,
                 mUseSeparateProcess.get(), testName);
 
diff --git a/src/com/android/nn/benchmark/app/NNClientEarlyTerminationTest.java b/src/com/android/nn/benchmark/app/NNClientEarlyTerminationTest.java
index e921db6..3ef56a0 100644
--- a/src/com/android/nn/benchmark/app/NNClientEarlyTerminationTest.java
+++ b/src/com/android/nn/benchmark/app/NNClientEarlyTerminationTest.java
@@ -99,7 +99,7 @@
     @Override
     public void setUp() {
         injectInstrumentation(InstrumentationRegistry.getInstrumentation());
-        final Intent runSomeInferencesInASeparateProcess = runSupportedModelsOnNThreadsFor(
+        final Intent runSomeInferencesInASeparateProcess = compileSupportedModelsOnNThreadsFor(
                 NNAPI_CLIENTS_COUNT,
                 MAX_SEPARATE_PROCESS_EXECUTION_TIME);
         setActivityIntent(runSomeInferencesInASeparateProcess);
@@ -149,7 +149,7 @@
                 driverDidNotCrash.get());
     }
 
-    private Intent runSupportedModelsOnNThreadsFor(int threadCount, Duration testDuration) {
+    private Intent compileSupportedModelsOnNThreadsFor(int threadCount, Duration testDuration) {
         Intent intent = new Intent();
         intent.putExtra(
                 NNParallelTestActivity.EXTRA_TEST_LIST, IntStream.range(0,
@@ -160,6 +160,7 @@
         intent.putExtra(NNParallelTestActivity.EXTRA_TEST_NAME, mTestName.getMethodName());
         intent.putExtra(NNParallelTestActivity.EXTRA_ACCELERATOR_NAME, mAcceleratorName);
         intent.putExtra(NNParallelTestActivity.EXTRA_IGNORE_UNSUPPORTED_MODELS, true);
+        intent.putExtra(NNParallelTestActivity.EXTRA_RUN_MODEL_COMPILATION_ONLY, true);
         return intent;
     }
 
diff --git a/src/com/android/nn/benchmark/app/NNParallelTestActivity.java b/src/com/android/nn/benchmark/app/NNParallelTestActivity.java
index 461d808..ce17abf 100644
--- a/src/com/android/nn/benchmark/app/NNParallelTestActivity.java
+++ b/src/com/android/nn/benchmark/app/NNParallelTestActivity.java
@@ -55,7 +55,7 @@
     public static final String EXTRA_TEST_NAME = "test_name";
     public static final String EXTRA_ACCELERATOR_NAME = "accelerator_name";
     public static final String EXTRA_IGNORE_UNSUPPORTED_MODELS = "ignore_unsupported_models";
-
+    public static final String EXTRA_RUN_MODEL_COMPILATION_ONLY = "run_model_compilation_only";
 
     public static enum TestResult {
         SUCCESS,
@@ -162,11 +162,14 @@
         boolean ignoreUnsupportedModels = intent.getBooleanExtra(EXTRA_IGNORE_UNSUPPORTED_MODELS,
                 false);
 
-        final long testTimeoutMillis = (long) (testDurationMillis * 1.5);
+        final boolean runModelCompilationOnly = intent.getBooleanExtra(
+                EXTRA_RUN_MODEL_COMPILATION_ONLY, false);
+
         mCoordinator.startTest(RunModelsInParallel.class,
                 RunModelsInParallel.intentInitializer(testList, threadCount,
                         Duration.ofMillis(testDurationMillis),
-                        mTestName, acceleratorName, ignoreUnsupportedModels),
+                        mTestName, acceleratorName, ignoreUnsupportedModels,
+                        runModelCompilationOnly),
                 testCompletionListener,
                 runInSeparateProcess, mTestName);
 
diff --git a/src/com/android/nn/benchmark/core/Processor.java b/src/com/android/nn/benchmark/core/Processor.java
index c9799f5..a265cb5 100644
--- a/src/com/android/nn/benchmark/core/Processor.java
+++ b/src/com/android/nn/benchmark/core/Processor.java
@@ -24,6 +24,7 @@
 import android.util.Pair;
 
 import java.io.IOException;
+import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.atomic.AtomicBoolean;
@@ -31,6 +32,7 @@
 /** Processor is a helper thread for running the work without blocking the UI thread. */
 public class Processor implements Runnable {
 
+
     public interface Callback {
         void onBenchmarkFinish(boolean ok);
 
@@ -57,6 +59,7 @@
     private boolean mTogglePause;
     private String mAcceleratorName;
     private boolean mIgnoreUnsupportedModels;
+    private boolean mRunModelCompilationOnly;
 
     public Processor(Context context, Processor.Callback callback, int[] testList) {
         mContext = context;
@@ -67,6 +70,7 @@
         }
         mAcceleratorName = null;
         mIgnoreUnsupportedModels = false;
+        mRunModelCompilationOnly = false;
     }
 
     public void setUseNNApi(boolean useNNApi) {
@@ -93,6 +97,10 @@
         mIgnoreUnsupportedModels = value;
     }
 
+    public void setRunModelCompilationOnly(boolean value) {
+        mRunModelCompilationOnly = value;
+    }
+
     // Method to retrieve benchmark results for instrumentation tests.
     public BenchmarkResult getInstrumentationResult(
             TestModels.TestModelEntry t, float warmupTimeSeconds, float runTimeSeconds)
@@ -277,14 +285,22 @@
                 }
             }
 
-            // Run the test
-            float warmupTime = 0.3f;
-            float runTime = 1.f;
-            if (mToggleLong) {
-                warmupTime = 2.f;
-                runTime = 10.f;
+            if (mRunModelCompilationOnly) {
+                mTestResults[ct] = BenchmarkResult.fromInferenceResults(testModel.mTestName,
+                        mUseNNApi
+                                ? BenchmarkResult.BACKEND_TFLITE_NNAPI
+                                : BenchmarkResult.BACKEND_TFLITE_CPU, Collections.emptyList(),
+                        Collections.emptyList(), null);
+            } else {
+                // Run the test
+                float warmupTime = 0.3f;
+                float runTime = 1.f;
+                if (mToggleLong) {
+                    warmupTime = 2.f;
+                    runTime = 10.f;
+                }
+                mTestResults[ct] = getBenchmark(warmupTime, runTime);
             }
-            mTestResults[ct] = getBenchmark(warmupTime, runTime);
         }
     }
 
diff --git a/src/com/android/nn/benchmark/crashtest/test/RunModelsInParallel.java b/src/com/android/nn/benchmark/crashtest/test/RunModelsInParallel.java
index e720544..b6ba74f 100644
--- a/src/com/android/nn/benchmark/crashtest/test/RunModelsInParallel.java
+++ b/src/com/android/nn/benchmark/crashtest/test/RunModelsInParallel.java
@@ -48,10 +48,11 @@
     private static final String TEST_NAME = "test_name";
     private static final String ACCELERATOR_NAME = "accelerator_name";
     private static final String IGNORE_UNSUPPORTED_MODELS = "ignore_unsupported_models";
+    private static final String RUN_MODEL_COMPILATION_ONLY = "run_model_compilation_only";
 
     static public CrashTestIntentInitializer intentInitializer(int[] models, int threadCount,
             Duration duration, String testName, String acceleratorName,
-            boolean ignoreUnsupportedModels) {
+            boolean ignoreUnsupportedModels, boolean runModelCompilationOnly) {
         return intent -> {
             intent.putExtra(MODELS, models);
             intent.putExtra(DURATION, duration.toMillis());
@@ -59,6 +60,7 @@
             intent.putExtra(TEST_NAME, testName);
             intent.putExtra(ACCELERATOR_NAME, acceleratorName);
             intent.putExtra(IGNORE_UNSUPPORTED_MODELS, ignoreUnsupportedModels);
+            intent.putExtra(RUN_MODEL_COMPILATION_ONLY, runModelCompilationOnly);
         };
     }
 
@@ -69,6 +71,7 @@
     private String mAcceleratorName;
     private boolean mIgnoreUnsupportedModels;
     private Context mContext;
+    private boolean mRunModelCompilationOnly;
 
     private ExecutorService mExecutorService = null;
     private final Set<Processor> activeTests = new HashSet<>();
@@ -87,6 +90,7 @@
         mAcceleratorName = configParams.getStringExtra(ACCELERATOR_NAME);
         mIgnoreUnsupportedModels = mAcceleratorName != null && configParams.getBooleanExtra(
                 IGNORE_UNSUPPORTED_MODELS, false);
+        mRunModelCompilationOnly = configParams.getBooleanExtra(RUN_MODEL_COMPILATION_ONLY, false);
         mContext = context;
         mProgressListener = progressListener.orElseGet(() -> (Optional<String> message) -> {
             Log.v(CrashTest.TAG, message.orElse("."));
@@ -126,6 +130,7 @@
         result.setCompleteInputSet(false);
         result.setNnApiAcceleratorName(mAcceleratorName);
         result.setIgnoreUnsupportedModels(mIgnoreUnsupportedModels);
+        result.setRunModelCompilationOnly(mRunModelCompilationOnly);
         return result;
     }