summaryrefslogtreecommitdiff
path: root/src/main/java/com/android/vts/util/Histogram.java
blob: 2f62a572b640dc792b63eb50837cf2f457de928d (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Copyright (c) 2016 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package com.android.vts.util;

import com.android.vts.entity.ProfilingPointRunEntity;
import com.google.common.primitives.Doubles;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.math3.stat.descriptive.rank.Percentile;

/** Helper object for describing graph data. */
public class Histogram extends Graph {
    public static final String PERCENTILES_KEY = "percentiles";
    public static final String PERCENTILE_VALUES_KEY = "percentile_values";
    public static final String MIN_KEY = "min";
    public static final String MAX_KEY = "max";

    private List<Double> values;
    private List<String> ids;
    private String xLabel;
    private String yLabel;
    private String name;
    private GraphType type = GraphType.HISTOGRAM;
    private Double min = null;
    private Double max = null;

    public Histogram(String name) {
        this.name = name;
        this.values = new ArrayList<>();
        this.ids = new ArrayList<>();
    }

    /**
     * Get the x axis label.
     *
     * @return The x axis label.
     */
    @Override
    public String getXLabel() {
        return xLabel;
    }

    /**
     * Get the graph type.
     *
     * @return The graph type.
     */
    @Override
    public GraphType getType() {
        return type;
    }

    /**
     * Get the name of the graph.
     *
     * @return The name of the graph.
     */
    @Override
    public String getName() {
        return name;
    }

    /**
     * Get the y axis label.
     *
     * @return The y axis label.
     */
    @Override
    public String getYLabel() {
        return yLabel;
    }

    /**
     * Get the number of data points stored in the graph.
     *
     * @return The number of data points stored in the graph.
     */
    @Override
    public int size() {
        return values.size();
    }

    /**
     * Get the minimum value.
     *
     * @return The minimum value.
     */
    public Double getMin() {
        return min;
    }

    /**
     * Get the maximum value.
     *
     * @return The maximum value.
     */
    public Double getMax() {
        return max;
    }

    /**
     * Add data to the graph.
     *
     * @param id The name of the graph.
     * @param profilingPoint The ProfilingPointRunEntity containing data to add.
     */
    @Override
    public void addData(String id, ProfilingPointRunEntity profilingPoint) {
        if (profilingPoint.getValues().size() == 0)
            return;
        xLabel = profilingPoint.getXLabel();
        yLabel = profilingPoint.getYLabel();
        for (long v : profilingPoint.getValues()) {
            double value = v;
            values.add(value);
            ids.add(id);
            if (max == null || value > max)
                max = value;
            if (min == null || value < min)
                min = value;
        }
    }

    /**
     * Serializes the graph to json format.
     *
     * @return A JsonElement object representing the graph object.
     */
    @Override
    public JsonObject toJson() {
        int[] percentiles = {1, 2, 5, 10, 25, 50, 75, 90, 95, 98, 99};
        double[] percentileValues = new double[percentiles.length];
        double[] valueList = Doubles.toArray(values);
        for (int i = 0; i < percentiles.length; i++) {
            percentileValues[i] =
                    Math.round(new Percentile().evaluate(valueList, percentiles[i]) * 1000d)
                    / 1000d;
        }
        JsonObject json = super.toJson();
        json.add(VALUE_KEY, new Gson().toJsonTree(values).getAsJsonArray());
        json.add(PERCENTILES_KEY, new Gson().toJsonTree(percentiles).getAsJsonArray());
        json.add(PERCENTILE_VALUES_KEY, new Gson().toJsonTree(percentileValues).getAsJsonArray());
        json.add(IDS_KEY, new Gson().toJsonTree(ids).getAsJsonArray());
        json.add(MIN_KEY, new JsonPrimitive(min));
        json.add(MAX_KEY, new JsonPrimitive(max));
        return json;
    }
}