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 import android.util.Log;
21 import com.android.nn.benchmark.core.InferenceInOutSequence;
22 import com.android.nn.benchmark.core.TestModels;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.concurrent.TimeUnit;
27 import java.util.List;
28 import java.util.stream.Collectors;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.Stopwatch;
32 import org.junit.runners.Parameterized.Parameters;
33 
34 /**
35  * Tests that ensure stability of NNAPI by loading models for a prolonged
36  * period of time.
37  */
38 public class NNModelLoadingStressTest extends BenchmarkTestBase {
39     private static final String TAG = NNModelLoadingStressTest.class.getSimpleName();
40 
41     private static final float WARMUP_SECONDS = 0; // No warmup.
42     private static final float INFERENCE_SECONDS = 0; // No inference.
43     private static final float RUNTIME_SECONDS = 30 * 60;
44 
45     @Rule public Stopwatch stopwatch = new Stopwatch() {};
46 
NNModelLoadingStressTest(TestModels.TestModelEntry model)47     public NNModelLoadingStressTest(TestModels.TestModelEntry model) {
48         super(model);
49     }
50 
51     @Parameters(name = "{0}")
modelsList()52     public static List<TestModels.TestModelEntry> modelsList() {
53         return TestModels.modelsList().stream()
54                 .map(model ->
55                         new TestModels.TestModelEntry(
56                                 model.mModelName,
57                                 model.mBaselineSec,
58                                 model.mInputShape,
59                                 new InferenceInOutSequence.FromAssets[0], // No inputs for inference.
60                                 null,
61                                 model.mTestName,
62                                 model.mModelFile,
63                                 null, // Disable evaluation.
64                                 model.mMinSdkVersion))
65                 .collect(Collectors.collectingAndThen(
66                         Collectors.toList(),
67                         Collections::unmodifiableList));
68     }
69 
70     @Test
71     @LargeTest
stressTestNNAPI()72     public void stressTestNNAPI() throws IOException {
73         waitUntilCharged();
74         setUseNNApi(true);
75         setCompleteInputSet(true);
76         float endTime = stopwatch.runtime(TimeUnit.SECONDS) + RUNTIME_SECONDS;
77         TestAction ta = new TestAction(mModel, WARMUP_SECONDS, INFERENCE_SECONDS);
78         while (stopwatch.runtime(TimeUnit.SECONDS) < endTime) {
79             runTest(ta, mModel.getTestName());
80         }
81     }
82 }
83