Debian Codegraph @

apct-tests/perftests/core/src/android/util/ArrayMapPerfTest.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2018 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.util;
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 org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28
29 import java.util.function.BiConsumer;
30 import java.util.function.BiFunction;
31
32 @RunWith(AndroidJUnit4.class)
33 @LargeTest
34 public class ArrayMapPerfTest {
35 private static final int NUM_ITERATIONS = 100;
36 private static final int SET_SIZE_SMALL = 10;
37 private static final int SET_SIZE_LARGE = 50;
38
39 @Rule
40 public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
41
42 @Test
43 public void testForEach_Small() {
44 BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
45 BiConsumer<String, Integer> consumer = (s, i) -> {
46 };
47 while (state.keepRunning()) {
48 for (int i = 0; i < NUM_ITERATIONS; ++i) {
49 ArrayMap<String, Integer> map = new ArrayMap<>();
50 for (int j = 0; j < SET_SIZE_SMALL; j++) {
51 map.put(Integer.toString(j), j);
52 }
53 map.forEach(consumer);
54 }
55 }
56 }
57
58 @Test
59 public void testForEach_Large() {
60 BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
61 BiConsumer<String, Integer> consumer = (s, i) -> {
62 };
63 while (state.keepRunning()) {
64 for (int i = 0; i < NUM_ITERATIONS; ++i) {
65 ArrayMap<String, Integer> map = new ArrayMap<>();
66 for (int j = 0; j < SET_SIZE_LARGE; j++) {
67 map.put(Integer.toString(j), j);
68 }
69 map.forEach(consumer);
70 }
71 }
72 }
73
74 @Test
75 public void testReplaceAll_Small() {
76 BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
77 BiFunction<Integer, Integer, Integer> function = (k, v) -> 2 * v;
78 while (state.keepRunning()) {
79 for (int i = 0; i < NUM_ITERATIONS; ++i) {
80 ArrayMap<Integer, Integer> map = new ArrayMap<>();
81 for (int j = 0; j < SET_SIZE_SMALL; j++) {
82 map.put(j, j);
83 }
84 map.replaceAll(function);
85 }
86 }
87 }
88
89 @Test
90 public void testReplaceAll_Large() {
91 BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
92 BiFunction<Integer, Integer, Integer> function = (k, v) -> 2 * v;
93 while (state.keepRunning()) {
94 for (int i = 0; i < NUM_ITERATIONS; ++i) {
95 ArrayMap<Integer, Integer> map = new ArrayMap<>();
96 for (int j = 0; j < SET_SIZE_LARGE; j++) {
97 map.put(j, j);
98 }
99 map.replaceAll(function);
100 }
101 }
102 }
103 }