1 /*
2  * Copyright (C) 2022 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 
17 package android.libcore.regression;
18 
19 import android.perftests.utils.BenchmarkState;
20 import android.perftests.utils.PerfStatusReporter;
21 
22 import androidx.test.filters.LargeTest;
23 
24 import junitparams.JUnitParamsRunner;
25 import junitparams.Parameters;
26 
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.Locale;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
36 
37 @RunWith(JUnitParamsRunner.class)
38 @LargeTest
39 public final class SchemePrefixPerfTest {
40     @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
41 
42     enum Strategy {
JAVA()43         JAVA() {
44             @Override
45             String execute(String spec) {
46                 int colon = spec.indexOf(':');
47 
48                 if (colon < 1) {
49                     return null;
50                 }
51 
52                 for (int i = 0; i < colon; i++) {
53                     char c = spec.charAt(i);
54                     if (!isValidSchemeChar(i, c)) {
55                         return null;
56                     }
57                 }
58 
59                 return spec.substring(0, colon).toLowerCase(Locale.US);
60             }
61 
62             private boolean isValidSchemeChar(int index, char c) {
63                 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
64                     return true;
65                 }
66                 if (index > 0 && ((c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.')) {
67                     return true;
68                 }
69                 return false;
70             }
71         },
72 
REGEX()73         REGEX() {
74             private final Pattern mPattern = Pattern.compile("^([a-zA-Z][a-zA-Z0-9+\\-.]*):");
75 
76             @Override
77             String execute(String spec) {
78                 Matcher matcher = mPattern.matcher(spec);
79                 if (matcher.find()) {
80                     return matcher.group(1).toLowerCase(Locale.US);
81                 } else {
82                     return null;
83                 }
84             }
85         };
86 
execute(String spec)87         abstract String execute(String spec);
88     }
89 
getData()90     public static Collection<Object[]> getData() {
91         return Arrays.asList(new Object[][] {{Strategy.REGEX}, {Strategy.JAVA}});
92     }
93 
94     @Test
95     @Parameters(method = "getData")
timeSchemePrefix(Strategy strategy)96     public void timeSchemePrefix(Strategy strategy) {
97         BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
98         while (state.keepRunning()) {
99             strategy.execute("http://android.com");
100         }
101     }
102 }
103