• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2016 Google Inc. All Rights Reserved.
3  *
4  * <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  * <p>http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * <p>Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11  * express or implied. See the License for the specific language governing permissions and
12  * limitations under the License.
13  */
14 package com.android.vts.util;
15 
16 import com.android.vts.entity.ProfilingPointRunEntity;
17 import com.android.vts.proto.VtsReportMessage.VtsProfilingRegressionMode;
18 import com.google.appengine.api.datastore.Entity;
19 import java.util.Arrays;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.Map;
23 import java.util.Set;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 
27 /** PerformanceSummary, an object summarizing performance across profiling points for a test run. */
28 public class PerformanceSummary {
29     protected static Logger logger = Logger.getLogger(PerformanceSummary.class.getName());
30     private Map<String, ProfilingPointSummary> summaryMap;
31     private Set<String> optionSplitKeys;
32 
33     /** Creates a performance summary object. */
PerformanceSummary()34     public PerformanceSummary() {
35         this.summaryMap = new HashMap<>();
36         this.optionSplitKeys = new HashSet<>();
37     }
38 
39     /**
40      * Creates a performance summary object with the specified device name filter. If the specified
41      * name is null, then use no filter.
42      *
43      * @param optionSplitKeys A set of option keys to split on (i.e. profiling data with different
44      *     values corresponding to the option key will be analyzed as different profiling points).
45      */
PerformanceSummary(Set<String> optionSplitKeys)46     public PerformanceSummary(Set<String> optionSplitKeys) {
47         this();
48         this.optionSplitKeys = optionSplitKeys;
49     }
50 
51     /**
52      * Add the profiling data from a ProfilingPointRunEntity to the performance summary.
53      *
54      * @param profilingRun The Entity object whose data to add.
55      */
addData(Entity profilingRun)56     public void addData(Entity profilingRun) {
57         ProfilingPointRunEntity pt = ProfilingPointRunEntity.fromEntity(profilingRun);
58         if (pt == null)
59             return;
60         if (pt.regressionMode == VtsProfilingRegressionMode.VTS_REGRESSION_MODE_DISABLED) {
61             return;
62         }
63 
64         String name = pt.name;
65         String optionSuffix = PerformanceUtil.getOptionAlias(profilingRun, optionSplitKeys);
66 
67         if (pt.labels != null) {
68             if (pt.labels.size() != pt.values.size()) {
69                 logger.log(Level.WARNING, "Labels and values are different sizes.");
70                 return;
71             }
72             if (!optionSuffix.equals("")) {
73                 name += " (" + optionSuffix + ")";
74             }
75             if (!summaryMap.containsKey(name)) {
76                 summaryMap.put(name, new ProfilingPointSummary());
77             }
78             summaryMap.get(name).update(pt);
79         } else {
80             // Use the option suffix as the table name.
81             // Group all profiling points together into one table
82             if (!summaryMap.containsKey(optionSuffix)) {
83                 summaryMap.put(optionSuffix, new ProfilingPointSummary());
84             }
85             summaryMap.get(optionSuffix).updateLabel(pt, pt.name);
86         }
87     }
88 
89     /**
90      * Adds a ProfilingPointSummary object into the summary map only if the key doesn't exist.
91      *
92      * @param key The name of the profiling point.
93      * @param summary The ProfilingPointSummary object to add into the summary map.
94      * @return True if the data was inserted into the performance summary, false otherwise.
95      */
insertProfilingPointSummary(String key, ProfilingPointSummary summary)96     public boolean insertProfilingPointSummary(String key, ProfilingPointSummary summary) {
97         if (!summaryMap.containsKey(key)) {
98             summaryMap.put(key, summary);
99             return true;
100         }
101         return false;
102     }
103 
104     /**
105      * Gets the number of profiling points.
106      *
107      * @return The number of profiling points in the performance summary.
108      */
size()109     public int size() {
110         return summaryMap.size();
111     }
112 
113     /**
114      * Gets the names of the profiling points.
115      *
116      * @return A string array of profiling point names.
117      */
getProfilingPointNames()118     public String[] getProfilingPointNames() {
119         String[] profilingNames = summaryMap.keySet().toArray(new String[summaryMap.size()]);
120         Arrays.sort(profilingNames);
121         return profilingNames;
122     }
123 
124     /**
125      * Determines if a profiling point is described by the performance summary.
126      *
127      * @param profilingPointName The name of the profiling point.
128      * @return True if the profiling point is contained in the performance summary, else false.
129      */
hasProfilingPoint(String profilingPointName)130     public boolean hasProfilingPoint(String profilingPointName) {
131         return summaryMap.containsKey(profilingPointName);
132     }
133 
134     /**
135      * Gets the profiling point summary by name.
136      *
137      * @param profilingPointName The name of the profiling point to fetch.
138      * @return The ProfilingPointSummary object describing the specified profiling point.
139      */
getProfilingPointSummary(String profilingPointName)140     public ProfilingPointSummary getProfilingPointSummary(String profilingPointName) {
141         return summaryMap.get(profilingPointName);
142     }
143 }
144