diff options
author | Justin Klaassen <justinklaassen@google.com> | 2018-04-03 23:21:57 -0400 |
---|---|---|
committer | Justin Klaassen <justinklaassen@google.com> | 2018-04-03 23:21:57 -0400 |
commit | 4d01eeaffaa720e4458a118baa137a11614f00f7 (patch) | |
tree | 66751893566986236788e3c796a7cc5e90d05f52 /android/text/StaticLayoutPerfTest.java | |
parent | a192cc2a132cb0ee8588e2df755563ec7008c179 (diff) | |
download | android-28-4d01eeaffaa720e4458a118baa137a11614f00f7.tar.gz |
Import Android SDK Platform P [4697573]
/google/data/ro/projects/android/fetch_artifact \
--bid 4697573 \
--target sdk_phone_armv7-win_sdk \
sdk-repo-linux-sources-4697573.zip
AndroidVersion.ApiLevel has been modified to appear as 28
Change-Id: If80578c3c657366cc9cf75f8db13d46e2dd4e077
Diffstat (limited to 'android/text/StaticLayoutPerfTest.java')
-rw-r--r-- | android/text/StaticLayoutPerfTest.java | 160 |
1 files changed, 59 insertions, 101 deletions
diff --git a/android/text/StaticLayoutPerfTest.java b/android/text/StaticLayoutPerfTest.java index 682885b3..e1a38a09 100644 --- a/android/text/StaticLayoutPerfTest.java +++ b/android/text/StaticLayoutPerfTest.java @@ -43,74 +43,42 @@ import java.util.Random; @LargeTest @RunWith(AndroidJUnit4.class) public class StaticLayoutPerfTest { - - public StaticLayoutPerfTest() {} - - @Rule - public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); - private static final int WORD_LENGTH = 9; // Random word has 9 characters. private static final int WORDS_IN_LINE = 8; // Roughly, 8 words in a line. - private static final int PARA_LENGTH = 500; // Number of characters in a paragraph. - private static final boolean NO_STYLE_TEXT = false; private static final boolean STYLE_TEXT = true; - private Random mRandom; - - private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - private static final int ALPHABET_LENGTH = ALPHABET.length(); - - private static final ColorStateList TEXT_COLOR = ColorStateList.valueOf(0x00000000); - private static final String[] FAMILIES = { "sans-serif", "serif", "monospace" }; - private static final int[] STYLES = { - Typeface.NORMAL, Typeface.BOLD, Typeface.ITALIC, Typeface.BOLD_ITALIC - }; - - private final char[] mBuffer = new char[PARA_LENGTH]; - private static TextPaint PAINT = new TextPaint(); private static final int TEXT_WIDTH = WORDS_IN_LINE * WORD_LENGTH * (int) PAINT.getTextSize(); - private CharSequence generateRandomParagraph(int wordLen, boolean applyRandomStyle) { - for (int i = 0; i < PARA_LENGTH; i++) { - if (i % (wordLen + 1) == wordLen) { - mBuffer[i] = ' '; - } else { - mBuffer[i] = ALPHABET.charAt(mRandom.nextInt(ALPHABET_LENGTH)); - } - } + public StaticLayoutPerfTest() {} - CharSequence cs = CharBuffer.wrap(mBuffer); - if (!applyRandomStyle) { - return cs; - } + @Rule + public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); - SpannableStringBuilder ssb = new SpannableStringBuilder(cs); - for (int i = 0; i < ssb.length(); i += WORD_LENGTH) { - final int spanStart = i; - final int spanEnd = (i + WORD_LENGTH) > ssb.length() ? ssb.length() : i + WORD_LENGTH; + private TextPerfUtils mTextUtil = new TextPerfUtils(); - final TextAppearanceSpan span = new TextAppearanceSpan( - FAMILIES[mRandom.nextInt(FAMILIES.length)], - STYLES[mRandom.nextInt(STYLES.length)], - 24 + mRandom.nextInt(32), // text size. min 24 max 56 - TEXT_COLOR, TEXT_COLOR); + @Before + public void setUp() { + mTextUtil.resetRandom(0 /* seed */); + } - ssb.setSpan(span, spanStart, spanEnd, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); - } - return ssb; + private PrecomputedText makeMeasured(CharSequence text, TextPaint paint) { + PrecomputedText.Params param = new PrecomputedText.Params.Builder(paint).build(); + return PrecomputedText.create(text, param); } - @Before - public void setUp() { - mRandom = new Random(0); + private PrecomputedText makeMeasured(CharSequence text, TextPaint paint, int strategy, + int frequency) { + PrecomputedText.Params param = new PrecomputedText.Params.Builder(paint) + .setHyphenationFrequency(frequency).setBreakStrategy(strategy).build(); + return PrecomputedText.create(text, param); } @Test public void testCreate_FixedText_NoStyle_Greedy_NoHyphenation() { final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); - final CharSequence text = generateRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT); + final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT); while (state.keepRunning()) { StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH) .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NONE) @@ -124,7 +92,7 @@ public class StaticLayoutPerfTest { final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); while (state.keepRunning()) { state.pauseTiming(); - final CharSequence text = generateRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT); + final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT); state.resumeTiming(); StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH) @@ -139,7 +107,7 @@ public class StaticLayoutPerfTest { final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); while (state.keepRunning()) { state.pauseTiming(); - final CharSequence text = generateRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT); + final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT); state.resumeTiming(); StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH) @@ -154,7 +122,7 @@ public class StaticLayoutPerfTest { final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); while (state.keepRunning()) { state.pauseTiming(); - final CharSequence text = generateRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT); + final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT); state.resumeTiming(); StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH) @@ -169,7 +137,7 @@ public class StaticLayoutPerfTest { final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); while (state.keepRunning()) { state.pauseTiming(); - final CharSequence text = generateRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT); + final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT); state.resumeTiming(); StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH) @@ -184,7 +152,7 @@ public class StaticLayoutPerfTest { final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); while (state.keepRunning()) { state.pauseTiming(); - final CharSequence text = generateRandomParagraph(WORD_LENGTH, STYLE_TEXT); + final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, STYLE_TEXT); state.resumeTiming(); StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH) @@ -195,15 +163,13 @@ public class StaticLayoutPerfTest { } @Test - public void testCreate_MeasuredText_NoStyled_Greedy_NoHyphenation() { + public void testCreate_PrecomputedText_NoStyled_Greedy_NoHyphenation() { final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); while (state.keepRunning()) { state.pauseTiming(); - final MeasuredText text = new MeasuredText.Builder( - generateRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), PAINT) - .setBreakStrategy(Layout.BREAK_STRATEGY_SIMPLE) - .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NONE) - .build(); + final PrecomputedText text = makeMeasured( + mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), PAINT, + Layout.BREAK_STRATEGY_SIMPLE, Layout.HYPHENATION_FREQUENCY_NONE); state.resumeTiming(); StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH) @@ -214,15 +180,13 @@ public class StaticLayoutPerfTest { } @Test - public void testCreate_MeasuredText_NoStyled_Greedy_Hyphenation() { + public void testCreate_PrecomputedText_NoStyled_Greedy_Hyphenation() { final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); while (state.keepRunning()) { state.pauseTiming(); - final MeasuredText text = new MeasuredText.Builder( - generateRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), PAINT) - .setBreakStrategy(Layout.BREAK_STRATEGY_SIMPLE) - .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL) - .build(); + final PrecomputedText text = makeMeasured( + mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), PAINT, + Layout.BREAK_STRATEGY_SIMPLE, Layout.HYPHENATION_FREQUENCY_NORMAL); state.resumeTiming(); StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH) @@ -233,15 +197,13 @@ public class StaticLayoutPerfTest { } @Test - public void testCreate_MeasuredText_NoStyled_Balanced_NoHyphenation() { + public void testCreate_PrecomputedText_NoStyled_Balanced_NoHyphenation() { final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); while (state.keepRunning()) { state.pauseTiming(); - final MeasuredText text = new MeasuredText.Builder( - generateRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), PAINT) - .setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED) - .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NONE) - .build(); + final PrecomputedText text = makeMeasured( + mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), PAINT, + Layout.BREAK_STRATEGY_BALANCED, Layout.HYPHENATION_FREQUENCY_NONE); state.resumeTiming(); StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH) @@ -252,15 +214,13 @@ public class StaticLayoutPerfTest { } @Test - public void testCreate_MeasuredText_NoStyled_Balanced_Hyphenation() { + public void testCreate_PrecomputedText_NoStyled_Balanced_Hyphenation() { final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); while (state.keepRunning()) { state.pauseTiming(); - final MeasuredText text = new MeasuredText.Builder( - generateRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), PAINT) - .setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED) - .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL) - .build(); + final PrecomputedText text = makeMeasured( + mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), PAINT, + Layout.BREAK_STRATEGY_BALANCED, Layout.HYPHENATION_FREQUENCY_NORMAL); state.resumeTiming(); StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH) @@ -271,15 +231,13 @@ public class StaticLayoutPerfTest { } @Test - public void testCreate_MeasuredText_Styled_Greedy_NoHyphenation() { + public void testCreate_PrecomputedText_Styled_Greedy_NoHyphenation() { final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); while (state.keepRunning()) { state.pauseTiming(); - final MeasuredText text = new MeasuredText.Builder( - generateRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), PAINT) - .setBreakStrategy(Layout.BREAK_STRATEGY_SIMPLE) - .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NONE) - .build(); + final PrecomputedText text = makeMeasured( + mTextUtil.nextRandomParagraph(WORD_LENGTH, STYLE_TEXT), PAINT, + Layout.BREAK_STRATEGY_SIMPLE, Layout.HYPHENATION_FREQUENCY_NONE); state.resumeTiming(); StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH) @@ -292,7 +250,7 @@ public class StaticLayoutPerfTest { @Test public void testDraw_FixedText_NoStyled() { final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); - final CharSequence text = generateRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT); + final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT); final RenderNode node = RenderNode.create("benchmark", null); while (state.keepRunning()) { state.pauseTiming(); @@ -311,7 +269,7 @@ public class StaticLayoutPerfTest { final RenderNode node = RenderNode.create("benchmark", null); while (state.keepRunning()) { state.pauseTiming(); - final CharSequence text = generateRandomParagraph(WORD_LENGTH, STYLE_TEXT); + final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, STYLE_TEXT); final StaticLayout layout = StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH).build(); final DisplayListCanvas c = node.start(1200, 200); @@ -327,7 +285,7 @@ public class StaticLayoutPerfTest { final RenderNode node = RenderNode.create("benchmark", null); while (state.keepRunning()) { state.pauseTiming(); - final CharSequence text = generateRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT); + final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT); final StaticLayout layout = StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH).build(); final DisplayListCanvas c = node.start(1200, 200); @@ -343,7 +301,7 @@ public class StaticLayoutPerfTest { final RenderNode node = RenderNode.create("benchmark", null); while (state.keepRunning()) { state.pauseTiming(); - final CharSequence text = generateRandomParagraph(WORD_LENGTH, STYLE_TEXT); + final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, STYLE_TEXT); final StaticLayout layout = StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH).build(); final DisplayListCanvas c = node.start(1200, 200); @@ -360,7 +318,7 @@ public class StaticLayoutPerfTest { final RenderNode node = RenderNode.create("benchmark", null); while (state.keepRunning()) { state.pauseTiming(); - final CharSequence text = generateRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT); + final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT); final StaticLayout layout = StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH).build(); final DisplayListCanvas c = node.start(1200, 200); @@ -372,13 +330,13 @@ public class StaticLayoutPerfTest { } @Test - public void testDraw_MeasuredText_Styled() { + public void testDraw_PrecomputedText_Styled() { final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); final RenderNode node = RenderNode.create("benchmark", null); while (state.keepRunning()) { state.pauseTiming(); - final MeasuredText text = new MeasuredText.Builder( - generateRandomParagraph(WORD_LENGTH, STYLE_TEXT), PAINT).build(); + final PrecomputedText text = makeMeasured( + mTextUtil.nextRandomParagraph(WORD_LENGTH, STYLE_TEXT), PAINT); final StaticLayout layout = StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH).build(); final DisplayListCanvas c = node.start(1200, 200); @@ -389,13 +347,13 @@ public class StaticLayoutPerfTest { } @Test - public void testDraw_MeasuredText_NoStyled() { + public void testDraw_PrecomputedText_NoStyled() { final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); final RenderNode node = RenderNode.create("benchmark", null); while (state.keepRunning()) { state.pauseTiming(); - final MeasuredText text = new MeasuredText.Builder( - generateRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), PAINT).build(); + final PrecomputedText text = makeMeasured( + mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), PAINT); final StaticLayout layout = StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH).build(); final DisplayListCanvas c = node.start(1200, 200); @@ -406,13 +364,13 @@ public class StaticLayoutPerfTest { } @Test - public void testDraw_MeasuredText_Styled_WithoutCache() { + public void testDraw_PrecomputedText_Styled_WithoutCache() { final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); final RenderNode node = RenderNode.create("benchmark", null); while (state.keepRunning()) { state.pauseTiming(); - final MeasuredText text = new MeasuredText.Builder( - generateRandomParagraph(WORD_LENGTH, STYLE_TEXT), PAINT).build(); + final PrecomputedText text = makeMeasured( + mTextUtil.nextRandomParagraph(WORD_LENGTH, STYLE_TEXT), PAINT); final StaticLayout layout = StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH).build(); final DisplayListCanvas c = node.start(1200, 200); @@ -424,13 +382,13 @@ public class StaticLayoutPerfTest { } @Test - public void testDraw_MeasuredText_NoStyled_WithoutCache() { + public void testDraw_PrecomputedText_NoStyled_WithoutCache() { final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); final RenderNode node = RenderNode.create("benchmark", null); while (state.keepRunning()) { state.pauseTiming(); - final MeasuredText text = new MeasuredText.Builder( - generateRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), PAINT).build(); + final PrecomputedText text = makeMeasured( + mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT), PAINT); final StaticLayout layout = StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH).build(); final DisplayListCanvas c = node.start(1200, 200); |