1 /* 2 * Copyright (C) 2017 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 StringReplaceAllBenchmark { 22 // NOTE: These estimates of MOVEABLE / NON_MOVEABLE are based on a knowledge of 23 // ART implementation details. They make a difference here because JNI calls related 24 // to strings took different paths depending on whether the String in question was 25 // moveable or not. 26 enum StringLengths { 27 EMPTY(""), 28 MOVEABLE_16(makeString(16)), 29 MOVEABLE_256(makeString(256)), 30 MOVEABLE_1024(makeString(1024)), 31 NON_MOVEABLE(makeString(64 * 1024)), 32 BOOT_IMAGE(java.util.jar.JarFile.MANIFEST_NAME); 33 34 private final String value; 35 StringLengths(String s)36 StringLengths(String s) { 37 this.value = s; 38 } 39 } 40 makeString(int length)41 private static final String makeString(int length) { 42 final String sequence8 = "abcdefghijklmnop"; 43 final int numAppends = (length / 16) - 1; 44 StringBuilder stringBuilder = new StringBuilder(length); 45 46 // (n-1) occurences of "abcdefghijklmnop" 47 for (int i = 0; i < numAppends; ++i) { 48 stringBuilder.append(sequence8); 49 } 50 51 // and one final occurence of qrstuvwx. 52 stringBuilder.append("qrstuvwx"); 53 54 return stringBuilder.toString(); 55 } 56 57 @Param private StringLengths s; 58 timeReplaceAllTrivialPatternNonExistent(int reps)59 public void timeReplaceAllTrivialPatternNonExistent(int reps) { 60 for (int i = 0; i < reps; ++i) { 61 s.value.replaceAll("fish", "0"); 62 } 63 } 64 timeReplaceTrivialPatternAllRepeated(int reps)65 public void timeReplaceTrivialPatternAllRepeated(int reps) { 66 for (int i = 0; i < reps; ++i) { 67 s.value.replaceAll("jklm", "0"); 68 } 69 } 70 timeReplaceAllTrivialPatternSingleOccurence(int reps)71 public void timeReplaceAllTrivialPatternSingleOccurence(int reps) { 72 for (int i = 0; i < reps; ++i) { 73 s.value.replaceAll("qrst", "0"); 74 } 75 } 76 } 77