Home | History | Annotate | Download | only in util
      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.ProfilingPointEntity;
     17 import com.android.vts.entity.ProfilingPointSummaryEntity;
     18 import com.google.appengine.api.datastore.Entity;
     19 import java.util.Arrays;
     20 import java.util.HashMap;
     21 import java.util.Map;
     22 import java.util.concurrent.TimeUnit;
     23 import java.util.logging.Logger;
     24 
     25 /** PerformanceSummary, an object summarizing performance across profiling points for a test run. */
     26 public class PerformanceSummary {
     27     protected static Logger logger = Logger.getLogger(PerformanceSummary.class.getName());
     28     private Map<String, ProfilingPointSummary> summaryMap;
     29 
     30     public final long startTime;
     31     public final long endTime;
     32     public final String label;
     33 
     34     /** Creates a performance summary object. */
     35     public PerformanceSummary(long startTime, long endTime, String label) {
     36         this.summaryMap = new HashMap<>();
     37         this.startTime = startTime;
     38         this.endTime = endTime;
     39         this.label = label;
     40     }
     41 
     42     /** Creates a performance summary object. */
     43     public PerformanceSummary(long startTime, long endTime) {
     44         this(
     45                 startTime,
     46                 endTime,
     47                 "<span class='date-label'>"
     48                         + Long.toString(TimeUnit.MICROSECONDS.toMillis(endTime))
     49                         + "</span>");
     50     }
     51 
     52     /**
     53      * Determine if the performance summary contains the provided time.
     54      *
     55      * @param time The time (unix timestamp, microseconds) to check.
     56      * @return True if the time is within the performance summary window, false otherwise.
     57      */
     58     public boolean contains(long time) {
     59         return time >= startTime && time <= endTime;
     60     }
     61 
     62     /**
     63      * Add the profiling data from a ProfilingPointRunEntity to the performance summary.
     64      *
     65      * @param profilingRun The Entity object whose data to add.
     66      */
     67     public void addData(ProfilingPointEntity profilingPoint, Entity profilingRun) {
     68         ProfilingPointSummaryEntity ppSummary =
     69                 ProfilingPointSummaryEntity.fromEntity(profilingRun);
     70         if (ppSummary == null) return;
     71 
     72         String name = profilingPoint.profilingPointName;
     73         if (ppSummary.labels != null && ppSummary.labels.size() > 0) {
     74             if (!ppSummary.series.equals("")) {
     75                 name += " (" + ppSummary.series + ")";
     76             }
     77             if (!summaryMap.containsKey(name)) {
     78                 summaryMap.put(
     79                         name,
     80                         new ProfilingPointSummary(
     81                                 profilingPoint.xLabel,
     82                                 profilingPoint.yLabel,
     83                                 profilingPoint.regressionMode));
     84             }
     85             summaryMap.get(name).update(ppSummary);
     86         } else {
     87             // Use the option suffix as the table name.
     88             // Group all profiling points together into one table
     89             if (!summaryMap.containsKey(ppSummary.series)) {
     90                 summaryMap.put(
     91                         ppSummary.series,
     92                         new ProfilingPointSummary(
     93                                 profilingPoint.xLabel,
     94                                 profilingPoint.yLabel,
     95                                 profilingPoint.regressionMode));
     96             }
     97             summaryMap.get(ppSummary.series).updateLabel(ppSummary, name);
     98         }
     99     }
    100 
    101     /**
    102      * Adds a ProfilingPointSummary object into the summary map only if the key doesn't exist.
    103      *
    104      * @param key The name of the profiling point.
    105      * @param summary The ProfilingPointSummary object to add into the summary map.
    106      * @return True if the data was inserted into the performance summary, false otherwise.
    107      */
    108     public boolean insertProfilingPointSummary(String key, ProfilingPointSummary summary) {
    109         if (!summaryMap.containsKey(key)) {
    110             summaryMap.put(key, summary);
    111             return true;
    112         }
    113         return false;
    114     }
    115 
    116     /**
    117      * Gets the number of profiling points.
    118      *
    119      * @return The number of profiling points in the performance summary.
    120      */
    121     public int size() {
    122         return summaryMap.size();
    123     }
    124 
    125     /**
    126      * Gets the names of the profiling points.
    127      *
    128      * @return A string array of profiling point names.
    129      */
    130     public String[] getProfilingPointNames() {
    131         String[] profilingNames = summaryMap.keySet().toArray(new String[summaryMap.size()]);
    132         Arrays.sort(profilingNames);
    133         return profilingNames;
    134     }
    135 
    136     /**
    137      * Determines if a profiling point is described by the performance summary.
    138      *
    139      * @param profilingPointName The name of the profiling point.
    140      * @return True if the profiling point is contained in the performance summary, else false.
    141      */
    142     public boolean hasProfilingPoint(String profilingPointName) {
    143         return summaryMap.containsKey(profilingPointName);
    144     }
    145 
    146     /**
    147      * Gets the profiling point summary by name.
    148      *
    149      * @param profilingPointName The name of the profiling point to fetch.
    150      * @return The ProfilingPointSummary object describing the specified profiling point.
    151      */
    152     public ProfilingPointSummary getProfilingPointSummary(String profilingPointName) {
    153         return summaryMap.get(profilingPointName);
    154     }
    155 }
    156