Home | History | Annotate | Download | only in regression
      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 benchmarks.regression;
     18 
     19 import com.google.caliper.Param;
     20 
     21 public class StringReplaceBenchmark {
     22     static enum StringLengths {
     23         EMPTY(""),
     24         L_16(makeString(16)),
     25         L_64(makeString(64)),
     26         L_256(makeString(256)),
     27         L_512(makeString(512));
     28 
     29         private final String value;
     30 
     31         private StringLengths(String s) {
     32             this.value = s;
     33         }
     34     }
     35 
     36     private static final String makeString(int length) {
     37         final String sequence8 = "abcdefghijklmnop";
     38         final int numAppends = (length / 16) - 1;
     39         StringBuilder stringBuilder = new StringBuilder(length);
     40 
     41         // (n-1) occurences of "abcdefghijklmnop"
     42         for (int i = 0; i < numAppends; ++i) {
     43             stringBuilder.append(sequence8);
     44         }
     45 
     46         // and one final occurence of qrstuvwx.
     47         stringBuilder.append("qrstuvwx");
     48 
     49         return stringBuilder.toString();
     50     }
     51 
     52     @Param private StringLengths s;
     53 
     54     public void timeReplaceCharNonExistent(int reps) {
     55         for (int i = 0; i < reps; ++i) {
     56             s.value.replace('z', '0');
     57         }
     58     }
     59 
     60     public void timeReplaceCharRepeated(int reps) {
     61         for (int i = 0; i < reps; ++i) {
     62             s.value.replace('a', '0');
     63         }
     64     }
     65 
     66     public void timeReplaceSingleChar(int reps) {
     67         for (int i = 0; i < reps; ++i) {
     68             s.value.replace('q', '0');
     69         }
     70     }
     71 
     72     public void timeReplaceSequenceNonExistent(int reps) {
     73         for (int i = 0; i < reps; ++i) {
     74             s.value.replace("fish", "0");
     75         }
     76     }
     77 
     78     public void timeReplaceSequenceRepeated(int reps) {
     79         for (int i = 0; i < reps; ++i) {
     80             s.value.replace("jklm", "0");
     81         }
     82     }
     83 
     84     public void timeReplaceSingleSequence(int reps) {
     85         for (int i = 0; i < reps; ++i) {
     86             s.value.replace("qrst", "0");
     87         }
     88     }
     89 }
     90