Tony Mak | c8ccab6 | 2018-12-12 18:46:38 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of 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, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Tony Mak | e5ee705 | 2019-04-01 20:51:37 +0100 | [diff] [blame] | 16 | package android.view.textclassifier; |
Tony Mak | c8ccab6 | 2018-12-12 18:46:38 +0800 | [diff] [blame] | 17 | |
| 18 | import android.content.Context; |
| 19 | import android.perftests.utils.BenchmarkState; |
| 20 | import android.perftests.utils.PerfStatusReporter; |
Tony Mak | c8ccab6 | 2018-12-12 18:46:38 +0800 | [diff] [blame] | 21 | |
KOUSHIK PANUGANTI | 412796e | 2018-12-17 14:16:52 -0800 | [diff] [blame] | 22 | import androidx.test.InstrumentationRegistry; |
| 23 | import androidx.test.filters.LargeTest; |
| 24 | |
Tony Mak | c8ccab6 | 2018-12-12 18:46:38 +0800 | [diff] [blame] | 25 | import org.junit.Before; |
| 26 | import org.junit.Rule; |
| 27 | import org.junit.Test; |
| 28 | import org.junit.runner.RunWith; |
| 29 | import org.junit.runners.Parameterized; |
| 30 | |
| 31 | import java.util.Arrays; |
| 32 | import java.util.Collection; |
| 33 | import java.util.Collections; |
| 34 | import java.util.Random; |
| 35 | |
| 36 | @RunWith(Parameterized.class) |
| 37 | @LargeTest |
| 38 | public class TextClassifierPerfTest { |
| 39 | /** Request contains meaning text, rather than garbled text. */ |
| 40 | private static final int ACTUAL_REQUEST = 0; |
| 41 | private static final String RANDOM_CHAR_SET = "abcdefghijklmnopqrstuvwxyz0123456789"; |
| 42 | |
| 43 | @Rule |
| 44 | public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); |
| 45 | |
gopinath | dcf274b | 2019-01-28 14:46:55 -0800 | [diff] [blame] | 46 | @Parameterized.Parameters(name = "size{0}") |
Tony Mak | c8ccab6 | 2018-12-12 18:46:38 +0800 | [diff] [blame] | 47 | public static Collection<Object[]> data() { |
| 48 | return Arrays.asList(new Object[][]{{ACTUAL_REQUEST}, {10}, {100}, {1000}}); |
| 49 | } |
| 50 | |
| 51 | private TextClassifier mTextClassifier; |
| 52 | private final int mSize; |
| 53 | |
| 54 | public TextClassifierPerfTest(int size) { |
| 55 | mSize = size; |
| 56 | } |
| 57 | |
| 58 | @Before |
| 59 | public void setUp() { |
| 60 | Context context = InstrumentationRegistry.getTargetContext(); |
| 61 | TextClassificationManager textClassificationManager = |
| 62 | context.getSystemService(TextClassificationManager.class); |
Abodunrinwa Toki | 324572e | 2019-02-12 19:01:01 +0000 | [diff] [blame] | 63 | mTextClassifier = textClassificationManager.getTextClassifier(TextClassifier.LOCAL); |
Tony Mak | c8ccab6 | 2018-12-12 18:46:38 +0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Test |
| 67 | public void testSuggestConversationActions() { |
| 68 | String text = mSize == ACTUAL_REQUEST ? "Where are you?" : generateRandomString(mSize); |
| 69 | ConversationActions.Request request = createConversationActionsRequest(text); |
| 70 | BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 71 | while (state.keepRunning()) { |
| 72 | mTextClassifier.suggestConversationActions(request); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | @Test |
| 77 | public void testDetectLanguage() { |
| 78 | String text = mSize == ACTUAL_REQUEST |
| 79 | ? "これは日本語のテキストです" : generateRandomString(mSize); |
| 80 | TextLanguage.Request request = createTextLanguageRequest(text); |
| 81 | BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 82 | while (state.keepRunning()) { |
| 83 | mTextClassifier.detectLanguage(request); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | private static ConversationActions.Request createConversationActionsRequest(CharSequence text) { |
| 88 | ConversationActions.Message message = |
| 89 | new ConversationActions.Message.Builder( |
Tony Mak | 91daa15 | 2019-01-24 16:00:28 +0000 | [diff] [blame] | 90 | ConversationActions.Message.PERSON_USER_OTHERS) |
Tony Mak | c8ccab6 | 2018-12-12 18:46:38 +0800 | [diff] [blame] | 91 | .setText(text) |
| 92 | .build(); |
| 93 | return new ConversationActions.Request.Builder(Collections.singletonList(message)) |
| 94 | .build(); |
| 95 | } |
| 96 | |
| 97 | private static TextLanguage.Request createTextLanguageRequest(CharSequence text) { |
| 98 | return new TextLanguage.Request.Builder(text).build(); |
| 99 | } |
| 100 | |
| 101 | private static String generateRandomString(int length) { |
| 102 | Random random = new Random(); |
| 103 | StringBuilder stringBuilder = new StringBuilder(length); |
| 104 | for (int i = 0; i < length; i++) { |
| 105 | int index = random.nextInt(RANDOM_CHAR_SET.length()); |
| 106 | stringBuilder.append(RANDOM_CHAR_SET.charAt(index)); |
| 107 | } |
| 108 | return stringBuilder.toString(); |
| 109 | } |
| 110 | } |