1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.nn.benchmark.app; 18 19 import android.test.suitebuilder.annotation.LargeTest; 20 import androidx.test.InstrumentationRegistry; 21 import com.android.nn.benchmark.core.TestModels; 22 import java.io.IOException; 23 import org.junit.Test; 24 import org.junit.runner.RunWith; 25 import org.junit.runners.Parameterized; 26 import android.util.Log; 27 28 /** 29 * NNAPI benchmark test. 30 * To run the test, please use command 31 * 32 * adb shell am instrument 33 * -e class "com.android.nn.benchmark.app.NNCrystalBallTest 34 * -w com.android.nn.benchmark.app/androidx.test.runner.AndroidJUnitRunner 35 * 36 * To run only one model, please run: 37 * adb shell am instrument 38 * -e class "com.android.nn.benchmark.app.NNCrystalBallTest#testNNAPI[MODEL_NAME]" 39 * -w com.android.nn.benchmark.app/androidx.test.runner.AndroidJUnitRunner 40 * 41 */ 42 @RunWith(Parameterized.class) 43 public class NNCrystalBallTest extends BenchmarkTestBase { 44 NNCrystalBallTest(TestModels.TestModelEntry model, String acceleratorName)45 public NNCrystalBallTest(TestModels.TestModelEntry model, String acceleratorName) { 46 super(model, acceleratorName); 47 } 48 test(boolean useNnapi, boolean useCompleteInputSet)49 private void test(boolean useNnapi, boolean useCompleteInputSet) throws IOException { 50 setUseNNApi(useNnapi); 51 setCompleteInputSet(useCompleteInputSet); 52 if (useNnapi && mAcceleratorName != null) { 53 Log.i(NNBenchmark.TAG, "Using accelerator " + mAcceleratorName); 54 setNnApiAcceleratorName(mAcceleratorName); 55 } 56 enableCompilationCachingBenchmarks(); 57 TestAction ta = new TestAction(mModel, WARMUP_REPEATABLE_SECONDS, 58 useCompleteInputSet ? COMPLETE_SET_TIMEOUT_SECOND : RUNTIME_REPEATABLE_SECONDS); 59 runTest(ta, mModel.getTestName()); 60 61 // Sends metric results to the instrumentation status output. 62 InstrumentationRegistry.getInstrumentation().sendStatus( 63 0, ta.getBenchmark().toBundle(mModel.getTestName())); 64 } 65 66 @Test 67 @LargeTest testTFLite()68 public void testTFLite() throws IOException { 69 test(false, false); 70 } 71 72 @Test 73 @LargeTest testNNAPI()74 public void testNNAPI() throws IOException { 75 test(true, true); 76 } 77 78 } 79