• 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.proto.VtsReportMessage.VtsProfilingRegressionMode;
20 
21 /** Helper object for storing statistics. */
22 public class StatSummary {
23     private String label;
24     private double min;
25     private double max;
26     private double mean;
27     private double var;
28     private int n;
29     private VtsProfilingRegressionMode regression_mode;
30 
31     /**
32      * Initializes the statistical summary.
33      *
34      * <p>Sets the label as provided. Initializes the mean, variance, and n (number of values seen)
35      * to
36      * 0.
37      *
38      * @param label The (String) label to assign to the summary.
39      * @param mode The VtsProfilingRegressionMode to use when analyzing performance.
40      */
StatSummary(String label, VtsProfilingRegressionMode mode)41     public StatSummary(String label, VtsProfilingRegressionMode mode) {
42         this.label = label;
43         this.min = Double.MAX_VALUE;
44         this.max = Double.MIN_VALUE;
45         this.mean = 0;
46         this.var = 0;
47         this.n = 0;
48         this.regression_mode = mode;
49     }
50 
51     /**
52      * Update the mean and variance using Welford's single-pass method.
53      *
54      * @param value The observed value in the stream.
55      */
updateStats(double value)56     public void updateStats(double value) {
57         n += 1;
58         double oldMean = mean;
59         mean = oldMean + (value - oldMean) / n;
60         var = var + (value - mean) * (value - oldMean);
61         if (value < min)
62             min = value;
63         if (value > max)
64             max = value;
65     }
66 
67     /**
68      * Gets the best case of the stream.
69      *
70      * @return The min or max.
71      */
getBestCase()72     public double getBestCase() {
73         switch (regression_mode) {
74             case VTS_REGRESSION_MODE_DECREASING:
75                 return getMax();
76             default:
77                 return getMin();
78         }
79     }
80 
81     /**
82      * Gets the calculated min of the stream.
83      *
84      * @return The min.
85      */
getMin()86     public double getMin() {
87         return min;
88     }
89 
90     /**
91      * Gets the calculated max of the stream.
92      *
93      * @return The max.
94      */
getMax()95     public double getMax() {
96         return max;
97     }
98 
99     /**
100      * Gets the calculated mean of the stream.
101      *
102      * @return The mean.
103      */
getMean()104     public double getMean() {
105         return mean;
106     }
107 
108     /**
109      * Gets the calculated standard deviation of the stream.
110      *
111      * @return The standard deviation.
112      */
getStd()113     public double getStd() {
114         return Math.sqrt(var / (n - 1));
115     }
116 
117     /**
118      * Gets the number of elements that have passed through the stream.
119      *
120      * @return Number of elements.
121      */
getCount()122     public int getCount() {
123         return n;
124     }
125 
126     /**
127      * Gets the label for the summarized statistics.
128      *
129      * @return The (string) label.
130      */
getLabel()131     public String getLabel() {
132         return label;
133     }
134 
135     /**
136      * Gets the regression mode.
137      *
138      * @return The VtsProfilingRegressionMode value.
139      */
getRegressionMode()140     public VtsProfilingRegressionMode getRegressionMode() {
141         return regression_mode;
142     }
143 }
144