1 /*
2  * Copyright (C) 2019 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.test.suitebuilder.annotation.LargeTest;
20 
21 import com.android.nn.benchmark.core.TestModels;
22 
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.junit.runners.Parameterized;
26 import org.junit.runners.Parameterized.Parameters;
27 
28 import java.io.IOException;
29 import java.time.Duration;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.stream.Collectors;
33 
34 /**
35  * Tests that ensure stability of NNAPI by running inference for a
36  * prolonged period of time.
37  */
38 @RunWith(Parameterized.class)
39 public class NNInferenceStressTest extends BenchmarkTestBase {
40     private static final String TAG = NNInferenceStressTest.class.getSimpleName();
41 
42     private static final float WARMUP_SECONDS = 0; // No warmup.
43     private static final float RUNTIME_SECONDS = Duration.ofHours(1).getSeconds();
44     private static final long LONG_STRESS_TEST_DURATION_SECONDS = Duration.ofHours(4).getSeconds();
45 
NNInferenceStressTest(TestModels.TestModelEntry model)46     public NNInferenceStressTest(TestModels.TestModelEntry model) {
47         super(model);
48     }
49 
50     @Parameters(name = "{0}")
modelsList()51     public static List<TestModels.TestModelEntry> modelsList() {
52         return TestModels.modelsList().stream()
53                 .map(TestModels.TestModelEntry::withDisabledEvaluation)
54                 .collect(Collectors.collectingAndThen(
55                         Collectors.toList(),
56                         Collections::unmodifiableList));
57     }
58 
59     @Test
60     @LargeTest
stressTestNNAPI()61     public void stressTestNNAPI() throws IOException {
62         waitUntilCharged();
63         setUseNNApi(true);
64         setCompleteInputSet(false);
65         // Will sample results for very long tests in order to avoid the results to saturate
66         // available memory.
67         final boolean shouldSampleResults = RUNTIME_SECONDS >= LONG_STRESS_TEST_DURATION_SECONDS;
68         TestAction ta = new TestAction(mModel, WARMUP_SECONDS, RUNTIME_SECONDS,
69                 shouldSampleResults);
70         runTest(ta, mModel.getTestName());
71     }
72 }
73