1 /*
2  * Copyright (C) 2012 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.simplecpu.cts;
18 
19 import android.util.Log;
20 
21 import com.android.compatibility.common.util.CtsAndroidTestCase;
22 import com.android.compatibility.common.util.DeviceReportLog;
23 import com.android.compatibility.common.util.ResultType;
24 import com.android.compatibility.common.util.ResultUnit;
25 import com.android.compatibility.common.util.Stat;
26 
27 /**
28  * Very simple CPU benchmarking to check the basic capability of CPU.
29  * Cases include
30  *   qsort
31  *   matrix multiplication (for floating point performance)
32  */
33 public class SimpleCpuTest extends CtsAndroidTestCase {
34     private static final String TAG = "BandwidthTest";
35     private static final int KB = 1024;
36     private static final int MB = 1024 * 1024;
37     private static final int NUMBER_REPEAT = 20;
38     // reject data outside +/- this value * median
39     private static final double OUTLIER_THRESHOLD = 0.1;
40     private static final String REPORT_LOG_NAME = "CtsSimpleCpuTestCases";
41 
42     @Override
setUp()43     protected void setUp() throws Exception {
44         super.setUp();
45         warmUpCpu();
46     }
47 
testSort004KB()48     public void testSort004KB() {
49         doTestSort(NUMBER_REPEAT, 4 * KB);
50     }
51 
testSort128KB()52     public void testSort128KB() {
53         doTestSort(NUMBER_REPEAT, 128 * KB);
54     }
55 
testSort001MB()56     public void testSort001MB() {
57         doTestSort(NUMBER_REPEAT, 1 * MB);
58     }
59 
60     // will fit into L1
testMatrixMultiplication032()61     public void testMatrixMultiplication032() {
62         doMatrixMultiplication(NUMBER_REPEAT, 32);
63     }
64 
65     // mostly fit into L2
testMatrixMultiplication128()66     public void testMatrixMultiplication128() {
67         doMatrixMultiplication(NUMBER_REPEAT, 128);
68     }
69 
70     // may fit into L2
testMatrixMultiplication200()71     public void testMatrixMultiplication200() {
72         doMatrixMultiplication(NUMBER_REPEAT, 200);
73     }
74 
testMatrixMultiplication400()75     public void testMatrixMultiplication400() {
76         doMatrixMultiplication(NUMBER_REPEAT, 400);
77     }
78 
79     // will exceed L2
testMatrixMultiplication600()80     public void testMatrixMultiplication600() {
81         doMatrixMultiplication(NUMBER_REPEAT, 600);
82     }
83 
84     /**
85      * run some code to force full CPU freq.
86      */
warmUpCpu()87     private void warmUpCpu() {
88         CpuNative.runSort(1 * MB, 10);
89     }
90 
91     /**
92      * qsort test
93      * @param numberRepeat
94      * @param arrayLength
95      */
doTestSort(int numberRepeat, int arrayLength)96     private void doTestSort(int numberRepeat, int arrayLength) {
97         final int numberRepeatInEachCall = 10;
98         double[] result = new double[numberRepeat];
99         for (int i = 0; i < numberRepeat; i++) {
100             result[i] = CpuNative.runSort(arrayLength, numberRepeatInEachCall);
101         }
102         String streamName = "do_test_sort";
103         DeviceReportLog report = new DeviceReportLog(REPORT_LOG_NAME, streamName);
104         report.addValue("array_length", arrayLength, ResultType.NEUTRAL, ResultUnit.NONE);
105         report.addValues("sorting_time", result, ResultType.LOWER_BETTER, ResultUnit.MS);
106         Stat.StatResult stat = Stat.getStatWithOutlierRejection(result, OUTLIER_THRESHOLD);
107         if (stat.mDataCount != result.length) {
108             Log.w(TAG, "rejecting " + (result.length - stat.mDataCount) + " outliers");
109         }
110         report.setSummary("sorting_time_average", stat.mAverage, ResultType.LOWER_BETTER, ResultUnit.MS);
111         report.submit(getInstrumentation());
112     }
113 
114     /**
115      * Matrix multiplication test, nxn matrix multiplication
116      * @param numberRepeat
117      * @param n should be multiple of 8
118      */
doMatrixMultiplication(int numberRepeat, int n)119     private void doMatrixMultiplication(int numberRepeat, int n) {
120         assertTrue(n % 8 == 0);
121         final int numberRepeatInEachCall = 10;
122         double[] result = new double[numberRepeat];
123         for (int i = 0; i < numberRepeat; i++) {
124             result[i] = CpuNative.runMatrixMultiplication(n, numberRepeatInEachCall);
125         }
126         String streamName = "do_matrix_multiplication";
127         DeviceReportLog report = new DeviceReportLog(REPORT_LOG_NAME, streamName);
128         report.addValue("matrix_dimension", n, ResultType.NEUTRAL, ResultUnit.NONE);
129         report.addValues("matrix_mutiplication_time", result, ResultType.LOWER_BETTER,
130                 ResultUnit.MS);
131         Stat.StatResult stat = Stat.getStatWithOutlierRejection(result, OUTLIER_THRESHOLD);
132         if (stat.mDataCount != result.length) {
133             Log.w(TAG, "rejecting " + (result.length - stat.mDataCount) + " outliers");
134         }
135         report.setSummary("matrix_mutiplication_time_average", stat.mAverage,
136                 ResultType.LOWER_BETTER, ResultUnit.MS);
137         report.submit(getInstrumentation());
138     }
139 
140 }
141