| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.text; |
| 17 |
|
| 18 |
import static android.view.View.MeasureSpec.AT_MOST; |
| 19 |
import static android.view.View.MeasureSpec.UNSPECIFIED; |
| 20 |
|
| 21 |
import android.graphics.Canvas; |
| 22 |
import android.graphics.RecordingCanvas; |
| 23 |
import android.graphics.RenderNode; |
| 24 |
import android.perftests.utils.BenchmarkState; |
| 25 |
import android.perftests.utils.PerfStatusReporter; |
| 26 |
import android.text.NonEditableTextGenerator.TextType; |
| 27 |
import android.widget.TextView; |
| 28 |
|
| 29 |
import androidx.test.InstrumentationRegistry; |
| 30 |
import androidx.test.filters.LargeTest; |
| 31 |
|
| 32 |
import org.junit.Rule; |
| 33 |
import org.junit.Test; |
| 34 |
import org.junit.runner.RunWith; |
| 35 |
import org.junit.runners.Parameterized; |
| 36 |
|
| 37 |
import java.util.ArrayList; |
| 38 |
import java.util.Collection; |
| 39 |
import java.util.List; |
| 40 |
import java.util.Locale; |
| 41 |
import java.util.Random; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
@LargeTest |
| 47 |
@RunWith(Parameterized.class) |
| 48 |
public class TextViewSetTextMeasurePerfTest { |
| 49 |
|
| 50 |
private static final boolean[] BOOLEANS = new boolean[]{false, true}; |
| 51 |
|
| 52 |
@Rule |
| 53 |
public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); |
| 54 |
|
| 55 |
@Parameterized.Parameters(name = "cached {3} {1}chars {0}") |
| 56 |
public static Collection cases() { |
| 57 |
final List<Object[]> params = new ArrayList<>(); |
| 58 |
for (int length : new int[]{128}) { |
| 59 |
for (boolean cached : BOOLEANS) { |
| 60 |
for (TextType textType : new TextType[]{TextType.STRING, |
| 61 |
TextType.SPANNABLE_BUILDER}) { |
| 62 |
params.add(new Object[]{textType.name(), length, textType, cached}); |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
return params; |
| 67 |
} |
| 68 |
|
| 69 |
private final int mLineWidth; |
| 70 |
private final int mLength; |
| 71 |
private final TextType mTextType; |
| 72 |
private final boolean mCached; |
| 73 |
private final TextPaint mTextPaint; |
| 74 |
|
| 75 |
public TextViewSetTextMeasurePerfTest(String label, int length, TextType textType, |
| 76 |
boolean cached) { |
| 77 |
mLength = length; |
| 78 |
mTextType = textType; |
| 79 |
mCached = cached; |
| 80 |
mTextPaint = new TextPaint(); |
| 81 |
mTextPaint.setTextSize(10); |
| 82 |
mLineWidth = Integer.MAX_VALUE; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
@Test |
| 89 |
public void timeCreate() throws Exception { |
| 90 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 91 |
|
| 92 |
state.pauseTiming(); |
| 93 |
Canvas.freeTextLayoutCaches(); |
| 94 |
final CharSequence text = createRandomText(mLength); |
| 95 |
final TextView textView = new TextView(InstrumentationRegistry.getTargetContext()); |
| 96 |
textView.setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NONE); |
| 97 |
|
| 98 |
textView.setText(text); |
| 99 |
state.resumeTiming(); |
| 100 |
|
| 101 |
while (state.keepRunning()) { |
| 102 |
state.pauseTiming(); |
| 103 |
textView.setTextLocale(Locale.UK); |
| 104 |
textView.setTextLocale(Locale.US); |
| 105 |
if (!mCached) Canvas.freeTextLayoutCaches(); |
| 106 |
state.resumeTiming(); |
| 107 |
|
| 108 |
textView.setText(text); |
| 109 |
textView.measure(AT_MOST | mLineWidth, UNSPECIFIED); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
@Test |
| 117 |
public void timeDraw() throws Exception { |
| 118 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 119 |
|
| 120 |
state.pauseTiming(); |
| 121 |
Canvas.freeTextLayoutCaches(); |
| 122 |
final RenderNode node = RenderNode.create("benchmark", null); |
| 123 |
final CharSequence text = createRandomText(mLength); |
| 124 |
final TextView textView = new TextView(InstrumentationRegistry.getTargetContext()); |
| 125 |
textView.setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_NONE); |
| 126 |
textView.setText(text); |
| 127 |
state.resumeTiming(); |
| 128 |
|
| 129 |
while (state.keepRunning()) { |
| 130 |
|
| 131 |
state.pauseTiming(); |
| 132 |
final RecordingCanvas canvas = node.start(1200, 200); |
| 133 |
int save = canvas.save(); |
| 134 |
textView.setTextLocale(Locale.UK); |
| 135 |
textView.setTextLocale(Locale.US); |
| 136 |
if (!mCached) Canvas.freeTextLayoutCaches(); |
| 137 |
state.resumeTiming(); |
| 138 |
|
| 139 |
textView.draw(canvas); |
| 140 |
|
| 141 |
state.pauseTiming(); |
| 142 |
canvas.restoreToCount(save); |
| 143 |
node.end(canvas); |
| 144 |
state.resumeTiming(); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
private CharSequence createRandomText(int length) { |
| 149 |
return new NonEditableTextGenerator(new Random(0)) |
| 150 |
.setSequenceLength(length) |
| 151 |
.setCreateBoring(false) |
| 152 |
.setTextType(mTextType) |
| 153 |
.build(); |
| 154 |
} |
| 155 |
} |