1 /* 2 * Copyright (C) 2014 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 android.sample.cts; 18 19 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper; 20 import com.android.compatibility.common.util.MeasureRun; 21 import com.android.compatibility.common.util.MeasureTime; 22 import com.android.compatibility.common.util.MetricsReportLog; 23 import com.android.compatibility.common.util.ResultType; 24 import com.android.compatibility.common.util.ResultUnit; 25 import com.android.compatibility.common.util.Stat; 26 import com.android.tradefed.build.IBuildInfo; 27 import com.android.tradefed.device.ITestDevice; 28 import com.android.tradefed.testtype.DeviceTestCase; 29 import com.android.tradefed.testtype.IAbi; 30 import com.android.tradefed.testtype.IAbiReceiver; 31 import com.android.tradefed.testtype.IBuildReceiver; 32 import com.android.tradefed.util.CommandResult; 33 import com.android.tradefed.util.CommandStatus; 34 import com.android.tradefed.util.FileUtil; 35 import com.android.tradefed.util.RunUtil; 36 37 import java.io.File; 38 39 /** 40 * Test to measure the transfer time of a file from the host to the device. 41 */ 42 public class SampleHostResultTest extends DeviceTestCase implements IAbiReceiver, IBuildReceiver { 43 44 private static final String TAG = SampleHostResultTest.class.getSimpleName(); 45 46 /** 47 * Name of the report log to store test metrics. 48 */ 49 private static final String REPORT_LOG_NAME = "SampleHostTestMetrics"; 50 51 /** 52 * The number of times to repeat the test. 53 */ 54 private static final int REPEAT = 5; 55 56 /** 57 * The device-side location to write the file to. 58 */ 59 private static final String FILE_PATH = "/data/local/tmp/%s"; 60 61 /** 62 * The name of the file to transfer. 63 * 64 * In this case we will transfer this test's module config. 65 */ 66 private static final String FILE_NAME = "CtsSampleHostTestCases.config"; 67 68 /** 69 * A helper to access resources in the build. 70 */ 71 private CompatibilityBuildHelper mBuildHelper; 72 73 /** 74 * A reference to the device under test. 75 */ 76 private ITestDevice mDevice; 77 78 /** 79 * A reference to the ABI under test. 80 */ 81 private IAbi mAbi; 82 83 @Override 84 public void setAbi(IAbi abi) { 85 mAbi = abi; 86 } 87 88 @Override 89 public void setBuild(IBuildInfo buildInfo) { 90 // Get the build, this is used to access the APK. 91 mBuildHelper = new CompatibilityBuildHelper(buildInfo); 92 } 93 94 @Override 95 protected void setUp() throws Exception { 96 super.setUp(); 97 // Get the device, this gives a handle to run commands and install APKs. 98 mDevice = getDevice(); 99 } 100 101 /** 102 * Measures the time taken to transfer a file to the device and then back. 103 * 104 * The workload is repeated several times and the report is populated with the result. 105 * 106 * @throws Exception 107 */ 108 public void testTransferTime() throws Exception { 109 final ITestDevice device = mDevice; 110 // Create the device side path where the file will be transfered. 111 final String devicePath = String.format(FILE_PATH, "tmp_testPushPull.txt"); 112 // Get this test's module config file from the build. 113 final File testFile = mBuildHelper.getTestFile(FILE_NAME); 114 double[] result = MeasureTime.measure(REPEAT, new MeasureRun() { 115 @Override 116 public void prepare(int i) throws Exception { 117 device.executeShellCommand(String.format("rm %s", devicePath)); 118 } 119 @Override 120 public void run(int i) throws Exception { 121 // Create a temporary file to compare with. 122 File tmpFile = FileUtil.createTempFile("tmp", "txt"); 123 try { 124 // Push the file across and ensure it exists. 125 assertTrue("Could not push file", device.pushFile(testFile, devicePath)); 126 assertTrue("Unsuccessful transfer", device.doesFileExist(devicePath)); 127 // Pull the file back and ensure it is the same. 128 assertTrue("Could not pull file", device.pullFile(devicePath, tmpFile)); 129 assertFilesAreEqual(testFile, tmpFile); 130 } finally { 131 // Clean up. 132 tmpFile.delete(); 133 device.executeShellCommand(String.format("rm %s", devicePath)); 134 } 135 } 136 }); 137 // Compute the stats. 138 Stat.StatResult stat = Stat.getStat(result); 139 // Get the report for this test and add the results to record. 140 String streamName = "test_transfer_time_metrics"; 141 MetricsReportLog report = new MetricsReportLog( 142 mBuildHelper.getBuildInfo(), mAbi.getName(), 143 String.format("%s#testTransferTime", getClass().getCanonicalName()), 144 REPORT_LOG_NAME, streamName); 145 report.addValues("times", result, ResultType.LOWER_BETTER, ResultUnit.MS); 146 report.addValue("min", stat.mMin, ResultType.LOWER_BETTER, ResultUnit.MS); 147 report.addValue("max", stat.mMax, ResultType.LOWER_BETTER, ResultUnit.MS); 148 // Set a summary. 149 report.setSummary("average", stat.mAverage, ResultType.LOWER_BETTER, ResultUnit.MS); 150 // Send the report to Tradefed. 151 report.submit(); 152 } 153 154 /** 155 * Asserts the two given files are equal using the diff utility. 156 * 157 * @throws Exception 158 */ 159 private static void assertFilesAreEqual(File first, File second) throws Exception { 160 CommandResult result = RunUtil.getDefault().runTimedCmd(5000, "diff", 161 first.getAbsolutePath(), second.getAbsolutePath()); 162 assertTrue("Diff failed to run", result.getStatus() == CommandStatus.SUCCESS); 163 assertTrue("Files are not equivalent", "".equals(result.getStdout())); 164 } 165 166 } 167