• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016 Google Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you
5  * may not use this file except in compliance with the License. You may
6  * 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
13  * implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 
17 package com.android.vts.util;
18 
19 import com.android.vts.entity.ProfilingPointRunEntity;
20 import com.android.vts.proto.VtsReportMessage.VtsProfilingRegressionMode;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Map;
26 
27 /** Represents statistical summaries of each profiling point. */
28 public class ProfilingPointSummary implements Iterable<StatSummary> {
29     private List<StatSummary> statSummaries;
30     private Map<String, Integer> labelIndices;
31     private List<String> labels;
32     private VtsProfilingRegressionMode regressionMode;
33     public String xLabel;
34     public String yLabel;
35 
36     /** Initializes the summary with empty arrays. */
ProfilingPointSummary()37     public ProfilingPointSummary() {
38         statSummaries = new ArrayList<>();
39         labelIndices = new HashMap<>();
40         labels = new ArrayList<>();
41     }
42 
43     /**
44      * Determines if a data label is present in the profiling point.
45      *
46      * @param label The name of the label.
47      * @return true if the label is present, false otherwise.
48      */
hasLabel(String label)49     public boolean hasLabel(String label) {
50         return labelIndices.containsKey(label);
51     }
52 
53     /**
54      * Gets the statistical summary for a specified data label.
55      *
56      * @param label The name of the label.
57      * @return The StatSummary object if it exists, or null otherwise.
58      */
getStatSummary(String label)59     public StatSummary getStatSummary(String label) {
60         if (!hasLabel(label))
61             return null;
62         return statSummaries.get(labelIndices.get(label));
63     }
64 
65     /**
66      * Gets the last-seen regression mode.
67      *
68      * @return The VtsProfilingRegressionMode value.
69      */
getRegressionMode()70     public VtsProfilingRegressionMode getRegressionMode() {
71         return regressionMode;
72     }
73 
74     /**
75      * Updates the profiling summary with the data from a new profiling report.
76      *
77      * @param profilingRun The profiling point run entity object containing profiling data.
78      */
update(ProfilingPointRunEntity profilingRun)79     public void update(ProfilingPointRunEntity profilingRun) {
80         for (int i = 0; i < profilingRun.labels.size(); i++) {
81             String label = profilingRun.labels.get(i);
82             if (!labelIndices.containsKey(label)) {
83                 StatSummary summary = new StatSummary(label, profilingRun.regressionMode);
84                 labelIndices.put(label, statSummaries.size());
85                 statSummaries.add(summary);
86             }
87             StatSummary summary = getStatSummary(label);
88             summary.updateStats(profilingRun.values.get(i));
89         }
90         this.regressionMode = profilingRun.regressionMode;
91         this.labels = profilingRun.labels;
92         this.xLabel = profilingRun.xLabel;
93         this.yLabel = profilingRun.yLabel;
94     }
95 
96     /**
97      * Updates the profiling summary at a label with the data from a new profiling report.
98      *
99      * <p>Updates the summary specified by the label with all values provided in the report. If
100      * labels
101      * are provided in the report, they will be ignored -- all values are updated only to the
102      * provided
103      * label.
104      *
105      * @param profilingEntity The ProfilingPointRunEntity object containing profiling data.
106      * @param label The ByteString label for which all values in the report will be updated.
107      */
updateLabel(ProfilingPointRunEntity profilingEntity, String label)108     public void updateLabel(ProfilingPointRunEntity profilingEntity, String label) {
109         if (!labelIndices.containsKey(label)) {
110             StatSummary summary = new StatSummary(label, profilingEntity.regressionMode);
111             labelIndices.put(label, labels.size());
112             labels.add(label);
113             statSummaries.add(summary);
114         }
115         StatSummary summary = getStatSummary(label);
116         for (long value : profilingEntity.values) {
117             summary.updateStats(value);
118         }
119         this.regressionMode = profilingEntity.regressionMode;
120         this.xLabel = profilingEntity.xLabel;
121         this.yLabel = profilingEntity.yLabel;
122     }
123 
124     /**
125      * Gets an iterator that returns stat summaries in the ordered the labels were specified in the
126      * ProfilingReportMessage objects.
127      */
128     @Override
iterator()129     public Iterator<StatSummary> iterator() {
130         Iterator<StatSummary> it = new Iterator<StatSummary>() {
131             private int currentIndex = 0;
132 
133             @Override
134             public boolean hasNext() {
135                 return labels != null && currentIndex < labels.size();
136             }
137 
138             @Override
139             public StatSummary next() {
140                 String label = labels.get(currentIndex++);
141                 return statSummaries.get(labelIndices.get(label));
142             }
143         };
144         return it;
145     }
146 }
147