1 /*
2  * Copyright (C) 2022 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.libcore.regression;
18 
19 import android.perftests.utils.BenchmarkState;
20 import android.perftests.utils.PerfStatusReporter;
21 
22 import androidx.test.filters.LargeTest;
23 
24 import junitparams.JUnitParamsRunner;
25 import junitparams.Parameters;
26 
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.concurrent.atomic.AtomicInteger;
34 
35 @RunWith(JUnitParamsRunner.class)
36 @LargeTest
37 public final class MutableIntPerfTest {
38     @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
39 
40     enum Kind {
ARRAY()41         ARRAY() {
42             int[] mValue = new int[1];
43 
44             @Override
45             void timeCreate(BenchmarkState state) {
46                 while (state.keepRunning()) {
47                     mValue = new int[] {5};
48                 }
49             }
50 
51             @Override
52             void timeIncrement(BenchmarkState state) {
53                 while (state.keepRunning()) {
54                     mValue[0]++;
55                 }
56             }
57 
58             @Override
59             int timeGet(BenchmarkState state) {
60                 int sum = 0;
61                 while (state.keepRunning()) {
62                     sum += mValue[0];
63                 }
64                 return sum;
65             }
66         },
ATOMIC()67         ATOMIC() {
68             AtomicInteger mValue = new AtomicInteger();
69 
70             @Override
71             void timeCreate(BenchmarkState state) {
72                 while (state.keepRunning()) {
73                     mValue = new AtomicInteger(5);
74                 }
75             }
76 
77             @Override
78             void timeIncrement(BenchmarkState state) {
79                 while (state.keepRunning()) {
80                     mValue.incrementAndGet();
81                 }
82             }
83 
84             @Override
85             int timeGet(BenchmarkState state) {
86                 int sum = 0;
87                 while (state.keepRunning()) {
88                     sum += mValue.intValue();
89                 }
90                 return sum;
91             }
92         };
93 
timeCreate(BenchmarkState state)94         abstract void timeCreate(BenchmarkState state);
95 
timeIncrement(BenchmarkState state)96         abstract void timeIncrement(BenchmarkState state);
97 
timeGet(BenchmarkState state)98         abstract int timeGet(BenchmarkState state);
99     }
100 
getData()101     public static Collection<Object[]> getData() {
102         return Arrays.asList(new Object[][] {{Kind.ARRAY}, {Kind.ATOMIC}});
103     }
104 
105     @Test
106     @Parameters(method = "getData")
timeCreate(Kind kind)107     public void timeCreate(Kind kind) {
108         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
109         kind.timeCreate(state);
110     }
111 
112     @Test
113     @Parameters(method = "getData")
timeIncrement(Kind kind)114     public void timeIncrement(Kind kind) {
115         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
116         kind.timeIncrement(state);
117     }
118 
119     @Test
120     @Parameters(method = "getData")
timeGet(Kind kind)121     public void timeGet(Kind kind) {
122         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
123         kind.timeGet(state);
124     }
125 }
126