View Javadoc
1   /*
2    * Copyright 2024 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.util;
17  
18  import org.openjdk.jmh.annotations.Benchmark;
19  import org.openjdk.jmh.annotations.Fork;
20  import org.openjdk.jmh.annotations.Level;
21  import org.openjdk.jmh.annotations.Measurement;
22  import org.openjdk.jmh.annotations.OutputTimeUnit;
23  import org.openjdk.jmh.annotations.Param;
24  import org.openjdk.jmh.annotations.Scope;
25  import org.openjdk.jmh.annotations.Setup;
26  import org.openjdk.jmh.annotations.State;
27  import org.openjdk.jmh.annotations.Threads;
28  import org.openjdk.jmh.annotations.Warmup;
29  
30  import java.util.SplittableRandom;
31  import java.util.concurrent.TimeUnit;
32  
33  @Threads(1)
34  @OutputTimeUnit(TimeUnit.MICROSECONDS)
35  @Fork(2)
36  @Warmup(iterations = 5, time = 1)
37  @Measurement(iterations = 8, time = 1)
38  @State(Scope.Benchmark)
39  public class AsciiStringCaseConversionBenchmark {
40      @Param({ "7", "16", "23", "32" })
41      int size;
42  
43      @Param({ "4", "11" })
44      int logPermutations;
45  
46      @Param({ "0" })
47      int seed;
48  
49      int permutations;
50  
51      AsciiString[] asciiStringData;
52  
53      String[] stringData;
54  
55      byte[] ret;
56  
57      private int i;
58  
59      @Param({ "true", "false" })
60      private boolean noUnsafe;
61  
62      @Setup(Level.Trial)
63      public void init() {
64          System.setProperty("io.netty.noUnsafe", Boolean.valueOf(noUnsafe).toString());
65          final SplittableRandom random = new SplittableRandom(seed);
66          permutations = 1 << logPermutations;
67          ret = new byte[size];
68          asciiStringData = new AsciiString[permutations];
69          stringData = new String[permutations];
70          for (int i = 0; i < permutations; ++i) {
71              final int foundIndex = random.nextInt(Math.max(0, size - 8), size);
72              final byte[] byteArray = new byte[size];
73              int j = 0;
74              for (; j < size; j++) {
75                  byte value = (byte) random.nextInt(0, (int) Byte.MAX_VALUE + 1);
76                  // turn any found value into something different
77                  if (j < foundIndex) {
78                      if (AsciiStringUtil.isUpperCase(value)) {
79                          value = AsciiStringUtil.toLowerCase(value);
80                      }
81                  }
82                  if (j == foundIndex) {
83                      value = 'N';
84                  }
85                  byteArray[j] = value;
86              }
87              asciiStringData[i] = new AsciiString(byteArray, false);
88              stringData[i] = asciiStringData[i].toString();
89          }
90      }
91  
92      private AsciiString getData() {
93          return asciiStringData[i++ & permutations - 1];
94      }
95  
96      private String getStringData() {
97          return stringData[i++ & permutations - 1];
98      }
99  
100     @Benchmark
101     public AsciiString toLowerCase() {
102         return getData().toLowerCase();
103     }
104 
105     @Benchmark
106     public AsciiString toUpperCase() {
107         return getData().toUpperCase();
108     }
109 
110     @Benchmark
111     public String stringToLowerCase() {
112         return getStringData().toLowerCase();
113     }
114 
115     @Benchmark
116     public String stringtoUpperCase() {
117         return getStringData().toUpperCase();
118     }
119 
120 }