Debian Codegraph @

apct-tests/perftests/core/src/android/widget/TextViewPrecomputedTextPerfTest.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.widget;
18
19 import static android.widget.TextView.UNKNOWN_BORING;
20
21 import android.content.Context;
22 import android.graphics.Canvas;
23 import android.graphics.RecordingCanvas;
24 import android.graphics.RenderNode;
25 import android.perftests.utils.BenchmarkState;
26 import android.perftests.utils.PerfStatusReporter;
27 import android.text.BoringLayout;
28 import android.text.Layout;
29 import android.text.PrecomputedText;
30 import android.text.TextPaint;
31 import android.text.TextPerfUtils;
32 import android.view.View.MeasureSpec;
33
34 import androidx.test.InstrumentationRegistry;
35 import androidx.test.filters.LargeTest;
36 import androidx.test.runner.AndroidJUnit4;
37
38 import org.junit.Before;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42
43 @LargeTest
44 @RunWith(AndroidJUnit4.class)
45 public class TextViewPrecomputedTextPerfTest {
46 private static final int WORD_LENGTH = 9; // Random word has 9 characters.
47 private static final int WORDS_IN_LINE = 8; // Roughly, 8 words in a line.
48 private static final boolean NO_STYLE_TEXT = false;
49 private static final boolean STYLE_TEXT = true;
50
51 private static TextPaint PAINT = new TextPaint();
52 private static final int TEXT_WIDTH = WORDS_IN_LINE * WORD_LENGTH * (int) PAINT.getTextSize();
53
54 public TextViewPrecomputedTextPerfTest() {}
55
56 private static class TestableTextView extends TextView {
57 public TestableTextView(Context ctx) {
58 super(ctx);
59 }
60
61 public void onMeasure(int w, int h) {
62 super.onMeasure(w, h);
63 }
64
65 public void onDraw(Canvas canvas) {
66 super.onDraw(canvas);
67 }
68 }
69
70 @Rule
71 public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
72
73 private TextPerfUtils mTextUtil = new TextPerfUtils();
74
75 @Before
76 public void setUp() {
77 mTextUtil.resetRandom(0 /* seed */);
78 }
79
80 private static Context getContext() {
81 return InstrumentationRegistry.getTargetContext();
82 }
83
84 @Test
85 public void testNewLayout_RandomText() {
86 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
87 BoringLayout.Metrics metrics = new BoringLayout.Metrics();
88 while (state.keepRunning()) {
89 state.pauseTiming();
90 final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT);
91 final TextView textView = new TextView(getContext());
92 textView.setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED);
93 textView.setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL);
94 textView.setText(text);
95 Canvas.freeTextLayoutCaches();
96 state.resumeTiming();
97
98 textView.makeNewLayout(TEXT_WIDTH, TEXT_WIDTH, UNKNOWN_BORING, UNKNOWN_BORING,
99 TEXT_WIDTH, false);
100 }
101 }
102
103 @Test
104 public void testNewLayout_RandomText_Selectable() {
105 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
106 BoringLayout.Metrics metrics = new BoringLayout.Metrics();
107 while (state.keepRunning()) {
108 state.pauseTiming();
109 final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT);
110 final TextView textView = new TextView(getContext());
111 textView.setTextIsSelectable(true);
112 textView.setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED);
113 textView.setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL);
114 textView.setText(text);
115 Canvas.freeTextLayoutCaches();
116 state.resumeTiming();
117
118 textView.makeNewLayout(TEXT_WIDTH, TEXT_WIDTH, UNKNOWN_BORING, UNKNOWN_BORING,
119 TEXT_WIDTH, false);
120 }
121 }
122
123 @Test
124 public void testNewLayout_PrecomputedText() {
125 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
126 BoringLayout.Metrics metrics = new BoringLayout.Metrics();
127 while (state.keepRunning()) {
128 state.pauseTiming();
129 final PrecomputedText.Params params = new PrecomputedText.Params.Builder(PAINT)
130 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL)
131 .setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED).build();
132 final CharSequence text = PrecomputedText.create(
133 mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), params);
134 final TextView textView = new TextView(getContext());
135 textView.setTextMetricsParams(params);
136 textView.setText(text);
137 Canvas.freeTextLayoutCaches();
138 state.resumeTiming();
139
140 textView.makeNewLayout(TEXT_WIDTH, TEXT_WIDTH, UNKNOWN_BORING, UNKNOWN_BORING,
141 TEXT_WIDTH, false);
142 }
143 }
144
145 @Test
146 public void testNewLayout_PrecomputedText_Selectable() {
147 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
148 BoringLayout.Metrics metrics = new BoringLayout.Metrics();
149 while (state.keepRunning()) {
150 state.pauseTiming();
151 final PrecomputedText.Params params = new PrecomputedText.Params.Builder(PAINT)
152 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL)
153 .setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED).build();
154 final CharSequence text = PrecomputedText.create(
155 mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), params);
156 final TextView textView = new TextView(getContext());
157 textView.setTextIsSelectable(true);
158 textView.setTextMetricsParams(params);
159 textView.setText(text);
160 Canvas.freeTextLayoutCaches();
161 state.resumeTiming();
162
163 textView.makeNewLayout(TEXT_WIDTH, TEXT_WIDTH, UNKNOWN_BORING, UNKNOWN_BORING,
164 TEXT_WIDTH, false);
165 }
166 }
167
168 @Test
169 public void testSetText_RandomText() {
170 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
171 BoringLayout.Metrics metrics = new BoringLayout.Metrics();
172 while (state.keepRunning()) {
173 state.pauseTiming();
174 final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT);
175 final TextView textView = new TextView(getContext());
176 textView.setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED);
177 textView.setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL);
178 Canvas.freeTextLayoutCaches();
179 state.resumeTiming();
180
181 textView.setText(text);
182 }
183 }
184
185 @Test
186 public void testSetText_RandomText_Selectable() {
187 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
188 BoringLayout.Metrics metrics = new BoringLayout.Metrics();
189 while (state.keepRunning()) {
190 state.pauseTiming();
191 final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT);
192 final TextView textView = new TextView(getContext());
193 textView.setTextIsSelectable(true);
194 textView.setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED);
195 textView.setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL);
196 Canvas.freeTextLayoutCaches();
197 state.resumeTiming();
198
199 textView.setText(text);
200 }
201 }
202
203 @Test
204 public void testSetText_PrecomputedText() {
205 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
206 BoringLayout.Metrics metrics = new BoringLayout.Metrics();
207 while (state.keepRunning()) {
208 state.pauseTiming();
209 final PrecomputedText.Params params = new PrecomputedText.Params.Builder(PAINT)
210 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL)
211 .setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED).build();
212 final CharSequence text = PrecomputedText.create(
213 mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), params);
214 final TextView textView = new TextView(getContext());
215 textView.setTextMetricsParams(params);
216 Canvas.freeTextLayoutCaches();
217 state.resumeTiming();
218
219 textView.setText(text);
220 }
221 }
222
223 @Test
224 public void testSetText_PrecomputedText_Selectable() {
225 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
226 BoringLayout.Metrics metrics = new BoringLayout.Metrics();
227 while (state.keepRunning()) {
228 state.pauseTiming();
229 final PrecomputedText.Params params = new PrecomputedText.Params.Builder(PAINT)
230 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL)
231 .setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED).build();
232 final CharSequence text = PrecomputedText.create(
233 mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), params);
234 final TextView textView = new TextView(getContext());
235 textView.setTextIsSelectable(true);
236 textView.setTextMetricsParams(params);
237 Canvas.freeTextLayoutCaches();
238 state.resumeTiming();
239
240 textView.setText(text);
241 }
242 }
243
244 @Test
245 public void testOnMeasure_RandomText() {
246 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
247 int width = MeasureSpec.makeMeasureSpec(TEXT_WIDTH, MeasureSpec.AT_MOST);
248 int height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
249 while (state.keepRunning()) {
250 state.pauseTiming();
251 final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT);
252 final TestableTextView textView = new TestableTextView(getContext());
253 textView.setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED);
254 textView.setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL);
255 textView.setText(text);
256 textView.nullLayouts();
257 Canvas.freeTextLayoutCaches();
258 state.resumeTiming();
259
260 textView.onMeasure(width, height);
261 }
262 }
263
264 @Test
265 public void testOnMeasure_RandomText_Selectable() {
266 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
267 int width = MeasureSpec.makeMeasureSpec(TEXT_WIDTH, MeasureSpec.AT_MOST);
268 int height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
269 while (state.keepRunning()) {
270 state.pauseTiming();
271 final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT);
272 final TestableTextView textView = new TestableTextView(getContext());
273 textView.setTextIsSelectable(true);
274 textView.setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED);
275 textView.setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL);
276 textView.setText(text);
277 textView.nullLayouts();
278 Canvas.freeTextLayoutCaches();
279 state.resumeTiming();
280
281 textView.onMeasure(width, height);
282 }
283 }
284
285 @Test
286 public void testOnMeasure_PrecomputedText() {
287 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
288 int width = MeasureSpec.makeMeasureSpec(TEXT_WIDTH, MeasureSpec.AT_MOST);
289 int height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
290 while (state.keepRunning()) {
291 state.pauseTiming();
292 final PrecomputedText.Params params = new PrecomputedText.Params.Builder(PAINT)
293 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL)
294 .setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED).build();
295 final CharSequence text = PrecomputedText.create(
296 mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), params);
297 final TestableTextView textView = new TestableTextView(getContext());
298 textView.setTextMetricsParams(params);
299 textView.setText(text);
300 textView.nullLayouts();
301 Canvas.freeTextLayoutCaches();
302 state.resumeTiming();
303
304 textView.onMeasure(width, height);
305 }
306 }
307
308 @Test
309 public void testOnMeasure_PrecomputedText_Selectable() {
310 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
311 int width = MeasureSpec.makeMeasureSpec(TEXT_WIDTH, MeasureSpec.AT_MOST);
312 int height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
313 while (state.keepRunning()) {
314 state.pauseTiming();
315 final PrecomputedText.Params params = new PrecomputedText.Params.Builder(PAINT)
316 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL)
317 .setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED).build();
318 final CharSequence text = PrecomputedText.create(
319 mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), params);
320 final TestableTextView textView = new TestableTextView(getContext());
321 textView.setTextIsSelectable(true);
322 textView.setTextMetricsParams(params);
323 textView.setText(text);
324 textView.nullLayouts();
325 Canvas.freeTextLayoutCaches();
326 state.resumeTiming();
327
328 textView.onMeasure(width, height);
329 }
330 }
331
332 @Test
333 public void testOnDraw_RandomText() {
334 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
335 int width = MeasureSpec.makeMeasureSpec(TEXT_WIDTH, MeasureSpec.AT_MOST);
336 int height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
337 final RenderNode node = RenderNode.create("benchmark", null);
338 while (state.keepRunning()) {
339 state.pauseTiming();
340 final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT);
341 final TestableTextView textView = new TestableTextView(getContext());
342 textView.setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED);
343 textView.setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL);
344 textView.setText(text);
345 textView.measure(width, height);
346 textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
347 final RecordingCanvas c = node.beginRecording(
348 textView.getMeasuredWidth(), textView.getMeasuredHeight());
349 textView.nullLayouts();
350 Canvas.freeTextLayoutCaches();
351 state.resumeTiming();
352
353 textView.onDraw(c);
354 node.endRecording();
355 }
356 }
357
358 @Test
359 public void testOnDraw_RandomText_Selectable() {
360 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
361 int width = MeasureSpec.makeMeasureSpec(TEXT_WIDTH, MeasureSpec.AT_MOST);
362 int height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
363 final RenderNode node = RenderNode.create("benchmark", null);
364 while (state.keepRunning()) {
365 state.pauseTiming();
366 final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT);
367 final TestableTextView textView = new TestableTextView(getContext());
368 textView.setTextIsSelectable(true);
369 textView.setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED);
370 textView.setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL);
371 textView.setText(text);
372 textView.measure(width, height);
373 textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
374 final RecordingCanvas c = node.beginRecording(
375 textView.getMeasuredWidth(), textView.getMeasuredHeight());
376 textView.nullLayouts();
377 Canvas.freeTextLayoutCaches();
378 state.resumeTiming();
379
380 textView.onDraw(c);
381 node.endRecording();
382 }
383 }
384
385 @Test
386 public void testOnDraw_PrecomputedText() {
387 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
388 int width = MeasureSpec.makeMeasureSpec(TEXT_WIDTH, MeasureSpec.AT_MOST);
389 int height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
390 final RenderNode node = RenderNode.create("benchmark", null);
391 while (state.keepRunning()) {
392 state.pauseTiming();
393 final PrecomputedText.Params params = new PrecomputedText.Params.Builder(PAINT)
394 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL)
395 .setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED).build();
396 final CharSequence text = PrecomputedText.create(
397 mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), params);
398 final TestableTextView textView = new TestableTextView(getContext());
399 textView.setTextMetricsParams(params);
400 textView.setText(text);
401 textView.measure(width, height);
402 textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
403 final RecordingCanvas c = node.beginRecording(
404 textView.getMeasuredWidth(), textView.getMeasuredHeight());
405 textView.nullLayouts();
406 Canvas.freeTextLayoutCaches();
407 state.resumeTiming();
408
409 textView.onDraw(c);
410 node.endRecording();
411 }
412 }
413
414 @Test
415 public void testOnDraw_PrecomputedText_Selectable() {
416 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
417 int width = MeasureSpec.makeMeasureSpec(MeasureSpec.AT_MOST, TEXT_WIDTH);
418 int height = MeasureSpec.makeMeasureSpec(MeasureSpec.UNSPECIFIED, 0);
419 final RenderNode node = RenderNode.create("benchmark", null);
420 while (state.keepRunning()) {
421 state.pauseTiming();
422 final PrecomputedText.Params params = new PrecomputedText.Params.Builder(PAINT)
423 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL)
424 .setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED).build();
425 final CharSequence text = PrecomputedText.create(
426 mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), params);
427 final TestableTextView textView = new TestableTextView(getContext());
428 textView.setTextIsSelectable(true);
429 textView.setTextMetricsParams(params);
430 textView.setText(text);
431 textView.measure(width, height);
432 textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
433 final RecordingCanvas c = node.beginRecording(
434 textView.getMeasuredWidth(), textView.getMeasuredHeight());
435 textView.nullLayouts();
436 Canvas.freeTextLayoutCaches();
437 state.resumeTiming();
438
439 textView.onDraw(c);
440 node.endRecording();
441 }
442 }
443 }