Roozbeh Pournader | 737dfea | 2017-08-10 11:32:24 -0700 | [diff] [blame] | 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.perftests.utils.BenchmarkState; |
| 20 | import android.perftests.utils.PerfStatusReporter; |
| 21 | |
| 22 | import android.support.test.filters.LargeTest; |
| 23 | import android.support.test.runner.AndroidJUnit4; |
| 24 | |
| 25 | import org.junit.Rule; |
| 26 | import org.junit.Test; |
| 27 | import org.junit.runner.RunWith; |
| 28 | |
| 29 | import java.nio.CharBuffer; |
| 30 | import java.util.Random; |
| 31 | |
| 32 | @LargeTest |
| 33 | @RunWith(AndroidJUnit4.class) |
| 34 | public class StaticLayoutPerfTest { |
| 35 | |
| 36 | public StaticLayoutPerfTest() { |
| 37 | } |
| 38 | |
| 39 | @Rule |
| 40 | public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); |
| 41 | |
| 42 | private static final String FIXED_TEXT = "Lorem ipsum dolor sit amet, consectetur adipiscing " |
| 43 | + "elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad " |
| 44 | + "minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea " |
| 45 | + "commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse " |
| 46 | + "cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non " |
| 47 | + "proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; |
| 48 | private static final int FIXED_TEXT_LENGTH = FIXED_TEXT.length(); |
| 49 | |
| 50 | private static TextPaint PAINT = new TextPaint(); |
| 51 | private static final int TEXT_WIDTH = 20 * (int) PAINT.getTextSize(); |
| 52 | |
| 53 | @Test |
| 54 | public void testCreate() { |
| 55 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 56 | while (state.keepRunning()) { |
| 57 | StaticLayout.Builder.obtain(FIXED_TEXT, 0, FIXED_TEXT_LENGTH, PAINT, TEXT_WIDTH) |
| 58 | .build(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
| 63 | private static final int ALPHABET_LENGTH = ALPHABET.length(); |
| 64 | |
| 65 | private static final int PARA_LENGTH = 500; |
| 66 | private final char[] mBuffer = new char[PARA_LENGTH]; |
| 67 | private final Random mRandom = new Random(31415926535L); |
| 68 | |
| 69 | private CharSequence generateRandomParagraph(int wordLen) { |
| 70 | for (int i = 0; i < PARA_LENGTH; i++) { |
| 71 | if (i % (wordLen + 1) == wordLen) { |
| 72 | mBuffer[i] = ' '; |
| 73 | } else { |
| 74 | mBuffer[i] = ALPHABET.charAt(mRandom.nextInt(ALPHABET_LENGTH)); |
| 75 | } |
| 76 | } |
| 77 | return CharBuffer.wrap(mBuffer); |
| 78 | } |
| 79 | |
| 80 | // This tries to simulate the case where the cache hit rate is low, and most of the text is |
| 81 | // new text. |
| 82 | @Test |
| 83 | public void testCreateRandom() { |
| 84 | final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 85 | while (state.keepRunning()) { |
| 86 | final CharSequence text = generateRandomParagraph(9); |
| 87 | StaticLayout.Builder.obtain(text, 0, text.length(), PAINT, TEXT_WIDTH) |
| 88 | .build(); |
| 89 | } |
| 90 | } |
| 91 | } |