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.tradefed.profiler; 18 19 import com.android.ddmlib.testrunner.TestIdentifier; 20 import com.android.tradefed.result.ByteArrayInputStreamSource; 21 import com.android.tradefed.result.InputStreamSource; 22 import java.util.Map; 23 24 /** 25 * A class which produces formatted metrics. It builds up formatted metrics using a 26 * {@link StringBuilder} as a buffer. 27 */ 28 public class MetricOutputData { 29 30 StringBuilder mMetrics; 31 32 public MetricOutputData() { 33 mMetrics = new StringBuilder(); 34 } 35 36 /** 37 * Start a new metric in the output buffer. 38 */ 39 public void startNewMetric() { 40 mMetrics.append("\n\n"); 41 } 42 43 /** 44 * Add new metrics to the output buffer. 45 * @param prefix A prefix to append to the test name. 46 * @param test A {@link TestIdentifier} which contains the test name. 47 * @param metrics The metrics to add. 48 */ 49 public void addMetrics(String prefix, TestIdentifier test, Map<String, Double> metrics) { 50 mMetrics.append(String.format("%s:%s:\n", prefix, test.getTestName())); 51 for (Map.Entry<String, Double> entry : metrics.entrySet()) { 52 mMetrics.append(String.format("%s=%.5f\n", entry.getKey(), entry.getValue())); 53 } 54 mMetrics.append("---\n"); 55 } 56 57 /** 58 * Add new metrics to the output buffer. 59 * @param prefix A prefix to append to the test name. 60 * @param test The name of the test. 61 * @param metrics The metrics to add. 62 */ 63 public void addMetrics(String prefix, String test, Map<String, Double> metrics) { 64 mMetrics.append(String.format("%s:%s:\n", prefix, test)); 65 for (Map.Entry<String, Double> entry : metrics.entrySet()) { 66 mMetrics.append(String.format("%s=%.5f\n", entry.getKey(), entry.getValue())); 67 } 68 mMetrics.append("---\n"); 69 } 70 71 /** 72 * Returns an {@link InputStreamSource} representation of the output buffer 73 * @return the {@link InputStreamSource} 74 */ 75 public InputStreamSource getFormattedMetrics() { 76 return new ByteArrayInputStreamSource(mMetrics.toString().getBytes()); 77 } 78 } 79