1 /*
2  * Copyright (C) 2016 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;
18 
19 import android.perftests.utils.BenchmarkState;
20 import android.perftests.utils.PerfStatusReporter;
21 import android.support.test.filters.LargeTest;
22 import android.support.test.runner.AndroidJUnit4;
23 
24 import dalvik.annotation.optimization.FastNative;
25 
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 
30 import java.util.concurrent.TimeUnit;
31 
32 @RunWith(AndroidJUnit4.class)
33 @LargeTest
34 public class SystemPerfTest {
35 
36     static {
37         System.loadLibrary("perftestscore_jni");
38     }
39 
40     @Rule
41     public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
42 
43     @Test
testNanoTimePerf()44     public void testNanoTimePerf() {
45         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
46         while (state.keepRunning()) {
47             System.nanoTime();
48         }
49     }
50 
51     @Test
testBenchmarkOverhead()52     public void testBenchmarkOverhead() {
53         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
54         while (state.keepRunning()) {}
55     }
56 
spinBlock(long durationNs)57     void spinBlock(long durationNs) {
58         long start = System.nanoTime();
59         while (System.nanoTime() - start < durationNs) {}
60     }
61 
62     @Test
testBenchmarkPauseResumeOverhead()63     public void testBenchmarkPauseResumeOverhead() {
64         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
65         while (state.keepRunning()) {
66             state.pauseTiming();
67             spinBlock(TimeUnit.MICROSECONDS.toNanos(5));
68             state.resumeTiming();
69         }
70     }
71 
72     @Test
testJniArrayNoop()73     public void testJniArrayNoop() {
74         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
75         final int[] data = new int[450];
76         while (state.keepRunning()) {
77             jintarrayArgumentNoop(data, data.length);
78         }
79     }
80 
81     @Test
testJniArrayGetLength()82     public void testJniArrayGetLength() {
83         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
84         final int[] data = new int[450];
85         while (state.keepRunning()) {
86             jintarrayGetLength(data);
87         }
88     }
89 
90     @Test
testJniArrayCriticalAccess()91     public void testJniArrayCriticalAccess() {
92         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
93         final int[] data = new int[450];
94         while (state.keepRunning()) {
95             jintarrayCriticalAccess(data, 50);
96         }
97     }
98 
99     @Test
testJniArrayBasicAccess()100     public void testJniArrayBasicAccess() {
101         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
102         final int[] data = new int[450];
103         while (state.keepRunning()) {
104             jintarrayBasicAccess(data, 50);
105         }
106     }
107 
108     @FastNative
jintarrayArgumentNoop(int[] array, int length)109     private static native void jintarrayArgumentNoop(int[] array, int length);
110     @FastNative
jintarrayGetLength(int[] array)111     private static native int jintarrayGetLength(int[] array);
112     @FastNative
jintarrayCriticalAccess(int[] array, int index)113     private static native int jintarrayCriticalAccess(int[] array, int index);
114     @FastNative
jintarrayBasicAccess(int[] array, int index)115     private static native int jintarrayBasicAccess(int[] array, int index);
116 }
117