1 /*
2  * Copyright (C) 2017 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.tradefed.config.OptionClass;
20 import com.android.tradefed.device.DeviceNotAvailableException;
21 import com.android.tradefed.invoker.TestInformation;
22 import com.android.tradefed.log.LogUtil.CLog;
23 import com.android.tradefed.result.ITestInvocationListener;
24 import com.android.tradefed.result.TestDescription;
25 
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
30 
31 /**
32  * Camera burst startup test
33  *
34  * <p>Runs Camera device performance test to measure time for taking a burst shot.
35  */
36 @OptionClass(alias = "camera-burst-shot")
37 public class CameraBurstStartupTest extends CameraTestBase {
38 
39     private static final Pattern STATS_REGEX = Pattern.compile("^(?<average>[0-9.]+)");
40 
CameraBurstStartupTest()41     public CameraBurstStartupTest() {
42         setTestPackage("com.google.android.camera");
43         setTestClass("com.android.camera.latency.BurstStartupTest");
44         setTestRunner("android.test.InstrumentationTestRunner");
45         setRuKey("CameraBurstStartup");
46         setTestTimeoutMs(60 * 60 * 1000); // 1 hour
47     }
48 
49     /** {@inheritDoc} */
50     @Override
run(TestInformation testInfo, ITestInvocationListener listener)51     public void run(TestInformation testInfo, ITestInvocationListener listener)
52             throws DeviceNotAvailableException {
53         runInstrumentationTest(testInfo, listener, new CollectingListener(listener));
54     }
55 
56     /** A listener to collect the output from test run and fatal errors */
57     private class CollectingListener extends CameraTestMetricsCollectionListener.DefaultCollectingListener {
58 
CollectingListener(ITestInvocationListener listener)59         public CollectingListener(ITestInvocationListener listener) {
60             super(listener);
61         }
62 
63         @Override
handleMetricsOnTestEnded(TestDescription test, Map<String, String> testMetrics)64         public void handleMetricsOnTestEnded(TestDescription test, Map<String, String> testMetrics) {
65             // Test metrics accumulated will be posted at the end of test run.
66             getAggregatedMetrics().putAll(parseResults(test.getTestName(), testMetrics));
67         }
68 
parseResults(String testName, Map<String, String> testMetrics)69         public Map<String, String> parseResults(String testName, Map<String, String> testMetrics) {
70             // Parse burst startup stats from the instrumentation result.
71             Map<String, String> parsed = new HashMap<String, String>();
72             for (Map.Entry<String, String> metric : testMetrics.entrySet()) {
73                 Matcher matcher = STATS_REGEX.matcher(metric.getValue());
74 
75                 if (matcher.matches()) {
76                     // Key name consists of a pair of test name and metric name.
77                     String keyName = String.format("%s_%s", testName, metric.getKey());
78                     parsed.put(keyName, matcher.group("average"));
79                 } else {
80                     CLog.w(String.format("Stats not in correct format: %s", metric.getValue()));
81                 }
82             }
83             return parsed;
84         }
85     }
86 }
87