Home | History | Annotate | Download | only in text
      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 static android.text.TextDirectionHeuristics.LTR;
     20 
     21 import android.perftests.utils.BenchmarkState;
     22 import android.perftests.utils.PerfStatusReporter;
     23 
     24 import android.support.test.filters.LargeTest;
     25 import android.support.test.runner.AndroidJUnit4;
     26 
     27 import android.content.res.ColorStateList;
     28 import android.graphics.Canvas;
     29 import android.graphics.Typeface;
     30 import android.text.Layout;
     31 import android.text.style.TextAppearanceSpan;
     32 import android.view.DisplayListCanvas;
     33 import android.view.RenderNode;
     34 
     35 import org.junit.Before;
     36 import org.junit.Rule;
     37 import org.junit.Test;
     38 import org.junit.runner.RunWith;
     39 
     40 import java.nio.CharBuffer;
     41 import java.util.Random;
     42 
     43 @LargeTest
     44 @RunWith(AndroidJUnit4.class)
     45 public class PrecomputedTextPerfTest {
     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 PrecomputedTextPerfTest() {}
     55 
     56     @Rule
     57     public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
     58 
     59     private TextPerfUtils mTextUtil = new TextPerfUtils();
     60 
     61     @Before
     62     public void setUp() {
     63         mTextUtil.resetRandom(0 /* seed */);
     64     }
     65 
     66     @Test
     67     public void testCreate_NoStyled_Hyphenation() {
     68         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
     69         final PrecomputedText.Params param = new PrecomputedText.Params.Builder(PAINT)
     70                 .setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED)
     71                 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL)
     72                 .build();
     73 
     74         while (state.keepRunning()) {
     75             state.pauseTiming();
     76             final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT);
     77             state.resumeTiming();
     78 
     79             PrecomputedText.create(text, param);
     80         }
     81     }
     82 
     83     @Test
     84     public void testCreate_NoStyled_NoHyphenation() {
     85         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
     86         final PrecomputedText.Params param = new PrecomputedText.Params.Builder(PAINT)
     87                 .setBreakStrategy(Layout.BREAK_STRATEGY_SIMPLE)
     88                 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NONE)
     89                 .build();
     90 
     91         while (state.keepRunning()) {
     92             state.pauseTiming();
     93             final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT);
     94             state.resumeTiming();
     95 
     96             PrecomputedText.create(text, param);
     97         }
     98     }
     99 
    100     @Test
    101     public void testCreate_NoStyled_Hyphenation_WidthOnly() {
    102         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
    103         final PrecomputedText.Params param = new PrecomputedText.Params.Builder(PAINT)
    104                 .setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED)
    105                 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL)
    106                 .build();
    107 
    108         while (state.keepRunning()) {
    109             state.pauseTiming();
    110             final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT);
    111             state.resumeTiming();
    112 
    113             PrecomputedText.create(text, param);
    114         }
    115     }
    116 
    117     @Test
    118     public void testCreate_NoStyled_NoHyphenation_WidthOnly() {
    119         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
    120         final PrecomputedText.Params param = new PrecomputedText.Params.Builder(PAINT)
    121                 .setBreakStrategy(Layout.BREAK_STRATEGY_SIMPLE)
    122                 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NONE)
    123                 .build();
    124 
    125         while (state.keepRunning()) {
    126             state.pauseTiming();
    127             final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, NO_STYLE_TEXT);
    128             state.resumeTiming();
    129 
    130             PrecomputedText.create(text, param);
    131         }
    132     }
    133 
    134     @Test
    135     public void testCreate_Styled_Hyphenation() {
    136         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
    137         final PrecomputedText.Params param = new PrecomputedText.Params.Builder(PAINT)
    138                 .setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED)
    139                 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL)
    140                 .build();
    141 
    142         while (state.keepRunning()) {
    143             state.pauseTiming();
    144             final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, STYLE_TEXT);
    145             state.resumeTiming();
    146 
    147             PrecomputedText.create(text, param);
    148         }
    149     }
    150 
    151     @Test
    152     public void testCreate_Styled_NoHyphenation() {
    153         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
    154         final PrecomputedText.Params param = new PrecomputedText.Params.Builder(PAINT)
    155                 .setBreakStrategy(Layout.BREAK_STRATEGY_SIMPLE)
    156                 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NONE)
    157                 .build();
    158 
    159         while (state.keepRunning()) {
    160             state.pauseTiming();
    161             final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, STYLE_TEXT);
    162             state.resumeTiming();
    163 
    164             PrecomputedText.create(text, param);
    165         }
    166     }
    167 
    168     @Test
    169     public void testCreate_Styled_Hyphenation_WidthOnly() {
    170         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
    171         final PrecomputedText.Params param = new PrecomputedText.Params.Builder(PAINT)
    172                 .setBreakStrategy(Layout.BREAK_STRATEGY_BALANCED)
    173                 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NORMAL)
    174                 .build();
    175 
    176         while (state.keepRunning()) {
    177             state.pauseTiming();
    178             final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, STYLE_TEXT);
    179             state.resumeTiming();
    180 
    181             PrecomputedText.create(text, param);
    182         }
    183     }
    184 
    185     @Test
    186     public void testCreate_Styled_NoHyphenation_WidthOnly() {
    187         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
    188         final PrecomputedText.Params param = new PrecomputedText.Params.Builder(PAINT)
    189                 .setBreakStrategy(Layout.BREAK_STRATEGY_SIMPLE)
    190                 .setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NONE)
    191                 .build();
    192 
    193         while (state.keepRunning()) {
    194             state.pauseTiming();
    195             final CharSequence text = mTextUtil.nextRandomParagraph(WORD_LENGTH, STYLE_TEXT);
    196             state.resumeTiming();
    197 
    198             PrecomputedText.create(text, param);
    199         }
    200     }
    201 }
    202