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 java.util.ArrayList; 20 import java.util.Collections; 21 import java.util.List; 22 23 public class Stats { 24 private long mMedian, mMin, mMax, mPercentile90, mPercentile95; 25 private double mMean, mStandardDeviation; 26 27 /* Calculate stats in constructor. */ 28 public Stats(List<Long> values) { 29 // make a copy since we're modifying it 30 values = new ArrayList<>(values); 31 final int size = values.size(); 32 if (size < 2) { 33 throw new IllegalArgumentException("At least two results are necessary."); 34 } 35 36 Collections.sort(values); 37 38 mMin = values.get(0); 39 mMax = values.get(values.size() - 1); 40 41 mMedian = size % 2 == 0 ? (values.get(size / 2) + values.get(size / 2 - 1)) / 2 : 42 values.get(size / 2); 43 mPercentile90 = getPercentile(values, 90); 44 mPercentile95 = getPercentile(values, 95); 45 46 for (int i = 0; i < size; ++i) { 47 long result = values.get(i); 48 mMean += result; 49 } 50 mMean /= (double) size; 51 52 for (int i = 0; i < size; ++i) { 53 final double tmp = values.get(i) - mMean; 54 mStandardDeviation += tmp * tmp; 55 } 56 mStandardDeviation = Math.sqrt(mStandardDeviation / (double) (size - 1)); 57 } 58 59 public double getMean() { 60 return mMean; 61 } 62 63 public long getMedian() { 64 return mMedian; 65 } 66 67 public long getMax() { 68 return mMax; 69 } 70 71 public long getMin() { 72 return mMin; 73 } 74 75 public double getStandardDeviation() { 76 return mStandardDeviation; 77 } 78 79 public long getPercentile90() { 80 return mPercentile90; 81 } 82 83 public long getPercentile95() { 84 return mPercentile95; 85 } 86 87 private static long getPercentile(List<Long> values, int percentile) { 88 if (percentile < 0 || percentile > 100) { 89 throw new IllegalArgumentException( 90 "invalid percentile " + percentile + ", should be 0-100"); 91 } 92 int idx = (values.size() - 1) * percentile / 100; 93 return values.get(idx); 94 } 95 } 96