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