summaryrefslogtreecommitdiff
path: root/libraries/collectors-helper/utilities/src/com/android/helpers/MetricUtility.java
blob: 5e7e8e5a07e2f255fe8e747d871108ccde41c2e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.android.helpers;

import android.app.Instrumentation;
import android.os.ParcelFileDescriptor;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.Map;

/**
 * MetricUtility consist of basic utility methods to construct the metrics
 * reported at the end of the test.
 */
public class MetricUtility {

    private static final String TAG = MetricUtility.class.getSimpleName();
    private static final String KEY_JOIN = "_";
    public static final String METRIC_SEPARATOR = ",";

    public static final int BUFFER_SIZE = 1024;
    private static final DecimalFormat DOUBLE_FORMAT = new DecimalFormat("#0.000001");

    /**
     * Append the given array of string to construct the final key used to track the metrics.
     *
     * @param keys to append using KEY_JOIN
     */
    public static String constructKey(String... keys) {
        return String.join(KEY_JOIN, keys);
    }

    /**
     * Add metric to the result map. If metric key already exist append the new metric.
     *
     * @param metricKey Unique key to track the metric.
     * @param metric metric to track.
     * @param resultMap map of all the metrics.
     */
    public static void addMetric(String metricKey, long metric, Map<String,
            StringBuilder> resultMap) {
        resultMap.compute(metricKey, (key, value) -> (value == null) ?
                new StringBuilder().append(metric) : value.append(METRIC_SEPARATOR).append(metric));
    }

    /**
     * Add metric to the result map. If metric key already exist append the new metric.
     *
     * @param metricKey Unique key to track the metric.
     * @param metric metric to track.
     * @param resultMap map of all the metrics.
     */
    public static void addMetric(
            String metricKey, double metric, Map<String, StringBuilder> resultMap) {
        resultMap.compute(
                metricKey,
                (key, value) ->
                        (value == null ? new StringBuilder() : value.append(METRIC_SEPARATOR))
                                .append(DOUBLE_FORMAT.format(metric)));
    }

    /**
     * Add metric to the result map. If metric key already exist increment the value by 1.
     *
     * @param metricKey Unique key to track the metric.
     * @param resultMap map of all the metrics.
     */
    public static void addMetric(String metricKey, Map<String,
            Integer> resultMap) {
        resultMap.compute(metricKey, (key, value) -> (value == null) ? 1 : value + 1);
    }

    /**
     * Turn executeShellCommand into a blocking operation.
     *
     * @param command shell command to be executed.
     * @param instr used to run the shell command.
     * @return byte array of execution result
     */
    public static byte[] executeCommandBlocking(String command, Instrumentation instr) {
        try (InputStream is = new ParcelFileDescriptor.AutoCloseInputStream(instr.getUiAutomation()
                .executeShellCommand(command));
                ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            byte[] buf = new byte[BUFFER_SIZE];
            int length;
            Log.i(TAG, "Start reading the data");
            while ((length = is.read(buf)) >= 0) {
                out.write(buf, 0, length);
            }
            Log.i(TAG, "Stop reading the data");
            return out.toByteArray();
        } catch (IOException e) {
            Log.e(TAG, "Error executing: " + command, e);
            return null;
        }
    }

}