1 /* 2 * Copyright (C) 2017 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.app.Activity; 20 import android.content.Intent; 21 import android.os.Bundle; 22 import android.util.Log; 23 import android.view.WindowManager; 24 import android.widget.TextView; 25 import com.android.nn.benchmark.core.BenchmarkException; 26 import com.android.nn.benchmark.core.BenchmarkResult; 27 import com.android.nn.benchmark.core.Processor; 28 import com.android.nn.benchmark.core.TestModels.TestModelEntry; 29 import java.io.IOException; 30 import java.util.concurrent.ExecutorService; 31 import java.util.concurrent.Executors; 32 33 public class NNBenchmark extends Activity implements Processor.Callback { 34 public static final String TAG = "NN_BENCHMARK"; 35 36 public static final String EXTRA_ENABLE_LONG = "enable long"; 37 public static final String EXTRA_ENABLE_PAUSE = "enable pause"; 38 public static final String EXTRA_DISABLE_NNAPI = "disable NNAPI"; 39 public static final String EXTRA_TESTS = "tests"; 40 41 public static final String EXTRA_RESULTS_TESTS = "tests"; 42 public static final String EXTRA_RESULTS_RESULTS = "results"; 43 44 private int mTestList[]; 45 46 private Processor mProcessor; 47 private final ExecutorService executorService = Executors.newSingleThreadExecutor(); 48 49 private TextView mTextView; 50 51 // Initialize the parameters for Instrumentation tests. prepareInstrumentationTest()52 protected void prepareInstrumentationTest() { 53 mTestList = new int[1]; 54 mProcessor = new Processor(this, this, mTestList); 55 } 56 setUseNNApi(boolean useNNApi)57 public void setUseNNApi(boolean useNNApi) { 58 mProcessor.setUseNNApi(useNNApi); 59 } 60 setCompleteInputSet(boolean completeInputSet)61 public void setCompleteInputSet(boolean completeInputSet) { 62 mProcessor.setCompleteInputSet(completeInputSet); 63 } 64 65 @Override onCreate(Bundle savedInstanceState)66 protected void onCreate(Bundle savedInstanceState) { 67 super.onCreate(savedInstanceState); 68 mTextView = new TextView(this); 69 mTextView.setTextSize(20); 70 mTextView.setText("Running NN benchmark..."); 71 setContentView(mTextView); 72 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 73 } 74 75 @Override onPause()76 protected void onPause() { 77 super.onPause(); 78 if (mProcessor != null) { 79 mProcessor.exitWithTimeout(30000l); 80 mProcessor = null; 81 } 82 } 83 onBenchmarkFinish(boolean ok)84 public void onBenchmarkFinish(boolean ok) { 85 if (ok) { 86 Intent intent = new Intent(); 87 intent.putExtra(EXTRA_RESULTS_TESTS, mTestList); 88 intent.putExtra(EXTRA_RESULTS_RESULTS, mProcessor.getTestResults()); 89 setResult(RESULT_OK, intent); 90 } else { 91 setResult(RESULT_CANCELED); 92 } 93 finish(); 94 } 95 onStatusUpdate(int testNumber, int numTests, String modelName)96 public void onStatusUpdate(int testNumber, int numTests, String modelName) { 97 runOnUiThread( 98 () -> { 99 mTextView.setText( 100 String.format( 101 "Running test %d of %d: %s", testNumber, numTests, modelName)); 102 }); 103 } 104 105 @Override onResume()106 protected void onResume() { 107 super.onResume(); 108 Intent i = getIntent(); 109 mTestList = i.getIntArrayExtra(EXTRA_TESTS); 110 if (mTestList != null && mTestList.length > 0) { 111 Log.v(TAG, String.format("Starting benchmark with %d test", mTestList.length)); 112 mProcessor = new Processor(this, this, mTestList); 113 mProcessor.setToggleLong(i.getBooleanExtra(EXTRA_ENABLE_LONG, false)); 114 mProcessor.setTogglePause(i.getBooleanExtra(EXTRA_ENABLE_PAUSE, false)); 115 mProcessor.setUseNNApi(!i.getBooleanExtra(EXTRA_DISABLE_NNAPI, false)); 116 executorService.submit(mProcessor); 117 } else { 118 Log.v(TAG, "No test to run, doing nothing"); 119 } 120 } 121 122 @Override onDestroy()123 protected void onDestroy() { 124 super.onDestroy(); 125 } 126 runSynchronously(TestModelEntry testModel, float warmupTimeSeconds, float runTimeSeconds)127 public BenchmarkResult runSynchronously(TestModelEntry testModel, 128 float warmupTimeSeconds, float runTimeSeconds) throws IOException, BenchmarkException { 129 return mProcessor.getInstrumentationResult(testModel, warmupTimeSeconds, runTimeSeconds); 130 } 131 } 132