Debian Codegraph @

apct-tests/perftests/core/src/android/text/StaticLayoutMultithreadPerfTest.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"); 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.perftests.utils.BenchmarkState;
20 import android.perftests.utils.PerfStatusReporter;
21
22 import androidx.test.filters.LargeTest;
23 import androidx.test.runner.AndroidJUnit4;
24
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28
29 import java.util.concurrent.CountDownLatch;
30 import java.util.concurrent.TimeUnit;
31 import java.util.concurrent.atomic.AtomicBoolean;
32
33 @LargeTest
34 @RunWith(AndroidJUnit4.class)
35 public class StaticLayoutMultithreadPerfTest {
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
40 private static TextPaint PAINT = new TextPaint();
41 private static final int TEXT_WIDTH = WORDS_IN_LINE * WORD_LENGTH * (int) PAINT.getTextSize();
42
43 public StaticLayoutMultithreadPerfTest() {}
44
45 @Rule
46 public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
47
48 private CountDownLatch mStartLatch;
49 private AtomicBoolean mThreadState; // True for running, False for stopped.
50
51 private static final long TIMEOUT_MS = 5000;
52
53 private Thread[] startBackgroundThread(int numOfThreads) {
54 mStartLatch = new CountDownLatch(numOfThreads);
55 mThreadState = new AtomicBoolean(true);
56
57 Thread[] threads = new Thread[numOfThreads];
58 for (int i = 0; i < numOfThreads; ++i) {
59 final int seed = i + 1;
60 threads[i] = new Thread(new Runnable() {
61 @Override
62 public void run() {
63 final TextPerfUtils util = new TextPerfUtils();
64 util.resetRandom(seed);
65
66 mStartLatch.countDown();
67 while (mThreadState.get()) {
68 final CharSequence text = util.nextRandomParagraph(
69 WORD_LENGTH, NO_STYLE_TEXT);
70 StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH)
71 .build();
72 }
73 }
74 });
75 }
76
77 for (int i = 0; i < numOfThreads; ++i) {
78 threads[i].start();
79 }
80
81 try {
82 mStartLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS);
83 } catch (InterruptedException e) {
84 throw new RuntimeException(e);
85 }
86 return threads;
87 }
88
89 private void finishThreads(Thread[] threads) {
90 mThreadState.set(false);
91 for (Thread thread : threads) {
92 try {
93 thread.join();
94 } catch (InterruptedException e) {
95 throw new RuntimeException(e);
96 }
97 }
98 mStartLatch = null;
99 mThreadState = null;
100 }
101
102 private void runRandomTest(int numOfTotalThreads) {
103 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
104 final TextPerfUtils util = new TextPerfUtils();
105 Thread[] threads = startBackgroundThread(numOfTotalThreads - 1);
106 while (state.keepRunning()) {
107 state.pauseTiming();
108 final CharSequence text = util.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT);
109 state.resumeTiming();
110
111 StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH)
112 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NONE)
113 .setBreakStrategy(Layout.BREAK_STRATEGY_SIMPLE)
114 .build();
115 }
116 finishThreads(threads);
117 }
118
119 @Test
120 public void testCreate_RandomText_Thread_1() {
121 runRandomTest(1);
122 }
123
124 @Test
125 public void testCreate_RandomText_Thread_2() {
126 runRandomTest(2);
127 }
128
129 @Test
130 public void testCreate_RandomText_Thread_4() {
131 runRandomTest(4);
132 }
133 }