• Home
  • 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 import android.service.textclassifier.TextClassifierService;
22 
23 import androidx.test.InstrumentationRegistry;
24 import androidx.test.filters.LargeTest;
25 
26 import org.junit.Before;
27 import org.junit.Rule;
28 import org.junit.Test;
29 
30 import java.util.Collections;
31 
32 @LargeTest
33 public class TextClassifierPerfTest {
34     private static final String TEXT = " Oh hi Mark, the number is (323) 654-6192.\n"
35             + "Anyway, I'll meet you at 1600 Pennsylvania Avenue NW.\n"
36             + "My flight is LX 38 and I'll arrive at 8:00pm.\n"
37             + "Also, check out www.google.com.\n";
38 
39     @Rule
40     public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
41 
42     private TextClassifier mTextClassifier;
43 
44     @Before
setUp()45     public void setUp() {
46         Context context = InstrumentationRegistry.getTargetContext();
47         mTextClassifier = TextClassifierService.getDefaultTextClassifierImplementation(context);
48     }
49 
50     @Test
testClassifyText()51     public void testClassifyText() {
52         TextClassification.Request request =
53                 new TextClassification.Request.Builder(TEXT, 0, TEXT.length()).build();
54         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
55         while (state.keepRunning()) {
56             mTextClassifier.classifyText(request);
57         }
58     }
59 
60     @Test
testSuggestSelection()61     public void testSuggestSelection() {
62         // Trying to select the phone number.
63         TextSelection.Request request =
64                 new TextSelection.Request.Builder(
65                         TEXT,
66                         /* startIndex= */ 28,
67                         /* endIndex= */29)
68                         .build();
69         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
70         while (state.keepRunning()) {
71             mTextClassifier.suggestSelection(request);
72         }
73     }
74 
75     @Test
testGenerateLinks()76     public void testGenerateLinks() {
77         TextLinks.Request request =
78                 new TextLinks.Request.Builder(TEXT).build();
79         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
80         while (state.keepRunning()) {
81             mTextClassifier.generateLinks(request);
82         }
83     }
84 
85     @Test
testSuggestConversationActions()86     public void testSuggestConversationActions() {
87         ConversationActions.Message message =
88                 new ConversationActions.Message.Builder(
89                         ConversationActions.Message.PERSON_USER_OTHERS)
90                         .setText(TEXT)
91                         .build();
92         ConversationActions.Request request = new ConversationActions.Request.Builder(
93                 Collections.singletonList(message))
94                 .build();
95 
96         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
97         while (state.keepRunning()) {
98             mTextClassifier.suggestConversationActions(request);
99         }
100     }
101 
102     @Test
testDetectLanguage()103     public void testDetectLanguage() {
104         TextLanguage.Request request = new TextLanguage.Request.Builder(TEXT).build();
105         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
106         while (state.keepRunning()) {
107             mTextClassifier.detectLanguage(request);
108         }
109     }
110 }
111