Siyamed Sinir | 545c3cf | 2016-03-30 16:01:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.text.util; |
| 18 | |
| 19 | import com.google.caliper.AfterExperiment; |
| 20 | import com.google.caliper.BeforeExperiment; |
| 21 | import com.google.caliper.Benchmark; |
| 22 | import com.google.caliper.Param; |
| 23 | |
| 24 | import android.text.Spannable; |
| 25 | import android.text.SpannableString; |
| 26 | import android.util.Patterns; |
| 27 | |
| 28 | import java.util.regex.Pattern; |
| 29 | |
| 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 "; |
| 33 | |
| 34 | private static final String NONMATCHING_STR = " Neque porro quisquam est qui dolorem ipsum " + |
| 35 | "quia dolor sit amet, consectetur, adipisci velit "; |
| 36 | |
| 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|$|^)"); |
| 41 | |
| 42 | @Param({"1", "4", "16", "64", "256"}) |
| 43 | private String mParamCopyAmount; |
| 44 | |
| 45 | @Param({MATCHING_STR, NONMATCHING_STR}) |
| 46 | private String mParamBasicText; |
| 47 | |
| 48 | private Spannable mTestSpannable; |
| 49 | |
| 50 | @BeforeExperiment |
| 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); |
| 56 | } |
| 57 | mTestSpannable = new SpannableString(strBuilder.toString()); |
| 58 | } |
| 59 | |
| 60 | @AfterExperiment |
| 61 | protected void tearDown() { |
| 62 | mTestSpannable = null; |
| 63 | } |
| 64 | |
| 65 | @Benchmark |
| 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); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | @Benchmark |
| 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); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | @Benchmark |
| 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); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | } |