blob: b405ece9b5c58ace2c5cac2846935501b6edd489 [file] [log] [blame]
Kuan-Tung Panfbd59a02017-08-02 19:50:04 +00001/*
2 * Copyright (C) 2015 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
17package com.android.media.tests;
18
Kuan-Tung Panfbd59a02017-08-02 19:50:04 +000019import com.android.tradefed.config.OptionClass;
20import com.android.tradefed.device.DeviceNotAvailableException;
21import com.android.tradefed.log.LogUtil.CLog;
22import com.android.tradefed.result.ITestInvocationListener;
jdesprez607ef1a2018-02-07 16:34:53 -080023import com.android.tradefed.result.TestDescription;
Kuan-Tung Panfbd59a02017-08-02 19:50:04 +000024
25import java.util.HashMap;
26import java.util.Map;
27import java.util.regex.Matcher;
28import java.util.regex.Pattern;
29
30/**
31 * Camera app latency test
32 *
33 * Runs Camera app latency test to measure Camera capture session time and reports the metrics.
34 */
35@OptionClass(alias = "camera-latency")
36public class Camera2LatencyTest extends CameraTestBase {
37
38 public Camera2LatencyTest() {
39 setTestPackage("com.google.android.camera");
40 setTestClass("com.android.camera.latency.CameraCaptureSessionTest");
41 setTestRunner("android.test.InstrumentationTestRunner");
42 setRuKey("CameraAppLatency");
43 setTestTimeoutMs(60 * 60 * 1000); // 1 hour
44 }
45
46 /**
47 * {@inheritDoc}
48 */
49 @Override
50 public void run(ITestInvocationListener listener) throws DeviceNotAvailableException {
51 runInstrumentationTest(listener, new CollectingListener(listener));
52 }
53
54 /**
55 * A listener to collect the results from each test run, then forward them to other listeners.
56 */
57 private class CollectingListener extends DefaultCollectingListener {
58
59 public CollectingListener(ITestInvocationListener listener) {
60 super(listener);
61 }
62
63 @Override
jdesprez607ef1a2018-02-07 16:34:53 -080064 public void handleMetricsOnTestEnded(TestDescription test, Map<String, String> testMetrics) {
Kuan-Tung Panfbd59a02017-08-02 19:50:04 +000065 // Test metrics accumulated will be posted at the end of test run.
66 getAggregatedMetrics().putAll(parseResults(test.getTestName(), testMetrics));
67 }
68
69 private Map<String, String> parseResults(String testName, Map<String, String> testMetrics) {
70 final Pattern STATS_REGEX = Pattern.compile(
71 "^(?<latency>[0-9.]+)\\|(?<values>[0-9 .-]+)");
72
73 // Parse activity time stats from the instrumentation result.
74 // Format : <metric_key>=<average_of_latency>|<raw_data>
75 // Example:
76 // FirstCaptureResultTimeMs=38|13 48 ... 35
77 // SecondCaptureResultTimeMs=29.2|65 24 ... 0
78 // CreateTimeMs=373.6|382 364 ... 323
79 //
80 // Then report only the first two startup time of cold startup and average warm startup.
81 Map<String, String> parsed = new HashMap<String, String>();
82 for (Map.Entry<String, String> metric : testMetrics.entrySet()) {
83 Matcher matcher = STATS_REGEX.matcher(metric.getValue());
84 if (matcher.matches()) {
85 String keyName = String.format("%s_%s", testName, metric.getKey());
86 parsed.put(keyName, matcher.group("latency"));
87 } else {
88 CLog.w(String.format("Stats not in correct format: %s", metric.getValue()));
89 }
90 }
91 return parsed;
92 }
93 }
94}