• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
16 package android.view.textclassifier;
17 
18 import android.content.Context;
19 import android.perftests.utils.BenchmarkState;
20 import android.perftests.utils.PerfStatusReporter;
21 
22 import androidx.test.InstrumentationRegistry;
23 import androidx.test.filters.LargeTest;
24 
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 
46     @Parameterized.Parameters(name = "size{0}")
data()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 
TextClassifierPerfTest(int size)54     public TextClassifierPerfTest(int size) {
55         mSize = size;
56     }
57 
58     @Before
setUp()59     public void setUp() {
60         Context context = InstrumentationRegistry.getTargetContext();
61         TextClassificationManager textClassificationManager =
62                 context.getSystemService(TextClassificationManager.class);
63         mTextClassifier = textClassificationManager.getTextClassifier(TextClassifier.LOCAL);
64     }
65 
66     @Test
testSuggestConversationActions()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
testDetectLanguage()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 
createConversationActionsRequest(CharSequence text)87     private static ConversationActions.Request createConversationActionsRequest(CharSequence text) {
88         ConversationActions.Message message =
89                 new ConversationActions.Message.Builder(
90                         ConversationActions.Message.PERSON_USER_OTHERS)
91                         .setText(text)
92                         .build();
93         return new ConversationActions.Request.Builder(Collections.singletonList(message))
94                 .build();
95     }
96 
createTextLanguageRequest(CharSequence text)97     private static TextLanguage.Request createTextLanguageRequest(CharSequence text) {
98         return new TextLanguage.Request.Builder(text).build();
99     }
100 
generateRandomString(int length)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 }
111