|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.benchmark.common.util; |
| 11 | + |
| 12 | +import org.apache.lucene.util.BytesRef; |
| 13 | +import org.apache.lucene.util.UnicodeUtil; |
| 14 | +import org.elasticsearch.common.UUIDs; |
| 15 | +import org.openjdk.jmh.annotations.Benchmark; |
| 16 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 17 | +import org.openjdk.jmh.annotations.Fork; |
| 18 | +import org.openjdk.jmh.annotations.Measurement; |
| 19 | +import org.openjdk.jmh.annotations.Mode; |
| 20 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 21 | +import org.openjdk.jmh.annotations.Param; |
| 22 | +import org.openjdk.jmh.annotations.Scope; |
| 23 | +import org.openjdk.jmh.annotations.Setup; |
| 24 | +import org.openjdk.jmh.annotations.State; |
| 25 | +import org.openjdk.jmh.annotations.Warmup; |
| 26 | + |
| 27 | +import java.nio.ByteBuffer; |
| 28 | +import java.nio.charset.StandardCharsets; |
| 29 | +import java.util.concurrent.ThreadLocalRandom; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | + |
| 32 | +@Warmup(iterations = 3) |
| 33 | +@Measurement(iterations = 3) |
| 34 | +@BenchmarkMode(Mode.AverageTime) |
| 35 | +@OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 36 | +@Fork(value = 1) |
| 37 | +public class UTF8StringBytesBenchmark { |
| 38 | + |
| 39 | + @State(Scope.Thread) |
| 40 | + public static class StringState { |
| 41 | + @Param({ "uuid", "short", "long", "nonAscii", "veryLong" }) |
| 42 | + String stringType; |
| 43 | + |
| 44 | + String string; |
| 45 | + BytesRef bytes; |
| 46 | + |
| 47 | + @Setup |
| 48 | + public void setup() { |
| 49 | + string = switch (stringType) { |
| 50 | + case "uuid" -> UUIDs.base64UUID(); |
| 51 | + case "short" -> generateAsciiString(20); |
| 52 | + case "long" -> generateAsciiString(100); |
| 53 | + case "nonAscii" -> generateUTF8String(200); |
| 54 | + case "veryLong" -> generateAsciiString(1000); |
| 55 | + default -> throw new IllegalArgumentException("Unknown stringType: " + stringType); |
| 56 | + }; |
| 57 | + bytes = getBytes(string); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Benchmark |
| 62 | + public BytesRef getBytesJDK(StringState state) { |
| 63 | + byte[] bytes = state.string.getBytes(StandardCharsets.UTF_8); |
| 64 | + return new BytesRef(bytes, 0, bytes.length); |
| 65 | + } |
| 66 | + |
| 67 | + @Benchmark |
| 68 | + public BytesRef getBytesUnicodeUtils(StringState state) { |
| 69 | + String string = state.string; |
| 70 | + int length = string.length(); |
| 71 | + int size = UnicodeUtil.calcUTF16toUTF8Length(string, 0, length); |
| 72 | + byte[] out = new byte[size]; |
| 73 | + UnicodeUtil.UTF16toUTF8(string, 0, length, out, 0); |
| 74 | + return new BytesRef(out, 0, out.length); |
| 75 | + } |
| 76 | + |
| 77 | + @Benchmark |
| 78 | + public BytesRef getBytesByteBufferEncoder(StringState state) { |
| 79 | + var byteBuff = StandardCharsets.UTF_8.encode(state.string); |
| 80 | + assert byteBuff.hasArray(); |
| 81 | + return new BytesRef(byteBuff.array(), byteBuff.arrayOffset() + byteBuff.position(), byteBuff.remaining()); |
| 82 | + } |
| 83 | + |
| 84 | + @Benchmark |
| 85 | + public String getStringJDK(StringState state) { |
| 86 | + BytesRef bytes = state.bytes; |
| 87 | + return new String(bytes.bytes, bytes.offset, bytes.length, StandardCharsets.UTF_8); |
| 88 | + } |
| 89 | + |
| 90 | + @Benchmark |
| 91 | + public String getStringByteBufferDecoder(StringState state) { |
| 92 | + BytesRef bytes = state.bytes; |
| 93 | + var byteBuff = ByteBuffer.wrap(bytes.bytes, bytes.offset, bytes.length); |
| 94 | + return StandardCharsets.UTF_8.decode(byteBuff).toString(); |
| 95 | + } |
| 96 | + |
| 97 | + private static BytesRef getBytes(String string) { |
| 98 | + int before = ThreadLocalRandom.current().nextInt(0, 50); |
| 99 | + int after = ThreadLocalRandom.current().nextInt(0, 50); |
| 100 | + byte[] stringBytes = string.getBytes(StandardCharsets.UTF_8); |
| 101 | + byte[] finalBytes = new byte[before + after + stringBytes.length]; |
| 102 | + System.arraycopy(stringBytes, 0, finalBytes, before, stringBytes.length); |
| 103 | + return new BytesRef(finalBytes, before, stringBytes.length); |
| 104 | + } |
| 105 | + |
| 106 | + public static String generateAsciiString(int n) { |
| 107 | + ThreadLocalRandom random = ThreadLocalRandom.current(); |
| 108 | + StringBuilder sb = new StringBuilder(n); |
| 109 | + |
| 110 | + for (int i = 0; i < n; i++) { |
| 111 | + int ascii = random.nextInt(128); |
| 112 | + sb.append((char) ascii); |
| 113 | + } |
| 114 | + |
| 115 | + return sb.toString(); |
| 116 | + } |
| 117 | + |
| 118 | + public static String generateUTF8String(int n) { |
| 119 | + ThreadLocalRandom random = ThreadLocalRandom.current(); |
| 120 | + StringBuilder sb = new StringBuilder(n); |
| 121 | + |
| 122 | + for (int i = 0; i < n; i++) { |
| 123 | + int codePoint; |
| 124 | + int probability = random.nextInt(100); |
| 125 | + |
| 126 | + if (probability < 85) { |
| 127 | + // 1-byte UTF-8 (ASCII range) |
| 128 | + // 0x0000 to 0x007F |
| 129 | + codePoint = random.nextInt(0x0080); |
| 130 | + } else if (probability < 95) { |
| 131 | + // 2-byte UTF-8 |
| 132 | + // 0x0080 to 0x07FF |
| 133 | + codePoint = random.nextInt(0x0080, 0x0800); |
| 134 | + } else { |
| 135 | + // 3-byte UTF-8 |
| 136 | + // 0x0800 to 0xFFFF |
| 137 | + do { |
| 138 | + codePoint = random.nextInt(0x0800, 0x10000); |
| 139 | + // Skip surrogate pairs (0xD800-0xDFFF) |
| 140 | + } while (codePoint >= 0xD800 && codePoint <= 0xDFFF); |
| 141 | + } |
| 142 | + |
| 143 | + sb.appendCodePoint(codePoint); |
| 144 | + } |
| 145 | + |
| 146 | + return sb.toString(); |
| 147 | + } |
| 148 | +} |
0 commit comments