1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may 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 implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.perftests.utils;
18 
19 import android.annotation.IntRange;
20 
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 
25 public class Stats {
26     private long mMedian, mMin, mMax;
27     private double mMean, mStandardDeviation;
28     private final List<Long> mValues;
29 
30     /* Calculate stats in constructor. */
Stats(List<Long> values)31     public Stats(List<Long> values) {
32         final int size = values.size();
33         if (size < 2) {
34             throw new IllegalArgumentException("At least two results are necessary.");
35         }
36 
37         // Make a copy since we're modifying it.
38         mValues = values = new ArrayList<>(values);
39 
40         Collections.sort(values);
41 
42         mMin = values.get(0);
43         mMax = values.get(values.size() - 1);
44 
45         mMedian = size % 2 == 0 ? (values.get(size / 2) + values.get(size / 2 - 1)) / 2 :
46                 values.get(size / 2);
47 
48         for (int i = 0; i < size; ++i) {
49             long result = values.get(i);
50             mMean += result;
51         }
52         mMean /= (double) size;
53 
54         for (int i = 0; i < size; ++i) {
55             final double tmp = values.get(i) - mMean;
56             mStandardDeviation += tmp * tmp;
57         }
58         mStandardDeviation = Math.sqrt(mStandardDeviation / (double) (size - 1));
59     }
60 
getSize()61     public int getSize() {
62         return mValues.size();
63     }
64 
getMean()65     public double getMean() {
66         return mMean;
67     }
68 
getMedian()69     public long getMedian() {
70         return mMedian;
71     }
72 
getMax()73     public long getMax() {
74         return mMax;
75     }
76 
getMin()77     public long getMin() {
78         return mMin;
79     }
80 
getStandardDeviation()81     public double getStandardDeviation() {
82         return mStandardDeviation;
83     }
84 
getPercentile(@ntRangefrom = 0, to = 100) int percentile)85     public long getPercentile(@IntRange(from = 0, to = 100) int percentile) {
86         return getPercentile(mValues, percentile);
87     }
88 
getPercentile(List<Long> values, int percentile)89     private static long getPercentile(List<Long> values, int percentile) {
90         if (percentile < 0 || percentile > 100) {
91             throw new IllegalArgumentException(
92                     "invalid percentile " + percentile + ", should be 0-100");
93         }
94         int idx = (values.size() - 1) * percentile / 100;
95         return values.get(idx);
96     }
97 }
98