Kweku Adams | 65b5ee3 | 2018-10-24 17:55:34 -0700 | [diff] [blame] | 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 | import android.support.test.filters.LargeTest; |
| 22 | import android.support.test.runner.AndroidJUnit4; |
| 23 | |
| 24 | import org.junit.Rule; |
| 25 | import org.junit.Test; |
| 26 | import org.junit.runner.RunWith; |
| 27 | |
| 28 | import java.util.function.Predicate; |
| 29 | |
| 30 | @RunWith(AndroidJUnit4.class) |
| 31 | @LargeTest |
| 32 | public class ArraySetPerfTest { |
| 33 | private static final int NUM_ITERATIONS = 100; |
| 34 | private static final int SET_SIZE_SMALL = 10; |
| 35 | private static final int SET_SIZE_LARGE = 50; |
| 36 | |
| 37 | @Rule |
| 38 | public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); |
| 39 | |
| 40 | @Test |
| 41 | public void testValueAt_InBounds() { |
| 42 | BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 43 | ArraySet<Integer> set = new ArraySet<>(); |
| 44 | set.add(0); |
| 45 | while (state.keepRunning()) { |
| 46 | for (int i = 0; i < NUM_ITERATIONS; ++i) { |
| 47 | set.valueAt(0); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | @Test |
| 53 | public void testValueAt_OutOfBounds_Negative() { |
| 54 | BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 55 | ArraySet<Integer> set = new ArraySet<>(); |
| 56 | while (state.keepRunning()) { |
| 57 | for (int i = 0; i < NUM_ITERATIONS; ++i) { |
| 58 | try { |
| 59 | set.valueAt(-1); |
| 60 | } catch (ArrayIndexOutOfBoundsException expected) { |
| 61 | // expected |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Tests the case where ArraySet could index into its array even though the index is out of |
| 69 | * bounds. |
| 70 | */ |
| 71 | @Test |
| 72 | public void testValueAt_OutOfBounds_EdgeCase() { |
| 73 | BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 74 | ArraySet<Integer> set = new ArraySet<>(); |
| 75 | set.add(0); |
| 76 | while (state.keepRunning()) { |
| 77 | for (int i = 0; i < NUM_ITERATIONS; ++i) { |
| 78 | try { |
| 79 | set.valueAt(1); |
| 80 | } catch (ArrayIndexOutOfBoundsException expected) { |
| 81 | // expected |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * This is the same code as testRemoveIf_Small_* without the removeIf in order to measure |
| 89 | * the performance of the rest of the code in the loop. |
| 90 | */ |
| 91 | @Test |
| 92 | public void testRemoveIf_Small_Base() { |
| 93 | BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 94 | Predicate<Integer> predicate = (i) -> i % 2 == 0; |
| 95 | while (state.keepRunning()) { |
| 96 | for (int i = 0; i < NUM_ITERATIONS; ++i) { |
| 97 | ArraySet<Integer> set = new ArraySet<>(); |
| 98 | for (int j = 0; j < SET_SIZE_SMALL; ++j) { |
| 99 | set.add(j); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | @Test |
| 106 | public void testRemoveIf_Small_RemoveNothing() { |
| 107 | BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 108 | Predicate<Integer> predicate = (i) -> false; |
| 109 | while (state.keepRunning()) { |
| 110 | for (int i = 0; i < NUM_ITERATIONS; ++i) { |
| 111 | ArraySet<Integer> set = new ArraySet<>(); |
| 112 | for (int j = 0; j < SET_SIZE_SMALL; ++j) { |
| 113 | set.add(j); |
| 114 | } |
| 115 | set.removeIf(predicate); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | @Test |
| 121 | public void testRemoveIf_Small_RemoveAll() { |
| 122 | BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 123 | Predicate<Integer> predicate = (i) -> true; |
| 124 | while (state.keepRunning()) { |
| 125 | for (int i = 0; i < NUM_ITERATIONS; ++i) { |
| 126 | ArraySet<Integer> set = new ArraySet<>(); |
| 127 | for (int j = 0; j < SET_SIZE_SMALL; j++) { |
| 128 | set.add(j); |
| 129 | } |
| 130 | set.removeIf(predicate); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | @Test |
| 136 | public void testRemoveIf_Small_RemoveHalf() { |
| 137 | BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 138 | Predicate<Integer> predicate = (i) -> i % 2 == 0; |
| 139 | while (state.keepRunning()) { |
| 140 | for (int i = 0; i < NUM_ITERATIONS; ++i) { |
| 141 | ArraySet<Integer> set = new ArraySet<>(); |
| 142 | for (int j = 0; j < SET_SIZE_SMALL; ++j) { |
| 143 | set.add(j); |
| 144 | } |
| 145 | set.removeIf(predicate); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * This is the same code as testRemoveIf_Large_* without the removeIf in order to measure |
| 152 | * the performance of the rest of the code in the loop. |
| 153 | */ |
| 154 | @Test |
| 155 | public void testRemoveIf_Large_Base() { |
| 156 | BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 157 | Predicate<Integer> predicate = (i) -> i % 2 == 0; |
| 158 | while (state.keepRunning()) { |
| 159 | for (int i = 0; i < NUM_ITERATIONS; ++i) { |
| 160 | ArraySet<Integer> set = new ArraySet<>(); |
| 161 | for (int j = 0; j < SET_SIZE_LARGE; ++j) { |
| 162 | set.add(j); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | @Test |
| 169 | public void testRemoveIf_Large_RemoveNothing() { |
| 170 | BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 171 | Predicate<Integer> predicate = (i) -> false; |
| 172 | while (state.keepRunning()) { |
| 173 | for (int i = 0; i < NUM_ITERATIONS; ++i) { |
| 174 | ArraySet<Integer> set = new ArraySet<>(); |
| 175 | for (int j = 0; j < SET_SIZE_LARGE; ++j) { |
| 176 | set.add(j); |
| 177 | } |
| 178 | set.removeIf(predicate); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | @Test |
| 184 | public void testRemoveIf_Large_RemoveAll() { |
| 185 | BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 186 | Predicate<Integer> predicate = (i) -> true; |
| 187 | while (state.keepRunning()) { |
| 188 | for (int i = 0; i < NUM_ITERATIONS; ++i) { |
| 189 | ArraySet<Integer> set = new ArraySet<>(); |
| 190 | for (int j = 0; j < SET_SIZE_LARGE; ++j) { |
| 191 | set.add(j); |
| 192 | } |
| 193 | set.removeIf(predicate); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | @Test |
| 199 | public void testRemoveIf_Large_RemoveHalf() { |
| 200 | BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 201 | Predicate<Integer> predicate = (i) -> i % 2 == 0; |
| 202 | while (state.keepRunning()) { |
| 203 | for (int i = 0; i < NUM_ITERATIONS; ++i) { |
| 204 | ArraySet<Integer> set = new ArraySet<>(); |
| 205 | for (int j = 0; j < SET_SIZE_LARGE; ++j) { |
| 206 | set.add(j); |
| 207 | } |
| 208 | set.removeIf(predicate); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | } |