Aurimas Liutikas | 3f26bef | 2018-08-03 14:21:56 -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.app; |
| 18 | |
| 19 | import static org.junit.Assert.fail; |
| 20 | |
Ryan Mitchell | 4043ca7 | 2019-06-03 16:11:24 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Aurimas Liutikas | 3f26bef | 2018-08-03 14:21:56 -0700 | [diff] [blame] | 22 | import android.content.res.Resources; |
| 23 | import android.content.res.XmlResourceParser; |
| 24 | import android.perftests.utils.BenchmarkState; |
| 25 | import android.perftests.utils.PerfStatusReporter; |
Ryan Mitchell | 4043ca7 | 2019-06-03 16:11:24 -0700 | [diff] [blame] | 26 | import android.util.TypedValue; |
KOUSHIK PANUGANTI | 412796e | 2018-12-17 14:16:52 -0800 | [diff] [blame] | 27 | |
Ryan Mitchell | 4043ca7 | 2019-06-03 16:11:24 -0700 | [diff] [blame] | 28 | import androidx.test.InstrumentationRegistry; |
KOUSHIK PANUGANTI | 412796e | 2018-12-17 14:16:52 -0800 | [diff] [blame] | 29 | import androidx.test.filters.LargeTest; |
Aurimas Liutikas | 3f26bef | 2018-08-03 14:21:56 -0700 | [diff] [blame] | 30 | |
Ryan Mitchell | 4043ca7 | 2019-06-03 16:11:24 -0700 | [diff] [blame] | 31 | import com.android.perftests.core.R; |
| 32 | |
Aurimas Liutikas | 3f26bef | 2018-08-03 14:21:56 -0700 | [diff] [blame] | 33 | import org.junit.Before; |
| 34 | import org.junit.Rule; |
| 35 | import org.junit.Test; |
| 36 | import org.xmlpull.v1.XmlPullParser; |
| 37 | import org.xmlpull.v1.XmlPullParserException; |
| 38 | |
| 39 | import java.io.IOException; |
| 40 | |
| 41 | /** |
| 42 | * Benchmarks for {@link android.content.res.Resources}. |
| 43 | */ |
| 44 | @LargeTest |
| 45 | public class ResourcesPerfTest { |
| 46 | @Rule |
| 47 | public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); |
| 48 | |
Aurimas Liutikas | 3f26bef | 2018-08-03 14:21:56 -0700 | [diff] [blame] | 49 | private Resources mRes; |
| 50 | |
Aurimas Liutikas | 3f26bef | 2018-08-03 14:21:56 -0700 | [diff] [blame] | 51 | @Before |
| 52 | public void setUp() { |
Ryan Mitchell | 4043ca7 | 2019-06-03 16:11:24 -0700 | [diff] [blame] | 53 | Context context = InstrumentationRegistry.getTargetContext(); |
| 54 | mRes = context.getResources(); |
Aurimas Liutikas | 3f26bef | 2018-08-03 14:21:56 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Ryan Mitchell | 4043ca7 | 2019-06-03 16:11:24 -0700 | [diff] [blame] | 57 | @Test |
| 58 | public void getValue() { |
| 59 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 60 | TypedValue value = new TypedValue(); |
| 61 | while (state.keepRunning()) { |
| 62 | mRes.getValue(R.integer.forty_two, value, false /* resolve_refs */); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | @Test |
| 67 | public void getFrameworkValue() { |
| 68 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 69 | TypedValue value = new TypedValue(); |
| 70 | while (state.keepRunning()) { |
| 71 | mRes.getValue(com.android.internal.R.integer.autofill_max_visible_datasets, value, |
| 72 | false /* resolve_refs */); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | @Test |
| 77 | public void getValueString() { |
| 78 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 79 | TypedValue value = new TypedValue(); |
| 80 | while (state.keepRunning()) { |
| 81 | mRes.getValue(R.string.long_text, value, false /* resolve_refs */); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void getFrameworkStringValue() { |
| 87 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 88 | TypedValue value = new TypedValue(); |
| 89 | while (state.keepRunning()) { |
| 90 | mRes.getValue(com.android.internal.R.string.cancel, value, false /* resolve_refs */); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | @Test |
| 95 | public void getValueManyConfigurations() { |
| 96 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 97 | TypedValue value = new TypedValue(); |
| 98 | while (state.keepRunning()) { |
Daniel Bright | 25f1022 | 2020-02-14 13:55:55 -0800 | [diff] [blame^] | 99 | mRes.getValue(com.android.internal.R.string.android_upgrading_apk, value, |
Ryan Mitchell | 4043ca7 | 2019-06-03 16:11:24 -0700 | [diff] [blame] | 100 | false /* resolve_refs */); |
| 101 | } |
Aurimas Liutikas | 3f26bef | 2018-08-03 14:21:56 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Test |
| 105 | public void getText() { |
| 106 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 107 | while (state.keepRunning()) { |
Ryan Mitchell | 4043ca7 | 2019-06-03 16:11:24 -0700 | [diff] [blame] | 108 | mRes.getText(R.string.long_text); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | |
| 113 | @Test |
| 114 | public void getFont() { |
| 115 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 116 | while (state.keepRunning()) { |
| 117 | mRes.getFont(R.font.samplefont); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | @Test |
| 122 | public void getString() { |
| 123 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 124 | while (state.keepRunning()) { |
| 125 | mRes.getString(R.string.long_text); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | @Test |
| 130 | public void getQuantityString() { |
| 131 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 132 | while (state.keepRunning()) { |
| 133 | mRes.getQuantityString(R.plurals.plurals_text, 5); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | @Test |
| 138 | public void getQuantityText() { |
| 139 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 140 | while (state.keepRunning()) { |
| 141 | mRes.getQuantityText(R.plurals.plurals_text, 5); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | @Test |
| 146 | public void getTextArray() { |
| 147 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 148 | while (state.keepRunning()) { |
| 149 | mRes.getTextArray(R.array.strings); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | @Test |
| 154 | public void getStringArray() { |
| 155 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 156 | while (state.keepRunning()) { |
| 157 | mRes.getStringArray(R.array.strings); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | @Test |
| 162 | public void getIntegerArray() { |
| 163 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 164 | while (state.keepRunning()) { |
| 165 | mRes.getIntArray(R.array.ints); |
Aurimas Liutikas | 3f26bef | 2018-08-03 14:21:56 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | @Test |
| 170 | public void getColor() { |
| 171 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 172 | while (state.keepRunning()) { |
Ryan Mitchell | 4043ca7 | 2019-06-03 16:11:24 -0700 | [diff] [blame] | 173 | mRes.getColor(R.color.white, null); |
Aurimas Liutikas | 3f26bef | 2018-08-03 14:21:56 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
| 177 | @Test |
Ryan Mitchell | 4043ca7 | 2019-06-03 16:11:24 -0700 | [diff] [blame] | 178 | public void getColorStateList() { |
Aurimas Liutikas | 3f26bef | 2018-08-03 14:21:56 -0700 | [diff] [blame] | 179 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 180 | while (state.keepRunning()) { |
Ryan Mitchell | 4043ca7 | 2019-06-03 16:11:24 -0700 | [diff] [blame] | 181 | mRes.getColorStateList(R.color.color_state_list, null); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | @Test |
| 186 | public void getVectorDrawable() { |
| 187 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 188 | while (state.keepRunning()) { |
| 189 | mRes.getDrawable(R.drawable.vector_drawable01, null); |
Aurimas Liutikas | 3f26bef | 2018-08-03 14:21:56 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
| 193 | @Test |
| 194 | public void getLayoutAndTravese() { |
| 195 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 196 | while (state.keepRunning()) { |
Ryan Mitchell | 4043ca7 | 2019-06-03 16:11:24 -0700 | [diff] [blame] | 197 | try (XmlResourceParser parser = mRes.getLayout(R.layout.test_relative_layout)) { |
Aurimas Liutikas | 3f26bef | 2018-08-03 14:21:56 -0700 | [diff] [blame] | 198 | while (parser.next() != XmlPullParser.END_DOCUMENT) { |
| 199 | // Walk the entire tree |
| 200 | } |
| 201 | } catch (IOException | XmlPullParserException exception) { |
| 202 | fail("Parsing of the layout failed. Something is really broken"); |
| 203 | } |
| 204 | } |
| 205 | } |
Ryan Mitchell | 4043ca7 | 2019-06-03 16:11:24 -0700 | [diff] [blame] | 206 | |
| 207 | @Test |
| 208 | public void getLayoutAndTraverseInvalidateCaches() { |
| 209 | mRes.flushLayoutCache(); |
| 210 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 211 | while (state.keepRunning()) { |
| 212 | try (XmlResourceParser parser = mRes.getLayout(R.layout.test_relative_layout)) { |
| 213 | while (parser.next() != XmlPullParser.END_DOCUMENT) { |
| 214 | // Walk the entire tree |
| 215 | } |
| 216 | } catch (IOException | XmlPullParserException exception) { |
| 217 | fail("Parsing of the layout failed. Something is really broken"); |
| 218 | } |
| 219 | |
| 220 | state.pauseTiming(); |
| 221 | mRes.flushLayoutCache(); |
| 222 | state.resumeTiming(); |
| 223 | } |
| 224 | } |
| 225 | } |