blob: 14a121d60c2e585ef6f4c39796e5e0d283e25a9d [file] [log] [blame]
Tony Makc8ccab62018-12-12 18:46:38 +08001/*
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 Make5ee7052019-04-01 20:51:37 +010016package android.view.textclassifier;
Tony Makc8ccab62018-12-12 18:46:38 +080017
18import android.content.Context;
19import android.perftests.utils.BenchmarkState;
20import android.perftests.utils.PerfStatusReporter;
Tony Makc8ccab62018-12-12 18:46:38 +080021
KOUSHIK PANUGANTI412796e2018-12-17 14:16:52 -080022import androidx.test.InstrumentationRegistry;
23import androidx.test.filters.LargeTest;
24
Tony Makc8ccab62018-12-12 18:46:38 +080025import org.junit.Before;
26import org.junit.Rule;
27import org.junit.Test;
28import org.junit.runner.RunWith;
29import org.junit.runners.Parameterized;
30
31import java.util.Arrays;
32import java.util.Collection;
33import java.util.Collections;
34import java.util.Random;
35
36@RunWith(Parameterized.class)
37@LargeTest
38public 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
gopinathdcf274b2019-01-28 14:46:55 -080046 @Parameterized.Parameters(name = "size{0}")
Tony Makc8ccab62018-12-12 18:46:38 +080047 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 Toki324572e2019-02-12 19:01:01 +000063 mTextClassifier = textClassificationManager.getTextClassifier(TextClassifier.LOCAL);
Tony Makc8ccab62018-12-12 18:46:38 +080064 }
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 Mak91daa152019-01-24 16:00:28 +000090 ConversationActions.Message.PERSON_USER_OTHERS)
Tony Makc8ccab62018-12-12 18:46:38 +080091 .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}