1 /*
2  * Copyright (C) 2012 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.media.tests;
18 
19 import com.android.ddmlib.IDevice;
20 import com.android.tradefed.device.DeviceNotAvailableException;
21 import com.android.tradefed.device.ITestDevice;
22 import com.android.tradefed.log.LogUtil.CLog;
23 import com.android.tradefed.result.ITestInvocationListener;
24 import com.android.tradefed.result.InputStreamSource;
25 import com.android.tradefed.result.LogDataType;
26 import com.android.tradefed.testtype.IDeviceTest;
27 import com.android.tradefed.testtype.IRemoteTest;
28 import com.android.tradefed.util.proto.TfMetricProtoUtil;
29 
30 import org.junit.Assert;
31 
32 import java.util.HashMap;
33 import java.util.Map;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
36 
37 /**
38  * Standalone panoramic photo processing benchmark test.
39  */
40 public class PanoramaBenchMarkTest implements IDeviceTest, IRemoteTest {
41 
42     private ITestDevice mTestDevice = null;
43 
44     private static final Pattern ELAPSED_TIME_PATTERN =
45             Pattern.compile("(Total elapsed time:)\\s+(\\d+\\.\\d*)\\s+(seconds)");
46 
47     private static final String PANORAMA_TEST_KEY = "PanoramaElapsedTime";
48     private static final String TEST_TAG = "CameraLatency";
49 
50     /**
51      * {@inheritDoc}
52      */
53     @Override
run(ITestInvocationListener listener)54     public void run(ITestInvocationListener listener) throws DeviceNotAvailableException {
55         Assert.assertNotNull(mTestDevice);
56 
57         String dataStore = mTestDevice.getMountPoint(IDevice.MNT_DATA);
58         String externalStore = mTestDevice.getMountPoint(IDevice.MNT_EXTERNAL_STORAGE);
59 
60         mTestDevice.executeShellCommand(String.format("chmod 777 %s/local/tmp/panorama_bench",
61                 dataStore));
62 
63         String shellOutput = mTestDevice.executeShellCommand(
64                 String.format("%s/local/tmp/panorama_bench %s/panorama_input/test %s/panorama.ppm",
65                 dataStore, externalStore, externalStore));
66 
67         String[] lines = shellOutput.split("\n");
68 
69         Map<String, String> metrics = new HashMap<String, String>();
70         for (String line : lines) {
71             Matcher m = ELAPSED_TIME_PATTERN.matcher(line.trim());
72             if (m.matches()) {
73                 CLog.d(String.format("Found elapsed time \"%s seconds\" from line %s",
74                         m.group(2), line));
75                 metrics.put(PANORAMA_TEST_KEY, m.group(2));
76                 break;
77             } else {
78                 CLog.d(String.format("Unabled to find elapsed time from line: %s", line));
79             }
80         }
81 
82         reportMetrics(listener, TEST_TAG, metrics);
83         cleanupDevice();
84     }
85 
86     /**
87      * {@inheritDoc}
88      */
89     @Override
setDevice(ITestDevice device)90     public void setDevice(ITestDevice device) {
91         mTestDevice = device;
92     }
93 
94     /**
95      * {@inheritDoc}
96      */
97     @Override
getDevice()98     public ITestDevice getDevice() {
99         return mTestDevice;
100     }
101 
102     /**
103      * Removes image files used to mock panorama stitching.
104      *
105      * @throws DeviceNotAvailableException If the device is unavailable or
106      *         something happened while deleting files
107      */
cleanupDevice()108     private void cleanupDevice() throws DeviceNotAvailableException {
109         String externalStore = mTestDevice.getMountPoint(IDevice.MNT_EXTERNAL_STORAGE);
110         mTestDevice.executeShellCommand(String.format("rm -r %s/panorama_input", externalStore));
111     }
112 
113     /**
114      * Report run metrics by creating an empty test run to stick them in.
115      *
116      * @param listener The {@link ITestInvocationListener} of test results
117      * @param runName The test name
118      * @param metrics The {@link Map} that contains metrics for the given test
119      */
reportMetrics(ITestInvocationListener listener, String runName, Map<String, String> metrics)120     private void reportMetrics(ITestInvocationListener listener, String runName,
121             Map<String, String> metrics) {
122         InputStreamSource bugreport = mTestDevice.getBugreport();
123         listener.testLog("bugreport", LogDataType.BUGREPORT, bugreport);
124         bugreport.close();
125 
126         CLog.d(String.format("About to report metrics: %s", metrics));
127         listener.testRunStarted(runName, 0);
128         listener.testRunEnded(0, TfMetricProtoUtil.upgradeConvert(metrics));
129     }
130 }
131