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