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 android.content.Intent;
20 import android.test.ActivityInstrumentationTestCase2;
21 import android.test.UiThreadTest;
22 
23 import androidx.test.InstrumentationRegistry;
24 
25 import com.android.nn.benchmark.app.BenchmarkTestBase;
26 import com.android.nn.crashtest.core.test.PerformanceDegradationTest;
27 
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.TestName;
32 import org.junit.runner.RunWith;
33 import org.junit.runners.Parameterized;
34 import org.junit.runners.Parameterized.Parameters;
35 
36 import java.util.Collections;
37 
38 @RunWith(Parameterized.class)
39 public class NNPerformanceDegradationTest extends
40         ActivityInstrumentationTestCase2<NNPerformanceDegradationTestActivity> implements
41         AcceleratorSpecificTestSupport {
42     public static final String TAG = PerformanceDegradationTest.TAG;
43 
44 
45     @Parameters(name = "Threshold {1} % and {0} extra compiling threads on accelerator {2}")
testConfiguration()46     public static Iterable<Object[]> testConfiguration() {
47         return AcceleratorSpecificTestSupport.perAcceleratorTestConfig(
48                 Collections.singletonList(
49                         new Object[]{/*thread count*/1, /*max degradation percent*/50}));
50     }
51 
NNPerformanceDegradationTest(int threadCount, int maxPerformanceDegradationPercent, String acceleratorName)52     public NNPerformanceDegradationTest(int threadCount,
53             int maxPerformanceDegradationPercent, String acceleratorName) {
54         super(NNPerformanceDegradationTestActivity.class);
55         mAcceleratorName = acceleratorName;
56         mThreadCount = threadCount;
57         mMaxPerformanceDegradationPercent = maxPerformanceDegradationPercent;
58     }
59 
60 
61     @Rule
62     public TestName mTestName = new TestName();
63 
64     private static final float WARM_UP_TIME_SECONDS = 5;
65     private static final float RUN_TIME_SECONDS = 20;
66 
67     private final String mAcceleratorName;
68     private final int mThreadCount;
69     private final int mMaxPerformanceDegradationPercent;
70 
getTestMaxPerfDegradationOfModelWIthThreads(String testName, String acceleratorName, int threadCount, int maxPerformanceDegradationPercent)71     protected static Intent getTestMaxPerfDegradationOfModelWIthThreads(String testName,
72             String acceleratorName, int threadCount,
73             int maxPerformanceDegradationPercent) {
74         Intent result = new Intent();
75         PerformanceDegradationTest.intentInitializer(WARM_UP_TIME_SECONDS, RUN_TIME_SECONDS,
76                 acceleratorName, threadCount, maxPerformanceDegradationPercent,
77                 testName).addIntentParams(result);
78         return result;
79     }
80 
81     @Test
82     @UiThreadTest
shouldNotDegradePerformanceOverThreshold()83     public void shouldNotDegradePerformanceOverThreshold() {
84         CrashTestStatus.TestResult testResult = getActivity().testResult();
85         assertEquals("Test didn't complete successfully", CrashTestStatus.TestResult.SUCCESS,
86                 testResult);
87     }
88 
89     @Before
90     @Override
setUp()91     public void setUp() {
92         injectInstrumentation(InstrumentationRegistry.getInstrumentation());
93         BenchmarkTestBase.waitUntilCharged(getInstrumentation().getTargetContext(), 60);
94         setActivityIntent(getTestMaxPerfDegradationOfModelWIthThreads(mTestName.getMethodName(),
95                 mAcceleratorName,
96                 mThreadCount,
97                 mMaxPerformanceDegradationPercent));
98     }
99 
100 
101 }
102