|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.druid.benchmark; |
| 21 | + |
| 22 | +import org.apache.druid.java.util.common.ByteBufferUtils; |
| 23 | +import org.apache.druid.query.groupby.epinephelinae.ByteBufferHashTable; |
| 24 | +import org.apache.druid.query.groupby.epinephelinae.Groupers; |
| 25 | +import org.openjdk.jmh.annotations.Benchmark; |
| 26 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 27 | +import org.openjdk.jmh.annotations.Fork; |
| 28 | +import org.openjdk.jmh.annotations.Level; |
| 29 | +import org.openjdk.jmh.annotations.Measurement; |
| 30 | +import org.openjdk.jmh.annotations.Mode; |
| 31 | +import org.openjdk.jmh.annotations.OperationsPerInvocation; |
| 32 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 33 | +import org.openjdk.jmh.annotations.Param; |
| 34 | +import org.openjdk.jmh.annotations.Scope; |
| 35 | +import org.openjdk.jmh.annotations.Setup; |
| 36 | +import org.openjdk.jmh.annotations.State; |
| 37 | +import org.openjdk.jmh.annotations.TearDown; |
| 38 | +import org.openjdk.jmh.annotations.Warmup; |
| 39 | + |
| 40 | +import java.nio.ByteBuffer; |
| 41 | +import java.util.Random; |
| 42 | +import java.util.concurrent.ThreadLocalRandom; |
| 43 | +import java.util.concurrent.TimeUnit; |
| 44 | + |
| 45 | +/** |
| 46 | + * Benchmark for ByteBufferHashTable.findBucket() method. |
| 47 | + * Measures lookup latency with various key sizes, simulating GROUP BY workloads |
| 48 | + * with a mix of existing key lookups and new key insertions. |
| 49 | + */ |
| 50 | +@BenchmarkMode(Mode.AverageTime) |
| 51 | +@OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 52 | +@OperationsPerInvocation(ByteBufferHashTableBenchmark.ITERATIONS) |
| 53 | +@Warmup(iterations = 3) |
| 54 | +@Measurement(iterations = 5) |
| 55 | +@Fork(1) |
| 56 | +@State(Scope.Benchmark) |
| 57 | +public class ByteBufferHashTableBenchmark |
| 58 | +{ |
| 59 | + public static final int ITERATIONS = 10000; |
| 60 | + |
| 61 | + private static final float MAX_LOAD_FACTOR = 0.7f; |
| 62 | + private static final int NUM_BUCKETS = 16384; |
| 63 | + // Percentage of lookups that will be for non-existent keys (simulates new group creation) |
| 64 | + private static final double NEW_KEY_RATIO = 0.1; |
| 65 | + // Size of aggregation values per bucket (e.g., 8 bytes for a long sum, 16 for sum+count, etc.) |
| 66 | + private static final int VALUE_SIZE = 16; |
| 67 | + |
| 68 | + @Param({"8", "16", "32", "64", "128"}) |
| 69 | + public int keySize; |
| 70 | + |
| 71 | + private BenchmarkHashTable hashTable; |
| 72 | + private ByteBuffer[] lookupKeys; |
| 73 | + private int[] lookupKeyHashes; |
| 74 | + |
| 75 | + // Direct buffers to be freed in tearDown |
| 76 | + private ByteBuffer tableBuffer; |
| 77 | + private ByteBuffer insertedKeysBuffer; |
| 78 | + private ByteBuffer lookupKeysBuffer; |
| 79 | + |
| 80 | + @Setup(Level.Trial) |
| 81 | + public void setup() |
| 82 | + { |
| 83 | + int bucketSize = Integer.BYTES + keySize + VALUE_SIZE; // hash + key + aggregation values |
| 84 | + tableBuffer = ByteBuffer.allocateDirect(NUM_BUCKETS * bucketSize); |
| 85 | + |
| 86 | + hashTable = new BenchmarkHashTable( |
| 87 | + MAX_LOAD_FACTOR, |
| 88 | + NUM_BUCKETS, |
| 89 | + bucketSize, |
| 90 | + tableBuffer, |
| 91 | + keySize |
| 92 | + ); |
| 93 | + hashTable.reset(); |
| 94 | + |
| 95 | + Random random = new Random(42); |
| 96 | + int numEntries = (int) (NUM_BUCKETS * MAX_LOAD_FACTOR); |
| 97 | + |
| 98 | + // Allocate direct buffer for inserted keys |
| 99 | + insertedKeysBuffer = ByteBuffer.allocateDirect(numEntries * keySize); |
| 100 | + int[] insertedKeyHashes = new int[numEntries]; |
| 101 | + |
| 102 | + // Insert entries into the hash table |
| 103 | + for (int i = 0; i < numEntries; i++) { |
| 104 | + byte[] keyBytes = new byte[keySize]; |
| 105 | + random.nextBytes(keyBytes); |
| 106 | + |
| 107 | + // Store key in direct buffer |
| 108 | + int keyOffset = i * keySize; |
| 109 | + insertedKeysBuffer.position(keyOffset); |
| 110 | + insertedKeysBuffer.put(keyBytes); |
| 111 | + |
| 112 | + // Create a slice for this key |
| 113 | + insertedKeysBuffer.position(keyOffset); |
| 114 | + insertedKeysBuffer.limit(keyOffset + keySize); |
| 115 | + ByteBuffer keyBuffer = insertedKeysBuffer.slice(); |
| 116 | + insertedKeysBuffer.clear(); // Reset limit for next iteration |
| 117 | + |
| 118 | + int keyHash = Groupers.smear(keyBuffer.getInt(0)) & Groupers.USED_FLAG_MASK; |
| 119 | + insertedKeyHashes[i] = keyHash; |
| 120 | + |
| 121 | + int bucket = hashTable.findBucket0(true, keyBuffer, keyHash); |
| 122 | + if (bucket >= 0) { |
| 123 | + // Reset position before initBucket since initializeNewBucketKey uses relative put |
| 124 | + keyBuffer.position(0); |
| 125 | + hashTable.initBucket(bucket, keyBuffer, keyHash); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + // Prepare lookup keys - mix of existing keys and new keys (not in table) |
| 130 | + // Allocate a single direct buffer for all lookup keys |
| 131 | + lookupKeysBuffer = ByteBuffer.allocateDirect(ITERATIONS * keySize); |
| 132 | + lookupKeys = new ByteBuffer[ITERATIONS]; |
| 133 | + lookupKeyHashes = new int[ITERATIONS]; |
| 134 | + |
| 135 | + int newKeyCount = (int) (ITERATIONS * NEW_KEY_RATIO); |
| 136 | + |
| 137 | + // Generate new keys that don't exist in the table |
| 138 | + Random newKeyRandom = new Random(12345); |
| 139 | + for (int i = 0; i < newKeyCount; i++) { |
| 140 | + byte[] keyBytes = new byte[keySize]; |
| 141 | + newKeyRandom.nextBytes(keyBytes); |
| 142 | + |
| 143 | + int keyOffset = i * keySize; |
| 144 | + lookupKeysBuffer.position(keyOffset); |
| 145 | + lookupKeysBuffer.put(keyBytes); |
| 146 | + |
| 147 | + // Create a slice for this key |
| 148 | + lookupKeysBuffer.position(keyOffset); |
| 149 | + lookupKeysBuffer.limit(keyOffset + keySize); |
| 150 | + lookupKeys[i] = lookupKeysBuffer.slice(); |
| 151 | + lookupKeysBuffer.clear(); |
| 152 | + |
| 153 | + lookupKeyHashes[i] = Groupers.smear(lookupKeys[i].getInt(0)) & Groupers.USED_FLAG_MASK; |
| 154 | + } |
| 155 | + |
| 156 | + // Fill the rest with existing keys (copy from insertedKeysBuffer) |
| 157 | + for (int i = newKeyCount; i < ITERATIONS; i++) { |
| 158 | + int idx = ThreadLocalRandom.current().nextInt(numEntries); |
| 159 | + |
| 160 | + // Copy key data from inserted keys buffer |
| 161 | + int srcOffset = idx * keySize; |
| 162 | + int dstOffset = i * keySize; |
| 163 | + |
| 164 | + for (int j = 0; j < keySize; j++) { |
| 165 | + lookupKeysBuffer.put(dstOffset + j, insertedKeysBuffer.get(srcOffset + j)); |
| 166 | + } |
| 167 | + |
| 168 | + // Create a slice for this key |
| 169 | + lookupKeysBuffer.position(dstOffset); |
| 170 | + lookupKeysBuffer.limit(dstOffset + keySize); |
| 171 | + lookupKeys[i] = lookupKeysBuffer.slice(); |
| 172 | + lookupKeysBuffer.clear(); |
| 173 | + |
| 174 | + lookupKeyHashes[i] = insertedKeyHashes[idx]; |
| 175 | + } |
| 176 | + |
| 177 | + // Shuffle to mix new and existing keys |
| 178 | + for (int i = ITERATIONS - 1; i > 0; i--) { |
| 179 | + int j = ThreadLocalRandom.current().nextInt(i + 1); |
| 180 | + ByteBuffer tempKey = lookupKeys[i]; |
| 181 | + lookupKeys[i] = lookupKeys[j]; |
| 182 | + lookupKeys[j] = tempKey; |
| 183 | + int tempHash = lookupKeyHashes[i]; |
| 184 | + lookupKeyHashes[i] = lookupKeyHashes[j]; |
| 185 | + lookupKeyHashes[j] = tempHash; |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + @TearDown(Level.Trial) |
| 190 | + public void tearDown() |
| 191 | + { |
| 192 | + if (tableBuffer != null) { |
| 193 | + ByteBufferUtils.free(tableBuffer); |
| 194 | + tableBuffer = null; |
| 195 | + } |
| 196 | + if (insertedKeysBuffer != null) { |
| 197 | + ByteBufferUtils.free(insertedKeysBuffer); |
| 198 | + insertedKeysBuffer = null; |
| 199 | + } |
| 200 | + if (lookupKeysBuffer != null) { |
| 201 | + ByteBufferUtils.free(lookupKeysBuffer); |
| 202 | + lookupKeysBuffer = null; |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + @Benchmark |
| 207 | + public int findBucket() |
| 208 | + { |
| 209 | + int result = 0; |
| 210 | + for (int i = 0; i < ITERATIONS; i++) { |
| 211 | + // allowNewBucket=true simulates GROUP BY where new groups can be created |
| 212 | + result ^= hashTable.findBucket0(true, lookupKeys[i], lookupKeyHashes[i]); |
| 213 | + } |
| 214 | + return result; |
| 215 | + } |
| 216 | + |
| 217 | + /** |
| 218 | + * Test harness that exposes protected findBucket() method for benchmarking. |
| 219 | + */ |
| 220 | + private static class BenchmarkHashTable extends ByteBufferHashTable |
| 221 | + { |
| 222 | + BenchmarkHashTable( |
| 223 | + float maxLoadFactor, |
| 224 | + int initialBuckets, |
| 225 | + int bucketSizeWithHash, |
| 226 | + ByteBuffer buffer, |
| 227 | + int keySize |
| 228 | + ) |
| 229 | + { |
| 230 | + super(maxLoadFactor, initialBuckets, bucketSizeWithHash, buffer, keySize, Integer.MAX_VALUE, null); |
| 231 | + } |
| 232 | + |
| 233 | + int findBucket0(boolean allowNewBucket, ByteBuffer keyBuffer, int keyHash) |
| 234 | + { |
| 235 | + return findBucket(allowNewBucket, maxBuckets, tableBuffer, keyBuffer, keyHash); |
| 236 | + } |
| 237 | + |
| 238 | + void initBucket(int bucket, ByteBuffer keyBuffer, int keyHash) |
| 239 | + { |
| 240 | + initializeNewBucketKey(bucket, keyBuffer, keyHash); |
| 241 | + } |
| 242 | + } |
| 243 | +} |
0 commit comments