|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.arrow.vector; |
| 18 | + |
| 19 | +import java.util.UUID; |
| 20 | +import java.util.concurrent.TimeUnit; |
| 21 | +import org.apache.arrow.memory.BufferAllocator; |
| 22 | +import org.apache.arrow.memory.RootAllocator; |
| 23 | +import org.apache.arrow.vector.complex.impl.UuidWriterImpl; |
| 24 | +import org.apache.arrow.vector.holders.NullableUuidHolder; |
| 25 | +import org.apache.arrow.vector.holders.UuidHolder; |
| 26 | +import org.openjdk.jmh.annotations.Benchmark; |
| 27 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 28 | +import org.openjdk.jmh.annotations.Mode; |
| 29 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 30 | +import org.openjdk.jmh.annotations.Scope; |
| 31 | +import org.openjdk.jmh.annotations.Setup; |
| 32 | +import org.openjdk.jmh.annotations.State; |
| 33 | +import org.openjdk.jmh.annotations.TearDown; |
| 34 | +import org.openjdk.jmh.runner.Runner; |
| 35 | +import org.openjdk.jmh.runner.RunnerException; |
| 36 | +import org.openjdk.jmh.runner.options.Options; |
| 37 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 38 | + |
| 39 | +/** Benchmarks for {@link UuidVector}. */ |
| 40 | +@State(Scope.Benchmark) |
| 41 | +public class UuidVectorBenchmarks { |
| 42 | + // checkstyle:off: MissingJavadocMethod |
| 43 | + |
| 44 | + private static final int VECTOR_LENGTH = 10_000; |
| 45 | + |
| 46 | + private static final int ALLOCATOR_CAPACITY = 1024 * 1024; |
| 47 | + |
| 48 | + private BufferAllocator allocator; |
| 49 | + |
| 50 | + private UuidVector vector; |
| 51 | + |
| 52 | + private UUID[] testUuids; |
| 53 | + |
| 54 | + @Setup |
| 55 | + public void prepare() { |
| 56 | + allocator = new RootAllocator(ALLOCATOR_CAPACITY); |
| 57 | + vector = new UuidVector("vector", allocator); |
| 58 | + vector.allocateNew(VECTOR_LENGTH); |
| 59 | + vector.setValueCount(VECTOR_LENGTH); |
| 60 | + |
| 61 | + // Pre-generate UUIDs for consistent benchmarking |
| 62 | + testUuids = new UUID[VECTOR_LENGTH]; |
| 63 | + for (int i = 0; i < VECTOR_LENGTH; i++) { |
| 64 | + testUuids[i] = new UUID(i, i * 2L); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @TearDown |
| 69 | + public void tearDown() { |
| 70 | + vector.close(); |
| 71 | + allocator.close(); |
| 72 | + } |
| 73 | + |
| 74 | + @Benchmark |
| 75 | + @BenchmarkMode(Mode.AverageTime) |
| 76 | + @OutputTimeUnit(TimeUnit.MICROSECONDS) |
| 77 | + public void setWithUuidHolder() { |
| 78 | + UuidHolder holder = new UuidHolder(); |
| 79 | + for (int i = 0; i < VECTOR_LENGTH; i++) { |
| 80 | + // Old version: get the holder populated with buffer reference, then set it back |
| 81 | + vector.get(i, holder); |
| 82 | + vector.setSafe(i, holder); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Benchmark |
| 87 | + @BenchmarkMode(Mode.AverageTime) |
| 88 | + @OutputTimeUnit(TimeUnit.MICROSECONDS) |
| 89 | + public void setWithNullableUuidHolder() { |
| 90 | + NullableUuidHolder holder = new NullableUuidHolder(); |
| 91 | + for (int i = 0; i < VECTOR_LENGTH; i++) { |
| 92 | + // Old version: get the holder populated with buffer reference, then set it back |
| 93 | + holder.isSet = i % 3 == 0 ? 0 : 1; |
| 94 | + if (holder.isSet == 1) { |
| 95 | + vector.get(i, holder); |
| 96 | + } |
| 97 | + vector.setSafe(i, holder); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Benchmark |
| 102 | + @BenchmarkMode(Mode.AverageTime) |
| 103 | + @OutputTimeUnit(TimeUnit.MICROSECONDS) |
| 104 | + public void setUuidDirectly() { |
| 105 | + for (int i = 0; i < VECTOR_LENGTH; i++) { |
| 106 | + vector.setSafe(i, testUuids[i]); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Benchmark |
| 111 | + @BenchmarkMode(Mode.AverageTime) |
| 112 | + @OutputTimeUnit(TimeUnit.MICROSECONDS) |
| 113 | + public void setWithWriter() { |
| 114 | + UuidWriterImpl writer = new UuidWriterImpl(vector); |
| 115 | + for (int i = 0; i < VECTOR_LENGTH; i++) { |
| 116 | + if (i % 3 != 0) { |
| 117 | + writer.writeExtension(testUuids[i]); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @Benchmark |
| 123 | + @BenchmarkMode(Mode.AverageTime) |
| 124 | + @OutputTimeUnit(TimeUnit.MICROSECONDS) |
| 125 | + public void getWithUuidHolder() { |
| 126 | + UuidHolder holder = new UuidHolder(); |
| 127 | + for (int i = 0; i < VECTOR_LENGTH; i++) { |
| 128 | + vector.get(i, holder); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + @Benchmark |
| 133 | + @BenchmarkMode(Mode.AverageTime) |
| 134 | + @OutputTimeUnit(TimeUnit.MICROSECONDS) |
| 135 | + public void getWithNullableUuidHolder() { |
| 136 | + NullableUuidHolder holder = new NullableUuidHolder(); |
| 137 | + for (int i = 0; i < VECTOR_LENGTH; i++) { |
| 138 | + vector.get(i, holder); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Benchmark |
| 143 | + @BenchmarkMode(Mode.AverageTime) |
| 144 | + @OutputTimeUnit(TimeUnit.MICROSECONDS) |
| 145 | + public void getUuidDirectly() { |
| 146 | + for (int i = 0; i < VECTOR_LENGTH; i++) { |
| 147 | + UUID uuid = vector.getObject(i); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + public static void main(String[] args) throws RunnerException { |
| 152 | + Options opt = |
| 153 | + new OptionsBuilder().include(UuidVectorBenchmarks.class.getSimpleName()).forks(1).build(); |
| 154 | + |
| 155 | + new Runner(opt).run(); |
| 156 | + } |
| 157 | + // checkstyle:on: MissingJavadocMethod |
| 158 | +} |
| 159 | + |
0 commit comments