1 /*
2  * Copyright (C) 2017 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 package android.multiuser;
17 
18 import android.os.Bundle;
19 
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.concurrent.TimeUnit;
23 
24 public class BenchmarkResults {
25     /** If the test fails, output this value as a signal of the failure. */
26     public static final long DECLARED_VALUE_IF_ERROR_MS = -10;
27 
28     private final ArrayList<Long> mResults = new ArrayList<>();
29 
addDuration(long duration)30     public void addDuration(long duration) {
31         mResults.add(TimeUnit.NANOSECONDS.toMillis(duration));
32     }
33 
getStatsToReport()34     public Bundle getStatsToReport() {
35         final Bundle stats = new Bundle();
36         stats.putDouble("Mean (ms)", mean());
37         return stats;
38     }
39 
getStatsToLog()40     public Bundle getStatsToLog() {
41         final Bundle stats = new Bundle();
42         stats.putDouble("Mean (ms)", mean());
43         stats.putDouble("Median (ms)", median());
44         stats.putDouble("Sigma (ms)", standardDeviation());
45         return stats;
46     }
47 
48     /**
49      * Same as {@link #getStatsToReport()} but for failure,
50      * using {@link #DECLARED_VALUE_IF_ERROR_MS}.
51      */
getFailedStatsToReport()52     public static Bundle getFailedStatsToReport() {
53         final Bundle stats = new Bundle();
54         stats.putDouble("Mean (ms)", DECLARED_VALUE_IF_ERROR_MS);
55         return stats;
56     }
57 
58     /**
59      * Same as {@link #getStatsToLog()} but for failure,
60      * using {@link #DECLARED_VALUE_IF_ERROR_MS}.
61      */
getFailedStatsToLog()62     public static Bundle getFailedStatsToLog() {
63         final Bundle stats = new Bundle();
64         stats.putDouble("Mean (ms)", DECLARED_VALUE_IF_ERROR_MS);
65         stats.putDouble("Median (ms)", DECLARED_VALUE_IF_ERROR_MS);
66         stats.putDouble("Sigma (ms)", DECLARED_VALUE_IF_ERROR_MS);
67         return stats;
68     }
69 
getAllDurations()70     public ArrayList<Long> getAllDurations() {
71         return mResults;
72     }
73 
mean()74     private double mean() {
75         final int size = mResults.size();
76         long sum = 0;
77         for (int i = 0; i < size; ++i) {
78             sum += mResults.get(i);
79         }
80         return (double) sum / size;
81     }
82 
median()83     private double median() {
84         final int size = mResults.size();
85         if (size == 0) {
86             return 0f;
87         }
88 
89         final ArrayList<Long> resultsCopy = new ArrayList<>(mResults);
90         Collections.sort(resultsCopy);
91         final int idx = size / 2;
92         return size % 2 == 0
93                 ? (double) (resultsCopy.get(idx) + resultsCopy.get(idx - 1)) / 2
94                 : resultsCopy.get(idx);
95     }
96 
standardDeviation()97     private double standardDeviation() {
98         final int size = mResults.size();
99         if (size == 0) {
100             return 0f;
101         }
102         final double mean = mean();
103         double sd = 0;
104         for (int i = 0; i < size; ++i) {
105             double diff = mResults.get(i) - mean;
106             sd += diff * diff;
107         }
108         return Math.sqrt(sd / size);
109     }
110 }
111