2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.text.util;
19 import com.google.caliper.AfterExperiment;
20 import com.google.caliper.BeforeExperiment;
21 import com.google.caliper.Benchmark;
22 import com.google.caliper.Param;
24 import android.text.Spannable;
25 import android.text.SpannableString;
26 import android.util.Patterns;
28 import java.util.regex.Pattern;
30 public class LinkifyBenchmark {
31 private static final String MATCHING_STR = " http://user:pass@host.com:5432/path?k=v#f " +
32 "host.com:5432/path?k=v#f ";
34 private static final String NONMATCHING_STR = " Neque porro quisquam est qui dolorem ipsum " +
35 "quia dolor sit amet, consectetur, adipisci velit ";
37 // this pattern does not recognize strings without http scheme therefore is expected to be
38 // faster in MATCHING_STR case.
39 private static final Pattern BASIC_PATTERN = Pattern.compile(
40 "(?:\\b|$|^)http://[a-zA-Z0-9:\\.@\\?=#/]+(?:\\b|$|^)");
42 @Param({"1", "4", "16", "64", "256"})
43 private String mParamCopyAmount;
45 @Param({MATCHING_STR, NONMATCHING_STR})
46 private String mParamBasicText;
48 private Spannable mTestSpannable;
51 protected void setUp() throws Exception {
52 int copyAmount = Integer.valueOf(mParamCopyAmount);
53 StringBuilder strBuilder = new StringBuilder();
54 for (int i = 0; i < copyAmount; i++) {
55 strBuilder.append(mParamBasicText);
57 mTestSpannable = new SpannableString(strBuilder.toString());
61 protected void tearDown() {
62 mTestSpannable = null;
66 public void timeNewRegEx(int reps) throws Exception {
67 for (int i = 0; i < reps; i++) {
68 Linkify.addLinks(mTestSpannable, Patterns.AUTOLINK_WEB_URL, "http://",
69 new String[]{"http://", "https://", "rtsp://"}, null, null);
74 public void timeOldRegEx(int reps) throws Exception {
75 for (int i = 0; i < reps; i++) {
76 Linkify.addLinks(mTestSpannable, Patterns.WEB_URL, "http://",
77 new String[]{"http://", "https://", "rtsp://"}, null, null);
82 public void timeBasicRegEx(int reps) throws Exception {
83 for (int i = 0; i < reps; i++) {
84 Linkify.addLinks(mTestSpannable, BASIC_PATTERN, "http://",
85 new String[]{"http://", "https://", "rtsp://"}, null, null);