Debian Codegraph @

src/test/java/io/airlift/slice/SliceUtf8Benchmark.java

raw
1 /*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14 package io.airlift.slice;
15
16 import org.openjdk.jmh.annotations.Benchmark;
17 import org.openjdk.jmh.annotations.BenchmarkMode;
18 import org.openjdk.jmh.annotations.Fork;
19 import org.openjdk.jmh.annotations.Measurement;
20 import org.openjdk.jmh.annotations.OutputTimeUnit;
21 import org.openjdk.jmh.annotations.Param;
22 import org.openjdk.jmh.annotations.Setup;
23 import org.openjdk.jmh.annotations.State;
24 import org.openjdk.jmh.annotations.Warmup;
25 import org.openjdk.jmh.runner.Runner;
26 import org.openjdk.jmh.runner.RunnerException;
27 import org.openjdk.jmh.runner.options.Options;
28 import org.openjdk.jmh.runner.options.OptionsBuilder;
29 import org.openjdk.jmh.runner.options.VerboseMode;
30
31 import java.nio.charset.StandardCharsets;
32 import java.util.concurrent.ThreadLocalRandom;
33 import java.util.stream.IntStream;
34
35 import static io.airlift.slice.SliceUtf8.countCodePoints;
36 import static io.airlift.slice.SliceUtf8.leftTrim;
37 import static io.airlift.slice.SliceUtf8.lengthOfCodePoint;
38 import static io.airlift.slice.SliceUtf8.lengthOfCodePointFromStartByte;
39 import static io.airlift.slice.SliceUtf8.offsetOfCodePoint;
40 import static io.airlift.slice.SliceUtf8.reverse;
41 import static io.airlift.slice.SliceUtf8.rightTrim;
42 import static io.airlift.slice.SliceUtf8.substring;
43 import static io.airlift.slice.SliceUtf8.toLowerCase;
44 import static io.airlift.slice.SliceUtf8.toUpperCase;
45 import static io.airlift.slice.SliceUtf8.trim;
46 import static io.airlift.slice.Slices.utf8Slice;
47 import static java.lang.Character.MAX_CODE_POINT;
48 import static java.lang.Character.SURROGATE;
49 import static java.lang.Character.getType;
50 import static java.util.concurrent.TimeUnit.MILLISECONDS;
51 import static java.util.concurrent.TimeUnit.NANOSECONDS;
52 import static org.openjdk.jmh.annotations.Mode.AverageTime;
53 import static org.openjdk.jmh.annotations.Scope.Thread;
54
55 @SuppressWarnings("MethodMayBeStatic")
56 @State(Thread)
57 @OutputTimeUnit(NANOSECONDS)
58 @BenchmarkMode(AverageTime)
59 @Fork(1)
60 @Warmup(iterations = 4, time = 500, timeUnit = MILLISECONDS)
61 @Measurement(iterations = 5, time = 500, timeUnit = MILLISECONDS)
62 public class SliceUtf8Benchmark
63 {
64 @Benchmark
65 public int benchmarkLengthOfCodePointFromStartByte(BenchmarkData data)
66 {
67 Slice slice = data.getSlice();
68 int i = 0;
69 int codePoints = 0;
70 while (i < slice.length()) {
71 i += lengthOfCodePointFromStartByte(slice.getByte(i));
72 codePoints++;
73 }
74 if (codePoints != data.getLength()) {
75 throw new AssertionError();
76 }
77 return codePoints;
78 }
79
80 @Benchmark
81 public int benchmarkCountCodePoints(BenchmarkData data)
82 {
83 int codePoints = countCodePoints(data.getSlice());
84 if (codePoints != data.getLength()) {
85 throw new AssertionError();
86 }
87 return codePoints;
88 }
89
90 @Benchmark
91 public int benchmarkOffsetByCodePoints(BenchmarkData data)
92 {
93 Slice slice = data.getSlice();
94 int offset = offsetOfCodePoint(slice, data.getLength() - 1);
95 if (offset + lengthOfCodePoint(slice, offset) != slice.length()) {
96 throw new AssertionError();
97 }
98 return offset;
99 }
100
101 @Benchmark
102 public Slice benchmarkSubstring(BenchmarkData data)
103 {
104 Slice slice = data.getSlice();
105 int length = data.getLength();
106 return substring(slice, (length / 2) - 1, length / 2);
107 }
108
109 @Benchmark
110 public Slice benchmarkReverse(BenchmarkData data)
111 {
112 return reverse(data.getSlice());
113 }
114
115 @Benchmark
116 public Slice benchmarkToLowerCase(BenchmarkData data)
117 {
118 return toLowerCase(data.getSlice());
119 }
120
121 @Benchmark
122 public Slice benchmarkToUpperCase(BenchmarkData data)
123 {
124 return toUpperCase(data.getSlice());
125 }
126
127 @Benchmark
128 public Slice benchmarkLeftTrim(WhitespaceData data)
129 {
130 return leftTrim(data.getLeftWhitespace());
131 }
132
133 @Benchmark
134 public Slice benchmarkRightTrim(WhitespaceData data)
135 {
136 return rightTrim(data.getRightWhitespace());
137 }
138
139 @Benchmark
140 public Slice benchmarkTrim(WhitespaceData data)
141 {
142 return trim(data.getBothWhitespace());
143 }
144
145
146 @State(Thread)
147 public static class BenchmarkData
148 {
149 private static final int[] ASCII_CODE_POINTS;
150 private static final int[] ALL_CODE_POINTS;
151
152 static {
153 ASCII_CODE_POINTS = IntStream.rangeClosed(0, 0x7F)
154 .toArray();
155 ALL_CODE_POINTS = IntStream.rangeClosed(0, MAX_CODE_POINT)
156 .filter(codePoint -> getType(codePoint) != SURROGATE)
157 .toArray();
158 }
159
160 @Param({ "2", "5", "10", "100", "1000", "10000" })
161 private int length;
162
163 @Param({ "true", "false" })
164 private boolean ascii;
165
166 private Slice slice;
167 private int[] codePoints;
168
169 @Setup
170 public void setup()
171 {
172 int[] codePointSet = ascii ? ASCII_CODE_POINTS : ALL_CODE_POINTS;
173 ThreadLocalRandom random = ThreadLocalRandom.current();
174
175 codePoints = new int[length];
176 DynamicSliceOutput sliceOutput = new DynamicSliceOutput(length * 4);
177 for (int i = 0; i < codePoints.length; i++) {
178 int codePoint = codePointSet[random.nextInt(codePointSet.length)];
179 codePoints[i] = codePoint;
180 sliceOutput.appendBytes(new String(Character.toChars(codePoint)).getBytes(StandardCharsets.UTF_8));
181 }
182 slice = sliceOutput.slice();
183 }
184
185 public Slice getSlice()
186 {
187 return slice;
188 }
189
190 public int getLength()
191 {
192 return length;
193 }
194 }
195
196 @State(Thread)
197 public static class WhitespaceData
198 {
199 private static final int[] ASCII_WHITESPACE;
200 private static final int[] ALL_WHITESPACE;
201
202 static {
203 ASCII_WHITESPACE = IntStream.rangeClosed(0, 0x7F)
204 .filter(Character::isWhitespace)
205 .toArray();
206 ALL_WHITESPACE = IntStream.rangeClosed(0, MAX_CODE_POINT)
207 .filter(Character::isWhitespace)
208 .toArray();
209 }
210
211 @Param({ "2", "5", "10", "100", "1000", "10000" })
212 private int length;
213
214 @Param({ "true", "false" })
215 private boolean ascii;
216
217 private Slice leftWhitespace;
218 private Slice rightWhitespace;
219 private Slice bothWhitespace;
220
221 @Setup
222 public void setup()
223 {
224 Slice whitespace = createRandomUtf8Slice(ascii ? ASCII_WHITESPACE : ALL_WHITESPACE, length + 1);
225 leftWhitespace = Slices.copyOf(whitespace);
226 leftWhitespace.setByte(leftWhitespace.length() - 1, 'X');
227 rightWhitespace = Slices.copyOf(whitespace);
228 rightWhitespace.setByte(0, 'X');
229 bothWhitespace = Slices.copyOf(whitespace);
230 bothWhitespace.setByte(length / 2, 'X');
231 }
232
233 private static Slice createRandomUtf8Slice(int[] codePointSet, int length)
234 {
235 int[] codePoints = new int[length];
236 ThreadLocalRandom random = ThreadLocalRandom.current();
237 for (int i = 0; i < codePoints.length; i++) {
238 int codePoint = codePointSet[random.nextInt(codePointSet.length)];
239 codePoints[i] = codePoint;
240 }
241 return utf8Slice(new String(codePoints, 0, codePoints.length));
242 }
243
244 public int getLength()
245 {
246 return length;
247 }
248
249 public Slice getLeftWhitespace()
250 {
251 return leftWhitespace;
252 }
253
254 public Slice getRightWhitespace()
255 {
256 return rightWhitespace;
257 }
258
259 public Slice getBothWhitespace()
260 {
261 return bothWhitespace;
262 }
263 }
264
265 public static void main(String[] args)
266 throws RunnerException
267 {
268 Options options = new OptionsBuilder()
269 .verbosity(VerboseMode.NORMAL)
270 .include(".*" + SliceUtf8Benchmark.class.getSimpleName() + ".*")
271 .build();
272
273 new Runner(options).run();
274 }
275 }