blob: b1f609ea77bdf33810aa401a765baf3d5f15b201 [file] [log] [blame]
Aurimas Liutikas3f26bef2018-08-03 14:21:56 -07001/*
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
17package android.app;
18
19import static org.junit.Assert.fail;
20
Ryan Mitchell4043ca72019-06-03 16:11:24 -070021import android.content.Context;
Aurimas Liutikas3f26bef2018-08-03 14:21:56 -070022import android.content.res.Resources;
23import android.content.res.XmlResourceParser;
24import android.perftests.utils.BenchmarkState;
25import android.perftests.utils.PerfStatusReporter;
Ryan Mitchell4043ca72019-06-03 16:11:24 -070026import android.util.TypedValue;
KOUSHIK PANUGANTI412796e2018-12-17 14:16:52 -080027
Ryan Mitchell4043ca72019-06-03 16:11:24 -070028import androidx.test.InstrumentationRegistry;
KOUSHIK PANUGANTI412796e2018-12-17 14:16:52 -080029import androidx.test.filters.LargeTest;
Aurimas Liutikas3f26bef2018-08-03 14:21:56 -070030
Ryan Mitchell4043ca72019-06-03 16:11:24 -070031import com.android.perftests.core.R;
32
Aurimas Liutikas3f26bef2018-08-03 14:21:56 -070033import org.junit.Before;
34import org.junit.Rule;
35import org.junit.Test;
36import org.xmlpull.v1.XmlPullParser;
37import org.xmlpull.v1.XmlPullParserException;
38
39import java.io.IOException;
40
41/**
42 * Benchmarks for {@link android.content.res.Resources}.
43 */
44@LargeTest
45public class ResourcesPerfTest {
46 @Rule
47 public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
48
Aurimas Liutikas3f26bef2018-08-03 14:21:56 -070049 private Resources mRes;
50
Aurimas Liutikas3f26bef2018-08-03 14:21:56 -070051 @Before
52 public void setUp() {
Ryan Mitchell4043ca72019-06-03 16:11:24 -070053 Context context = InstrumentationRegistry.getTargetContext();
54 mRes = context.getResources();
Aurimas Liutikas3f26bef2018-08-03 14:21:56 -070055 }
56
Ryan Mitchell4043ca72019-06-03 16:11:24 -070057 @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 Bright25f10222020-02-14 13:55:55 -080099 mRes.getValue(com.android.internal.R.string.android_upgrading_apk, value,
Ryan Mitchell4043ca72019-06-03 16:11:24 -0700100 false /* resolve_refs */);
101 }
Aurimas Liutikas3f26bef2018-08-03 14:21:56 -0700102 }
103
104 @Test
105 public void getText() {
106 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
107 while (state.keepRunning()) {
Ryan Mitchell4043ca72019-06-03 16:11:24 -0700108 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 Liutikas3f26bef2018-08-03 14:21:56 -0700166 }
167 }
168
169 @Test
170 public void getColor() {
171 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
172 while (state.keepRunning()) {
Ryan Mitchell4043ca72019-06-03 16:11:24 -0700173 mRes.getColor(R.color.white, null);
Aurimas Liutikas3f26bef2018-08-03 14:21:56 -0700174 }
175 }
176
177 @Test
Ryan Mitchell4043ca72019-06-03 16:11:24 -0700178 public void getColorStateList() {
Aurimas Liutikas3f26bef2018-08-03 14:21:56 -0700179 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
180 while (state.keepRunning()) {
Ryan Mitchell4043ca72019-06-03 16:11:24 -0700181 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 Liutikas3f26bef2018-08-03 14:21:56 -0700190 }
191 }
192
193 @Test
194 public void getLayoutAndTravese() {
195 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
196 while (state.keepRunning()) {
Ryan Mitchell4043ca72019-06-03 16:11:24 -0700197 try (XmlResourceParser parser = mRes.getLayout(R.layout.test_relative_layout)) {
Aurimas Liutikas3f26bef2018-08-03 14:21:56 -0700198 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 Mitchell4043ca72019-06-03 16:11:24 -0700206
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}