Debian Codegraph @

apct-tests/perftests/core/src/android/text/StaticLayoutPerfTest.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17 package android.text;
18
19 import android.graphics.Canvas;
20 import android.graphics.RecordingCanvas;
21 import android.graphics.RenderNode;
22 import android.perftests.utils.BenchmarkState;
23 import android.perftests.utils.PerfStatusReporter;
24
25 import androidx.test.filters.LargeTest;
26 import androidx.test.runner.AndroidJUnit4;
27
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32
33 @LargeTest
34 @RunWith(AndroidJUnit4.class)
35 public class StaticLayoutPerfTest {
36 private static final int WORD_LENGTH = 9; // Random word has 9 characters.
37 private static final int WORDS_IN_LINE = 8; // Roughly, 8 words in a line.
38 private static final boolean NO_STYLE_TEXT = false;
39 private static final boolean STYLE_TEXT = true;
40
41 private static TextPaint PAINT = new TextPaint();
42 private static final int TEXT_WIDTH = WORDS_IN_LINE * WORD_LENGTH * (int) PAINT.getTextSize();
43
44 public StaticLayoutPerfTest() {}
45
46 @Rule
47 public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
48
49 private TextPerfUtils mTextUtil = new TextPerfUtils();
50
51 @Before
52 public void setUp() {
53 mTextUtil.resetRandom(0 /* seed */);
54 }
55
56 private PrecomputedText makeMeasured(CharSequence text, TextPaint paint) {
57 PrecomputedText.Params param = new PrecomputedText.Params.Builder(paint).build();
58 return PrecomputedText.create(text, param);
59 }
60
61 private PrecomputedText makeMeasured(CharSequence text, TextPaint paint, int strategy,
62 int frequency) {
63 PrecomputedText.Params param = new PrecomputedText.Params.Builder(paint)
64 .setHyphenationFrequency(frequency).setBreakStrategy(strategy).build();
65 return PrecomputedText.create(text, param);
66 }
67
68 @Test
69 public void testCreate_FixedText_NoStyle_Greedy_NoHyphenation() {
70 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
71 final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT);
72 while (state.keepRunning()) {
73 StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH)
74 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NONE)
75 .setBreakStrategy(Layout.BREAK_STRATEGY_SIMPLE)
76 .build();
77 }
78 }
79
80 @Test
81 public void testCreate_RandomText_NoStyled_Greedy_NoHyphenation() {
82 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
83 while (state.keepRunning()) {
84 state.pauseTiming();
85 final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT);
86 state.resumeTiming();
87
88 StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH)
89 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NONE)
90 .setBreakStrategy(Layout.BREAK_STRATEGY_SIMPLE)
91 .build();
92 }
93 }
94
95 @Test
96 public void testCreate_RandomText_NoStyled_Greedy_Hyphenation() {
97 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
98 while (state.keepRunning()) {
99 state.pauseTiming();
100 final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT);
101 state.resumeTiming();
102
103 StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH)
104 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL)
105 .setBreakStrategy(Layout.BREAK_STRATEGY_SIMPLE)
106 .build();
107 }
108 }
109
110 @Test
111 public void testCreate_RandomText_NoStyled_Balanced_NoHyphenation() {
112 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
113 while (state.keepRunning()) {
114 state.pauseTiming();
115 final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT);
116 state.resumeTiming();
117
118 StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH)
119 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NONE)
120 .setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED)
121 .build();
122 }
123 }
124
125 @Test
126 public void testCreate_RandomText_NoStyled_Balanced_Hyphenation() {
127 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
128 while (state.keepRunning()) {
129 state.pauseTiming();
130 final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT);
131 state.resumeTiming();
132
133 StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH)
134 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL)
135 .setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED)
136 .build();
137 }
138 }
139
140 @Test
141 public void testCreate_RandomText_NoStyled_Balanced_Hyphenation_Fast() {
142 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
143 while (state.keepRunning()) {
144 state.pauseTiming();
145 final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT);
146 state.resumeTiming();
147
148 StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH)
149 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL_FAST)
150 .setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED)
151 .build();
152 }
153 }
154
155 @Test
156 public void testCreate_RandomText_Styled_Greedy_NoHyphenation() {
157 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
158 while (state.keepRunning()) {
159 state.pauseTiming();
160 final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, STYLE_TEXT);
161 state.resumeTiming();
162
163 StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH)
164 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NONE)
165 .setBreakStrategy(Layout.BREAK_STRATEGY_SIMPLE)
166 .build();
167 }
168 }
169
170 @Test
171 public void testCreate_PrecomputedText_NoStyled_Greedy_NoHyphenation() {
172 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
173 while (state.keepRunning()) {
174 state.pauseTiming();
175 final PrecomputedText text = makeMeasured(
176 mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), PAINT,
177 Layout.BREAK_STRATEGY_SIMPLE, Layout.HYPHENATION_FREQUENCY_NONE);
178 state.resumeTiming();
179
180 StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH)
181 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NONE)
182 .setBreakStrategy(Layout.BREAK_STRATEGY_SIMPLE)
183 .build();
184 }
185 }
186
187 @Test
188 public void testCreate_PrecomputedText_NoStyled_Greedy_NoHyphenation_DirDifferent() {
189 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
190 while (state.keepRunning()) {
191 state.pauseTiming();
192 final PrecomputedText text = makeMeasured(
193 mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), PAINT,
194 Layout.BREAK_STRATEGY_SIMPLE, Layout.HYPHENATION_FREQUENCY_NONE);
195 Canvas.freeTextLayoutCaches();
196 state.resumeTiming();
197
198 StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH)
199 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NONE)
200 .setBreakStrategy(Layout.BREAK_STRATEGY_SIMPLE)
201 .setTextDirection(TextDirectionHeuristics.RTL)
202 .build();
203 }
204 }
205
206 @Test
207 public void testCreate_PrecomputedText_NoStyled_Greedy_Hyphenation() {
208 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
209 while (state.keepRunning()) {
210 state.pauseTiming();
211 final PrecomputedText text = makeMeasured(
212 mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), PAINT,
213 Layout.BREAK_STRATEGY_SIMPLE, Layout.HYPHENATION_FREQUENCY_NORMAL);
214 state.resumeTiming();
215
216 StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH)
217 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL)
218 .setBreakStrategy(Layout.BREAK_STRATEGY_SIMPLE)
219 .build();
220 }
221 }
222
223 @Test
224 public void testCreate_PrecomputedText_NoStyled_Balanced_NoHyphenation() {
225 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
226 while (state.keepRunning()) {
227 state.pauseTiming();
228 final PrecomputedText text = makeMeasured(
229 mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), PAINT,
230 Layout.BREAK_STRATEGY_BALANCED, Layout.HYPHENATION_FREQUENCY_NONE);
231 state.resumeTiming();
232
233 StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH)
234 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NONE)
235 .setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED)
236 .build();
237 }
238 }
239
240 @Test
241 public void testCreate_PrecomputedText_NoStyled_Balanced_Hyphenation() {
242 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
243 while (state.keepRunning()) {
244 state.pauseTiming();
245 final PrecomputedText text = makeMeasured(
246 mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), PAINT,
247 Layout.BREAK_STRATEGY_BALANCED, Layout.HYPHENATION_FREQUENCY_NORMAL);
248 state.resumeTiming();
249
250 StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH)
251 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL)
252 .setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED)
253 .build();
254 }
255 }
256
257 @Test
258 public void testCreate_PrecomputedText_Styled_Greedy_NoHyphenation() {
259 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
260 while (state.keepRunning()) {
261 state.pauseTiming();
262 final PrecomputedText text = makeMeasured(
263 mTextUtil.nextRandomParagraph(WORD_LENGTH, STYLE_TEXT), PAINT,
264 Layout.BREAK_STRATEGY_SIMPLE, Layout.HYPHENATION_FREQUENCY_NONE);
265 state.resumeTiming();
266
267 StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH)
268 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NONE)
269 .setBreakStrategy(Layout.BREAK_STRATEGY_SIMPLE)
270 .build();
271 }
272 }
273
274 @Test
275 public void testDraw_FixedText_NoStyled() {
276 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
277 final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT);
278 final RenderNode node = RenderNode.create("benchmark", null);
279 while (state.keepRunning()) {
280 state.pauseTiming();
281 final StaticLayout layout =
282 StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH).build();
283 final RecordingCanvas c = node.beginRecording(1200, 200);
284 state.resumeTiming();
285
286 layout.draw(c);
287 node.endRecording();
288 }
289 }
290
291 @Test
292 public void testDraw_RandomText_Styled() {
293 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
294 final RenderNode node = RenderNode.create("benchmark", null);
295 while (state.keepRunning()) {
296 state.pauseTiming();
297 final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, STYLE_TEXT);
298 final StaticLayout layout =
299 StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH).build();
300 final RecordingCanvas c = node.beginRecording(1200, 200);
301 state.resumeTiming();
302
303 layout.draw(c);
304 node.endRecording();
305 }
306 }
307
308 @Test
309 public void testDraw_RandomText_NoStyled() {
310 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
311 final RenderNode node = RenderNode.create("benchmark", null);
312 while (state.keepRunning()) {
313 state.pauseTiming();
314 final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT);
315 final StaticLayout layout =
316 StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH).build();
317 final RecordingCanvas c = node.beginRecording(1200, 200);
318 state.resumeTiming();
319
320 layout.draw(c);
321 node.endRecording();
322 }
323 }
324
325 @Test
326 public void testDraw_RandomText_Styled_WithoutCache() {
327 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
328 final RenderNode node = RenderNode.create("benchmark", null);
329 while (state.keepRunning()) {
330 state.pauseTiming();
331 final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, STYLE_TEXT);
332 final StaticLayout layout =
333 StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH).build();
334 final RecordingCanvas c = node.beginRecording(1200, 200);
335 Canvas.freeTextLayoutCaches();
336 state.resumeTiming();
337
338 layout.draw(c);
339 node.endRecording();
340 }
341 }
342
343 @Test
344 public void testDraw_RandomText_NoStyled_WithoutCache() {
345 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
346 final RenderNode node = RenderNode.create("benchmark", null);
347 while (state.keepRunning()) {
348 state.pauseTiming();
349 final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT);
350 final StaticLayout layout =
351 StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH).build();
352 final RecordingCanvas c = node.beginRecording(1200, 200);
353 Canvas.freeTextLayoutCaches();
354 state.resumeTiming();
355
356 layout.draw(c);
357 node.endRecording();
358 }
359 }
360
361 @Test
362 public void testDraw_PrecomputedText_Styled() {
363 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
364 final RenderNode node = RenderNode.create("benchmark", null);
365 while (state.keepRunning()) {
366 state.pauseTiming();
367 final PrecomputedText text = makeMeasured(
368 mTextUtil.nextRandomParagraph(WORD_LENGTH, STYLE_TEXT), PAINT);
369 final StaticLayout layout =
370 StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH).build();
371 final RecordingCanvas c = node.beginRecording(1200, 200);
372 state.resumeTiming();
373
374 layout.draw(c);
375 node.endRecording();
376 }
377 }
378
379 @Test
380 public void testDraw_PrecomputedText_NoStyled() {
381 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
382 final RenderNode node = RenderNode.create("benchmark", null);
383 while (state.keepRunning()) {
384 state.pauseTiming();
385 final PrecomputedText text = makeMeasured(
386 mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), PAINT);
387 final StaticLayout layout =
388 StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH).build();
389 final RecordingCanvas c = node.beginRecording(1200, 200);
390 state.resumeTiming();
391
392 layout.draw(c);
393 node.endRecording();
394 }
395 }
396
397 @Test
398 public void testDraw_PrecomputedText_Styled_WithoutCache() {
399 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
400 final RenderNode node = RenderNode.create("benchmark", null);
401 while (state.keepRunning()) {
402 state.pauseTiming();
403 final PrecomputedText text = makeMeasured(
404 mTextUtil.nextRandomParagraph(WORD_LENGTH, STYLE_TEXT), PAINT);
405 final StaticLayout layout =
406 StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH).build();
407 final RecordingCanvas c = node.beginRecording(1200, 200);
408 Canvas.freeTextLayoutCaches();
409 state.resumeTiming();
410
411 layout.draw(c);
412 node.endRecording();
413 }
414 }
415
416 @Test
417 public void testDraw_PrecomputedText_NoStyled_WithoutCache() {
418 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
419 final RenderNode node = RenderNode.create("benchmark", null);
420 while (state.keepRunning()) {
421 state.pauseTiming();
422 final PrecomputedText text = makeMeasured(
423 mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), PAINT);
424 final StaticLayout layout =
425 StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH).build();
426 final RecordingCanvas c = node.beginRecording(1200, 200);
427 Canvas.freeTextLayoutCaches();
428 state.resumeTiming();
429
430 layout.draw(c);
431 node.endRecording();
432 }
433 }
434
435 }