1 /*
2  * Copyright (C) 2021 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.performance.tests;
18 
19 import com.android.tradefed.log.LogUtil;
20 import com.android.tradefed.result.ITestInvocationListener;
21 import com.android.tradefed.util.proto.TfMetricProtoUtil;
22 
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.concurrent.Callable;
29 
30 /** A simple data structure that used to capture performance metrics and report median values */
31 public class DataRecorder {
32 
33     private final String mRunName;
34     private Map<String, List<Float>> mFloatMetrics = new HashMap<>();
35     private Map<String, List<Long>> mLongMetrics = new HashMap<>();
36 
DataRecorder(String runName)37     public DataRecorder(String runName) {
38         mRunName = runName;
39     }
40 
recordMetric(String name, Long value)41     public void recordMetric(String name, Long value) {
42         List<Long> list = mLongMetrics.computeIfAbsent(name, k -> new ArrayList<>());
43         list.add(value);
44     }
45 
recordMetric(String name, Float value)46     public void recordMetric(String name, Float value) {
47         List<Float> list = mFloatMetrics.computeIfAbsent(name, k -> new ArrayList<>());
48         list.add(value);
49     }
50 
captureTime(String key, Callable<Void> action)51     public void captureTime(String key, Callable<Void> action) throws Exception {
52         long startTime = System.currentTimeMillis();
53         action.call();
54         recordMetric(key, System.currentTimeMillis() - startTime);
55     }
56 
reportMetrics(ITestInvocationListener listener, Map<String, String> metrics)57     public void reportMetrics(ITestInvocationListener listener, Map<String, String> metrics) {
58         for (Map.Entry<String, List<Long>> entry : mLongMetrics.entrySet()) {
59             metrics.put(entry.getKey(), getLongMedian(entry.getValue()).toString());
60         }
61         for (Map.Entry<String, List<Float>> entry : mFloatMetrics.entrySet()) {
62             metrics.put(entry.getKey(), getFloatMedian(entry.getValue()).toString());
63         }
64 
65         LogUtil.CLog.i("About to report metrics: %s", metrics);
66         listener.testRunStarted(mRunName, 0);
67         listener.testRunEnded(0, TfMetricProtoUtil.upgradeConvert(metrics));
68     }
69 
getLongMedian(List<Long> items)70     private static Long getLongMedian(List<Long> items) {
71         Collections.sort(items);
72         int medianEntry = items.size() / 2;
73         return items.get(medianEntry);
74     }
75 
getFloatMedian(List<Float> items)76     private static Float getFloatMedian(List<Float> items) {
77         Collections.sort(items);
78         int medianEntry = items.size() / 2;
79         return items.get(medianEntry);
80     }
81 
82     @Override
toString()83     public String toString() {
84         return mLongMetrics.toString() + "," + mFloatMetrics.toString();
85     }
86 }
87