diff options
5 files changed, 143 insertions, 69 deletions
diff --git a/tests/SystemMemoryTest/host/src/com/android/tests/sysmem/host/Cujs.java b/tests/SystemMemoryTest/host/src/com/android/tests/sysmem/host/Cujs.java index 1b638747ee09..b605e62db0f2 100644 --- a/tests/SystemMemoryTest/host/src/com/android/tests/sysmem/host/Cujs.java +++ b/tests/SystemMemoryTest/host/src/com/android/tests/sysmem/host/Cujs.java @@ -16,24 +16,21 @@ package com.android.tests.sysmem.host; -import com.android.tradefed.device.DeviceNotAvailableException; -import com.android.tradefed.device.ITestDevice; - /** * Critical user journeys with which to exercise the system, driven from the * host. */ public class Cujs { - private ITestDevice mDevice; + private Device mDevice; - public Cujs(ITestDevice device) { + public Cujs(Device device) { this.mDevice = device; } /** * Runs the critical user journeys. */ - public void run() throws DeviceNotAvailableException { + public void run() throws TestException { // Invoke the Device Cujs instrumentation to run the cujs. // TODO: Consider exercising the system in other interesting ways as // well. diff --git a/tests/SystemMemoryTest/host/src/com/android/tests/sysmem/host/Device.java b/tests/SystemMemoryTest/host/src/com/android/tests/sysmem/host/Device.java new file mode 100644 index 000000000000..03503cec5fec --- /dev/null +++ b/tests/SystemMemoryTest/host/src/com/android/tests/sysmem/host/Device.java @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.tests.sysmem.host; + +import com.android.tradefed.device.DeviceNotAvailableException; +import com.android.tradefed.device.ITestDevice; + +import java.util.InputMismatchException; +import java.util.Scanner; + +/** + * Wrapper around ITestDevice exposing useful device functions. + */ +class Device { + + private ITestDevice mDevice; + + Device(ITestDevice device) { + mDevice = device; + } + + /** + * Execute a shell command and return the output as a string. + */ + public String executeShellCommand(String command) throws TestException { + try { + return mDevice.executeShellCommand(command); + } catch (DeviceNotAvailableException e) { + throw new TestException(e); + } + } + + /** + * Enable adb root + */ + public void enableAdbRoot() throws TestException { + try { + mDevice.enableAdbRoot(); + } catch (DeviceNotAvailableException e) { + throw new TestException(e); + } + } + + /** + * Returns the pid for the process with the given name. + */ + public int getPidForProcess(String name) throws TestException { + String psout = executeShellCommand("ps -A -o PID,CMD"); + Scanner sc = new Scanner(psout); + try { + // ps output is of the form: + // PID CMD + // 1 init + // 2 kthreadd + // ... + // 9693 ps + sc.nextLine(); + while (sc.hasNextLine()) { + int pid = sc.nextInt(); + String cmd = sc.next(); + + if (name.equals(cmd)) { + return pid; + } + } + } catch (InputMismatchException e) { + throw new TestException("unexpected ps output format: " + psout, e); + } + + throw new TestException("failed to get pid for process " + name); + } +} diff --git a/tests/SystemMemoryTest/host/src/com/android/tests/sysmem/host/MemoryTest.java b/tests/SystemMemoryTest/host/src/com/android/tests/sysmem/host/MemoryTest.java index cfd598da1a5f..48b7f393d0a3 100644 --- a/tests/SystemMemoryTest/host/src/com/android/tests/sysmem/host/MemoryTest.java +++ b/tests/SystemMemoryTest/host/src/com/android/tests/sysmem/host/MemoryTest.java @@ -16,7 +16,6 @@ package com.android.tests.sysmem.host; -import com.android.tradefed.device.DeviceNotAvailableException; import com.android.tradefed.device.ITestDevice; import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; import com.android.tradefed.testtype.DeviceJUnit4ClassRunner.TestLogData; @@ -27,8 +26,6 @@ import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; -import java.io.IOException; - /** * Runs a system memory test. */ @@ -44,8 +41,9 @@ public class MemoryTest implements IDeviceTest { private Cujs mCujs; @Override - public void setDevice(ITestDevice device) { - mTestDevice = device; + public void setDevice(ITestDevice testDevice) { + mTestDevice = testDevice; + Device device = new Device(testDevice); mMetrics = new Metrics(device, testMetrics, testLogs); mCujs = new Cujs(device); } @@ -56,14 +54,13 @@ public class MemoryTest implements IDeviceTest { } // Invoke a single iteration of running the cujs. - private void runCujs() throws DeviceNotAvailableException { + private void runCujs() throws TestException { mCujs.run(); mIterations++; } // Sample desired memory. - private void sample() - throws DeviceNotAvailableException, IOException, Metrics.MetricsException { + private void sample() throws TestException { mMetrics.sample(String.format("%03d", mIterations)); } @@ -71,7 +68,7 @@ public class MemoryTest implements IDeviceTest { * Runs the memory tests. */ @Test - public void run() throws Exception { + public void run() throws TestException { sample(); // Sample before running cujs runCujs(); sample(); // Sample after first iteration of cujs diff --git a/tests/SystemMemoryTest/host/src/com/android/tests/sysmem/host/Metrics.java b/tests/SystemMemoryTest/host/src/com/android/tests/sysmem/host/Metrics.java index 70bc22c485e1..b408a81d8f93 100644 --- a/tests/SystemMemoryTest/host/src/com/android/tests/sysmem/host/Metrics.java +++ b/tests/SystemMemoryTest/host/src/com/android/tests/sysmem/host/Metrics.java @@ -16,8 +16,6 @@ package com.android.tests.sysmem.host; -import com.android.tradefed.device.DeviceNotAvailableException; -import com.android.tradefed.device.ITestDevice; import com.android.tradefed.result.FileInputStreamSource; import com.android.tradefed.result.LogDataType; import com.android.tradefed.testtype.DeviceJUnit4ClassRunner.TestLogData; @@ -34,24 +32,11 @@ import java.util.Scanner; */ class Metrics { - private ITestDevice mDevice; + private Device mDevice; private TestMetrics mMetrics; private TestLogData mLogs; /** - * Exception thrown in case of error sampling metrics. - */ - public static class MetricsException extends Exception { - MetricsException(String msg) { - super(msg); - } - - MetricsException(String msg, Exception cause) { - super(msg, cause); - } - } - - /** * Constructs a metrics instance that will output high level metrics and * more detailed breakdowns using the given <code>metrics</code> and * <code>logs</code> objects. @@ -60,7 +45,7 @@ class Metrics { * @param metrics where to log the high level metrics when taking a sample * @param logs where to log detailed breakdowns when taking a sample */ - Metrics(ITestDevice device, TestMetrics metrics, TestLogData logs) { + Metrics(Device device, TestMetrics metrics, TestLogData logs) { this.mDevice = device; this.mMetrics = metrics; this.mLogs = logs; @@ -69,43 +54,17 @@ class Metrics { /** * Writes the given <code>text</code> to a log with the given label. */ - private void logText(String label, String text) throws IOException { - File file = File.createTempFile(label, "txt"); - PrintStream ps = new PrintStream(file); - ps.print(text); - try (FileInputStreamSource dataStream = new FileInputStreamSource(file)) { - mLogs.addTestLog(label, LogDataType.TEXT, dataStream); - } - } - - /** - * Returns the pid for the process with the given name. - */ - private int getPidForProcess(String name) - throws DeviceNotAvailableException, IOException, MetricsException { - String psout = mDevice.executeShellCommand("ps -A -o PID,CMD"); - Scanner sc = new Scanner(psout); + private void logText(String label, String text) throws TestException { try { - // ps output is of the form: - // PID CMD - // 1 init - // 2 kthreadd - // ... - // 9693 ps - sc.nextLine(); - while (sc.hasNextLine()) { - int pid = sc.nextInt(); - String cmd = sc.next(); - - if (name.equals(cmd)) { - return pid; - } + File file = File.createTempFile(label, "txt"); + PrintStream ps = new PrintStream(file); + ps.print(text); + try (FileInputStreamSource dataStream = new FileInputStreamSource(file)) { + mLogs.addTestLog(label, LogDataType.TEXT, dataStream); } - } catch (InputMismatchException e) { - throw new MetricsException("unexpected ps output format: " + psout, e); + } catch (IOException e) { + throw new TestException(e); } - - throw new MetricsException("failed to get pid for process " + name); } /** @@ -116,11 +75,11 @@ class Metrics { * * @param label prefix to use for metrics and logs output for this sample. */ - void sample(String label) throws DeviceNotAvailableException, IOException, MetricsException { + void sample(String label) throws TestException { // adb root access is required to get showmap mDevice.enableAdbRoot(); - int pid = getPidForProcess("system_server"); + int pid = mDevice.getPidForProcess("system_server"); // Read showmap for system server and add it as a test log String showmap = mDevice.executeShellCommand("showmap " + pid); @@ -146,7 +105,7 @@ class Metrics { mMetrics.addTestMetric(label + ".system_server.rss", Long.toString(rss)); mMetrics.addTestMetric(label + ".system_server.pss", Long.toString(pss)); } catch (InputMismatchException e) { - throw new MetricsException("unexpected showmap format", e); + throw new TestException("unexpected showmap format", e); } // Run debuggerd -j to get GC stats for system server and add it as a diff --git a/tests/SystemMemoryTest/host/src/com/android/tests/sysmem/host/TestException.java b/tests/SystemMemoryTest/host/src/com/android/tests/sysmem/host/TestException.java new file mode 100644 index 000000000000..dc3b68f4b455 --- /dev/null +++ b/tests/SystemMemoryTest/host/src/com/android/tests/sysmem/host/TestException.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.tests.sysmem.host; + +/** + * Exception thrown in case of unexpected error encountered when executing the + * test. + */ +class TestException extends Exception { + TestException(Exception cause) { + super(cause); + } + + TestException(String msg) { + super(msg); + } + + TestException(String msg, Exception cause) { + super(msg, cause); + } +} |