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.device.ITestDevice; 19 import com.android.tradefed.invoker.IInvocationContext; 20 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric; 21 22 import com.google.common.base.Preconditions; 23 24 import java.io.Serializable; 25 import java.util.HashMap; 26 import java.util.LinkedHashMap; 27 28 /** 29 * Object to hold all the data collected by metric collectors. TODO: Add the data holding and 30 * receiving of data methods. 31 */ 32 public class DeviceMetricData implements Serializable { 33 private static final long serialVersionUID = 1; 34 // When collecting metrics for multiple devices, the configuration name of the device is added 35 // as a namespace to differentiate the metrics. 36 private static final String DEVICE_NAME_FORMAT_KEY = "{%s}:%s"; 37 38 private HashMap<String, Metric> mCurrentMetrics = new LinkedHashMap<>(); 39 40 private final IInvocationContext mContext; 41 42 /** Ctor */ DeviceMetricData(IInvocationContext context)43 public DeviceMetricData(IInvocationContext context) { 44 mContext = context; 45 } 46 47 /** 48 * Add a single metric associated with the primary device. 49 * 50 * @param key The key of the metric. 51 * @param metric The value associated with the metric. 52 */ addMetric(String key, Metric.Builder metric)53 public void addMetric(String key, Metric.Builder metric) { 54 synchronized (mCurrentMetrics) { 55 String actualKey = key; 56 if (mContext.getDevices().size() > 1) { 57 // If there is more than one device, default add is for first device. 58 String deviceName = mContext.getDeviceName(mContext.getDevices().get(0)); 59 actualKey = String.format(DEVICE_NAME_FORMAT_KEY, deviceName, key); 60 } 61 // Last opportunity to automatically set some values. 62 Metric m = metric.build(); 63 mCurrentMetrics.put(actualKey, m); 64 } 65 } 66 67 /** 68 * Add a single metric associated with a specified device. 69 * 70 * @param device the {@link ITestDevice} the metric is associated to. 71 * @param key The key of the metric. 72 * @param metric The value associated with the metric. 73 */ addMetricForDevice(ITestDevice device, String key, Metric.Builder metric)74 public void addMetricForDevice(ITestDevice device, String key, Metric.Builder metric) { 75 synchronized (mCurrentMetrics) { 76 String actualKey = key; 77 if (mContext.getDevices().size() > 1) { 78 // If there is more than one device, default add is for first device. 79 String deviceName = mContext.getDeviceName(device); 80 actualKey = String.format(DEVICE_NAME_FORMAT_KEY, deviceName, key); 81 } 82 // Last opportunity to automatically set some values. 83 Metric m = metric.build(); 84 mCurrentMetrics.put(actualKey, m); 85 } 86 } 87 88 /** 89 * Push all the data received so far to the map of metrics that will be reported. This should 90 * also clean up the resources after pushing them. 91 * 92 * @param metrics The metrics currently available. 93 */ addToMetrics(HashMap<String, Metric> metrics)94 public void addToMetrics(HashMap<String, Metric> metrics) { 95 Preconditions.checkNotNull(metrics); 96 synchronized (mCurrentMetrics) { 97 metrics.putAll(mCurrentMetrics); 98 } 99 } 100 } 101