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.tests.sysmem.host; 18 19 import com.android.tradefed.device.ITestDevice; 20 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; 21 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner.TestLogData; 22 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner.TestMetrics; 23 import com.android.tradefed.testtype.IDeviceTest; 24 25 import org.junit.Rule; 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 29 /** 30 * Runs a system memory test. 31 */ 32 @RunWith(DeviceJUnit4ClassRunner.class) 33 public class MemoryTest implements IDeviceTest { 34 35 @Rule public TestMetrics testMetrics = new TestMetrics(); 36 @Rule public TestLogData testLogs = new TestLogData(); 37 38 private ITestDevice mTestDevice; 39 private int mIterations = 0; // Number of times cujs have been run. 40 private Metrics mMetrics; 41 private Cujs mCujs; 42 43 @Override setDevice(ITestDevice testDevice)44 public void setDevice(ITestDevice testDevice) { 45 mTestDevice = testDevice; 46 Device device = new Device(testDevice); 47 mMetrics = new Metrics(device, testMetrics, testLogs); 48 mCujs = new Cujs(device); 49 } 50 51 @Override getDevice()52 public ITestDevice getDevice() { 53 return mTestDevice; 54 } 55 56 // Invoke a single iteration of running the cujs. runCujs()57 private void runCujs() throws TestException { 58 mCujs.run(); 59 mIterations++; 60 } 61 62 // Sample desired memory. sample()63 private void sample() throws TestException { 64 mMetrics.sample(String.format("%03d", mIterations)); 65 } 66 67 /** 68 * Runs the memory tests. 69 */ 70 @Test run()71 public void run() throws TestException { 72 sample(); // Sample before running cujs 73 runCujs(); 74 sample(); // Sample after first iteration of cujs 75 76 // Run cujs in a loop to highlight memory leaks. 77 for (int i = 0; i < 5; ++i) { 78 for (int j = 0; j < 5; j++) { 79 runCujs(); 80 } 81 sample(); 82 } 83 } 84 } 85