• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.game.qualification.test;
18 
19 import org.junit.runner.RunWith;
20 import org.junit.Test;
21 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
22 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
23 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner.TestMetrics;
24 import com.android.tradefed.metrics.proto.MetricMeasurement.DataType;
25 import com.android.tradefed.metrics.proto.MetricMeasurement.Measurements;
26 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric;
27 import com.android.tradefed.device.DeviceNotAvailableException;
28 import com.android.tradefed.device.NativeDevice;
29 import com.android.tradefed.result.InputStreamSource;
30 import com.android.tradefed.log.LogUtil.CLog;
31 import static org.junit.Assert.assertEquals;
32 import static org.junit.Assert.assertTrue;
33 import org.junit.Rule;
34 import java.io.InputStream;
35 import java.io.IOException;
36 import java.io.BufferedReader;
37 import java.io.InputStreamReader;
38 
39 @RunWith(DeviceJUnit4ClassRunner.class)
40 public class MemoryTests extends BaseHostJUnit4Test {
41 
42     @Rule
43     public TestMetrics metrics = new TestMetrics();
44 
45     /**
46      * Device must be able to allocate at least 2.3GB of memory.
47      */
48     @Test
testMemoryAllocationLimit()49     public void testMemoryAllocationLimit()
50         throws DeviceNotAvailableException, IOException {
51             getDevice().startLogcat();
52             getDevice().clearLogcat();
53 
54             String pkgname = "com.android.game.qualification.allocstress";
55             String actname = pkgname + ".MainActivity";
56             getDevice().executeShellCommand("am start -W " + pkgname + "/" + actname);
57 
58             // Wait until app is finished.
59             while((getDevice().executeShellCommand("dumpsys activity | grep top-activity")).contains("allocstress"));
60 
61             boolean hasAllocStats = false;
62             int totalAllocated = 0;
63 
64             try (
65                 InputStreamSource logcatSource = getDevice().getLogcat();
66                 BufferedReader logcat = new BufferedReader(new InputStreamReader(logcatSource.createInputStream()));
67             ) {
68 
69                 String s = logcat.readLine();
70                 String pattern = "total alloc: ";
71                 String p = null;
72                 while (s != null) {
73                     if (s.contains(pattern)) {
74                         hasAllocStats = true;
75                         p = s;
76                     }
77                     s = logcat.readLine();
78                 }
79                 int totalAllocIndex = p.indexOf(pattern) + pattern.length();
80                 totalAllocated = Integer.parseInt(p.substring(totalAllocIndex));
81 
82             }
83 
84             getDevice().stopLogcat();
85 
86             assertTrue(hasAllocStats);
87             metrics.addTestMetric("memory_allocated", Metric.newBuilder()
88                                                             .setType(DataType.RAW)
89                                                             .setMeasurements(Measurements.newBuilder().setSingleInt(totalAllocated))
90                                                             .build());
91             assertTrue("Device failed to allocate an appropriate amount of memory (2.3GB) before being killed", totalAllocated > 2300);
92     }
93 
94 }
95