1 /*
2  * Copyright (C) 2019 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.keystore.cts;
18 
19 import junit.framework.TestCase;
20 
21 import java.util.Arrays;
22 
23 /**
24  * Simple struct to package test results. All times are in nanoseconds. Doubles are used rather than
25  * longs because arithmetic on nanoseconds in longs can overflow.
26  */
27 public class PerformanceTestResult {
28     private double mSetupTime;
29     private int mSampleCount = 0;
30     private double[] mSamples = new double[PerformanceTestBase.TEST_ITERATION_LIMIT];
31     private double mTeardownTime;
32 
addSetupTime(double timeInNs)33     public void addSetupTime(double timeInNs) {
34         mSetupTime += timeInNs / PerformanceTestBase.MS_PER_NS;
35     }
36 
getSetupTime()37     public double getSetupTime() {
38         return mSetupTime;
39     }
40 
addTeardownTime(double timeInNs)41     public void addTeardownTime(double timeInNs) {
42         mTeardownTime += timeInNs / PerformanceTestBase.MS_PER_NS;
43     }
44 
getTearDownTime()45     public double getTearDownTime() {
46         return mTeardownTime;
47     }
48 
addMeasurement(double timeInNs)49     public void addMeasurement(double timeInNs) {
50         if (mSampleCount == PerformanceTestBase.TEST_ITERATION_LIMIT) {
51             TestCase.fail("Array is full, this is a test bug.");
52         }
53         mSamples[mSampleCount++] = timeInNs / PerformanceTestBase.MS_PER_NS;
54     }
55 
getSampleCount()56     public int getSampleCount() {
57         return mSampleCount;
58     }
59 
getTotalTime()60     public double getTotalTime() {
61         double sum = 0;
62         for (int i = 0; i < mSampleCount; ++i) {
63             sum += mSamples[i];
64         }
65         return sum;
66     }
67 
getTotalTimeSq()68     public double getTotalTimeSq() {
69         double sum = 0;
70         for (int i = 0; i < mSampleCount; ++i) {
71             sum += (double) mSamples[i] * mSamples[i];
72         }
73         return sum;
74     }
75 
getMedian()76     public double getMedian() {
77         return getPercentile(0.5);
78     }
79 
getPercentile(double v)80     public double getPercentile(double v) {
81         Arrays.sort(mSamples, 0, mSampleCount);
82         return mSamples[(int) Math.ceil(mSampleCount * v) - 1];
83     }
84 
getMean()85     public double getMean() {
86         return getTotalTime() / mSampleCount;
87     }
88 
getSampleStdDev()89     public double getSampleStdDev() {
90         double totalTime = getTotalTime();
91         return Math.sqrt(mSampleCount * getTotalTimeSq() - totalTime * totalTime)
92                 / (mSampleCount * (mSampleCount - 1));
93     }
94 
95     @Override
toString()96     public String toString() {
97         return mSampleCount
98                 + ","
99                 + getMean()
100                 + ","
101                 + getSampleStdDev()
102                 + ","
103                 + getMedian()
104                 + ","
105                 + getPercentile(0.9);
106     }
107 }
108