1 /* 2 * Copyright (C) 2020 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.crashtest.app; 18 19 import static com.android.nn.crashtest.app.CrashTestStatus.TestResult.SUCCESS; 20 21 import android.content.Intent; 22 import android.test.ActivityInstrumentationTestCase2; 23 import android.test.UiThreadTest; 24 25 import androidx.test.InstrumentationRegistry; 26 import androidx.test.filters.LargeTest; 27 28 import com.android.nn.benchmark.app.AcceleratorSpecificTestSupport; 29 import com.android.nn.benchmark.app.BenchmarkTestBase; 30 import com.android.nn.benchmark.core.NnApiDelegationFailure; 31 import com.android.nn.benchmark.core.TestModels; 32 33 import org.junit.Before; 34 import org.junit.Rule; 35 import org.junit.Test; 36 import org.junit.rules.TestName; 37 import org.junit.runner.RunWith; 38 import org.junit.runners.Parameterized; 39 40 import java.time.Duration; 41 import java.util.Optional; 42 import java.util.concurrent.ExecutionException; 43 import java.util.concurrent.ExecutorService; 44 import java.util.concurrent.Executors; 45 import java.util.concurrent.Future; 46 47 @RunWith(Parameterized.class) 48 public abstract class NNMultipleProcessTest 49 extends ActivityInstrumentationTestCase2<NNMultiProcessTestActivity> 50 implements AcceleratorSpecificTestSupport { 51 private static final String TAG = "NNMultipleProcessTest"; 52 53 private final ExecutorService mDriverLivenessValidationExecutor = 54 Executors.newSingleThreadExecutor(); 55 protected Optional<TestModels.TestModelEntry> mModelForLivenessTest; 56 protected final String mAcceleratorName; 57 protected final int mProcessCount; 58 protected final int mThreadCount; 59 protected final Duration mDuration; 60 protected final int mFailureRatePercent; 61 62 @Rule 63 public TestName mTestName = new TestName(); 64 NNMultipleProcessTest(int processCount, int threadCount, Duration duration, int failureRatePercent, String acceleratorName)65 public NNMultipleProcessTest(int processCount, int threadCount, Duration duration, 66 int failureRatePercent, String acceleratorName) { 67 super(NNMultiProcessTestActivity.class); 68 mProcessCount = processCount; 69 mThreadCount = threadCount; 70 mDuration = duration; 71 mAcceleratorName = acceleratorName; 72 mFailureRatePercent = failureRatePercent; 73 } 74 75 @Before 76 @Override setUp()77 public void setUp() { 78 injectInstrumentation(InstrumentationRegistry.getInstrumentation()); 79 BenchmarkTestBase.waitUntilCharged(getInstrumentation().getTargetContext(), 90); 80 setActivityIntent(getRunModelsInMultipleProcessesConfigIntent()); 81 } 82 findModelForLivenessTest()83 protected Optional<TestModels.TestModelEntry> findModelForLivenessTest() 84 throws NnApiDelegationFailure { 85 return AcceleratorSpecificTestSupport.findTestModelRunningOnAccelerator( 86 getInstrumentation().getTargetContext(), mAcceleratorName); 87 } 88 89 @Test 90 @LargeTest 91 @UiThreadTest testDriverDoesNotFailWithParallelWorkload()92 public void testDriverDoesNotFailWithParallelWorkload() 93 throws ExecutionException, InterruptedException, NnApiDelegationFailure { 94 final NNMultiProcessTestActivity activity = getActivity(); 95 96 Optional<TestModels.TestModelEntry> modelForLivenessTest = findModelForLivenessTest(); 97 assertTrue("No model available to be run on accelerator " + mAcceleratorName, 98 modelForLivenessTest.isPresent()); 99 100 final DriverLivenessChecker driverLivenessChecker = 101 new DriverLivenessChecker(activity, mAcceleratorName, modelForLivenessTest.get()); 102 Future<Boolean> driverDidNotCrash = 103 mDriverLivenessValidationExecutor.submit(driverLivenessChecker); 104 105 assertEquals(SUCCESS, activity.testResult()); 106 driverLivenessChecker.stop(); 107 assertTrue("Driver shouldn't crash if used by multiple threads and processes", 108 driverDidNotCrash.get()); 109 } 110 111 /** 112 * @return the intent to use to initialise the RunModelsInMultipleProcesses test class 113 */ getRunModelsInMultipleProcessesConfigIntent()114 protected abstract Intent getRunModelsInMultipleProcessesConfigIntent(); 115 }