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   static {
30     int spaceInAscii = 32;
31     int sevenBitAsciiMax = 128;
32     StringBuilder sb = new StringBuilder(sevenBitAsciiMax - spaceInAscii);
33     for (int ch = spaceInAscii; ch < sevenBitAsciiMax; ch++) {
34       sb.append((char) ch);
35     }
36     ASCII_CHARACTERS = sb.toString();
37   }
38 
39   private static final String ALL_DIGITS;
40   static {
41     StringBuilder sb = new StringBuilder();
42     String zeros =
43         "0\u0660\u06f0\u07c0\u0966\u09e6\u0a66\u0ae6\u0b66\u0be6\u0c66"
44             + "\u0ce6\u0d66\u0e50\u0ed0\u0f20\u1040\u1090\u17e0\u1810\u1946"
45             + "\u19d0\u1b50\u1bb0\u1c40\u1c50\ua620\ua8d0\ua900\uaa50\uff10";
46     for (char base : zeros.toCharArray()) {
47       for (int offset = 0; offset < 10; offset++) {
48         sb.append((char) (base + offset));
49       }
50     }
51     ALL_DIGITS = sb.toString();
52   }
53 
54   /**
55    * Sample CharMatcher instances for benchmarking.
56    */
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(CharMatcher.inRange('0', '9')
66         .or(CharMatcher.inRange('A', 'F')).or(CharMatcher.inRange('a', 'f')),
67         "0123456789ABCDEFabcdef"),
68     GERMAN_59(CharMatcher.inRange('a', 'z')
69         .or(CharMatcher.inRange('A', 'Z')).or(CharMatcher.anyOf("äöüßÄÖÜ")),
70         "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZäöüßÄÖÜ");
71 
72     public final CharMatcher matcher;
73     public final String matchingChars;
74 
SampleMatcherConfig(String matchingChars)75     SampleMatcherConfig(String matchingChars) {
76       this(CharMatcher.anyOf(matchingChars), matchingChars);
77     }
78 
SampleMatcherConfig(CharMatcher matcher, String matchingChars)79     SampleMatcherConfig(CharMatcher matcher, String matchingChars) {
80       this.matcher = matcher;
81       this.matchingChars = matchingChars;
82     }
83   }
84 }
85