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 package com.android.tradefed.device.metric;
17 
18 import com.android.tradefed.build.IBuildInfo;
19 import com.android.tradefed.device.ITestDevice;
20 import com.android.tradefed.invoker.IInvocationContext;
21 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric;
22 import com.android.tradefed.result.ITestInvocationListener;
23 import com.android.tradefed.result.TestDescription;
24 
25 import java.util.List;
26 import java.util.Map;
27 
28 /**
29  * This interface will be added as a decorator when reporting tests results in order to collect
30  * matching metrics.
31  *
32  * <p>This interface cannot be used as a <result_reporter> even it extends {@link
33  * ITestInvocationListener}. The configuration checking will reject it. It must be used as a
34  * "metrics_collector".
35  *
36  * <p>Collectors are not expected to keep an internal state as they may be re-used in several
37  * places. If an internal state really must be used, then it should be cleaned on {@link
38  * #init(IInvocationContext, ITestInvocationListener)}.
39  */
40 public interface IMetricCollector extends ITestInvocationListener {
41 
42     /**
43      * Initialization of the collector with the current context and where to forward results. Will
44      * only be called once per instance, and the collector is expected to update its internal
45      * context and listener. Init will never be called during a test run always before.
46      *
47      * <p>Do not override unless you know what you are doing.
48      *
49      * @param context the {@link IInvocationContext} for the invocation in progress.
50      * @param listener the {@link ITestInvocationListener} where to put results.
51      * @return the new listener wrapping the original one.
52      */
init( IInvocationContext context, ITestInvocationListener listener)53     public ITestInvocationListener init(
54             IInvocationContext context, ITestInvocationListener listener);
55 
56     /** Returns the list of devices available in the invocation. */
getDevices()57     public List<ITestDevice> getDevices();
58 
59     /** Returns the list of build information available in the invocation. */
getBuildInfos()60     public List<IBuildInfo> getBuildInfos();
61 
62     /** Returns the original {@link ITestInvocationListener} where we are forwarding the results. */
getInvocationListener()63     public ITestInvocationListener getInvocationListener();
64 
65     /**
66      * Callback when a test run is started.
67      *
68      * @param runData the {@link DeviceMetricData} holding the data for the run.
69      */
onTestRunStart(DeviceMetricData runData)70     public void onTestRunStart(DeviceMetricData runData);
71 
72     /**
73      * Callback when a test run is ended. This should be the time for clean up.
74      *
75      * @param runData the {@link DeviceMetricData} holding the data for the run. Will be the same
76      *     object as during {@link #onTestRunStart(DeviceMetricData)}.
77      * @param currentRunMetrics the current map of metrics passed to {@link #testRunEnded(long,
78      *     Map)}.
79      */
onTestRunEnd(DeviceMetricData runData, final Map<String, Metric> currentRunMetrics)80     public void onTestRunEnd(DeviceMetricData runData, final Map<String, Metric> currentRunMetrics);
81 
82     /**
83      * Callback when a test case is started.
84      *
85      * @param testData the {@link DeviceMetricData} holding the data for the test case.
86      */
onTestStart(DeviceMetricData testData)87     public void onTestStart(DeviceMetricData testData);
88 
89     /**
90      * Callback when a test case is ended. This should be the time for clean up.
91      *
92      * @param testData the {@link DeviceMetricData} holding the data for the test case. Will be the
93      *     same object as during {@link #onTestStart(DeviceMetricData)}.
94      * @param currentTestCaseMetrics the current map of metrics passed to {@link
95      *     #testEnded(TestDescription, Map)}.
96      */
onTestEnd( DeviceMetricData testData, final Map<String, Metric> currentTestCaseMetrics)97     public void onTestEnd(
98             DeviceMetricData testData, final Map<String, Metric> currentTestCaseMetrics);
99 }
100