1 /* 2 * Copyright (C) 2012 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.common.base; 16 17 /** 18 * Common benchmarking utilities. 19 * 20 * @author Christopher Swenson 21 * @author Louis Wasserman 22 */ 23 class BenchmarkHelpers { 24 private static final String WHITESPACE_CHARACTERS = 25 "\u00a0\u180e\u202f\t\n\013\f\r \u0085" 26 + "\u1680\u2028\u2029\u205f\u3000\u2000\u2001\u2002\u2003\u2004\u2005" 27 + "\u2006\u2007\u2008\u2009\u200a"; 28 private static final String ASCII_CHARACTERS; 29 30 static { 31 int spaceInAscii = 32; 32 int sevenBitAsciiMax = 128; 33 StringBuilder sb = new StringBuilder(sevenBitAsciiMax - spaceInAscii); 34 for (int ch = spaceInAscii; ch < sevenBitAsciiMax; ch++) { 35 sb.append((char) ch); 36 } 37 ASCII_CHARACTERS = sb.toString(); 38 } 39 40 private static final String ALL_DIGITS; 41 42 static { 43 StringBuilder sb = new StringBuilder(); 44 String zeros = 45 "0\u0660\u06f0\u07c0\u0966\u09e6\u0a66\u0ae6\u0b66\u0be6\u0c66" 46 + "\u0ce6\u0d66\u0e50\u0ed0\u0f20\u1040\u1090\u17e0\u1810\u1946" 47 + "\u19d0\u1b50\u1bb0\u1c40\u1c50\ua620\ua8d0\ua900\uaa50\uff10"; 48 for (char base : zeros.toCharArray()) { 49 for (int offset = 0; offset < 10; offset++) { 50 sb.append((char) (base + offset)); 51 } 52 } 53 ALL_DIGITS = sb.toString(); 54 } 55 56 /** Sample CharMatcher instances for benchmarking. */ 57 public enum SampleMatcherConfig { 58 WHITESPACE(CharMatcher.whitespace(), WHITESPACE_CHARACTERS), 59 HASH(CharMatcher.is('#'), "#"), 60 ASCII(CharMatcher.ascii(), ASCII_CHARACTERS), 61 WESTERN_DIGIT("0123456789"), 62 ALL_DIGIT(CharMatcher.digit(), ALL_DIGITS), 63 OPS_5("+-*/%"), 64 HEX_16(CharMatcher.inRange('0', '9').or(CharMatcher.inRange('A', 'F')), "0123456789ABCDEF"), 65 HEX_22( 66 CharMatcher.inRange('0', '9') 67 .or(CharMatcher.inRange('A', 'F')) 68 .or(CharMatcher.inRange('a', 'f')), 69 "0123456789ABCDEFabcdef"), 70 GERMAN_59( 71 CharMatcher.inRange('a', 'z') 72 .or(CharMatcher.inRange('A', 'Z')) 73 .or(CharMatcher.anyOf("äöüßÄÖÜ")), 74 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZäöüßÄÖÜ"); 75 76 public final CharMatcher matcher; 77 public final String matchingChars; 78 SampleMatcherConfig(String matchingChars)79 SampleMatcherConfig(String matchingChars) { 80 this(CharMatcher.anyOf(matchingChars), matchingChars); 81 } 82 SampleMatcherConfig(CharMatcher matcher, String matchingChars)83 SampleMatcherConfig(CharMatcher matcher, String matchingChars) { 84 this.matcher = matcher; 85 this.matchingChars = matchingChars; 86 } 87 } 88 } 89