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