Skip to content

Commit 353aac6

Browse files
committed
Add Uuid vector benchmarks
1 parent e7a2f97 commit 353aac6

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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.profile.GCProfiler;
35+
import org.openjdk.jmh.runner.Runner;
36+
import org.openjdk.jmh.runner.RunnerException;
37+
import org.openjdk.jmh.runner.options.Options;
38+
import org.openjdk.jmh.runner.options.OptionsBuilder;
39+
40+
/** Benchmarks for {@link UuidVector}. */
41+
@State(Scope.Benchmark)
42+
public class UuidVectorBenchmarks {
43+
// checkstyle:off: MissingJavadocMethod
44+
45+
private static final int VECTOR_LENGTH = 10_000;
46+
47+
private static final int ALLOCATOR_CAPACITY = 1024 * 1024;
48+
49+
private BufferAllocator allocator;
50+
51+
private UuidVector vector;
52+
53+
private UUID[] testUuids;
54+
55+
@Setup
56+
public void prepare() {
57+
allocator = new RootAllocator(ALLOCATOR_CAPACITY);
58+
vector = new UuidVector("vector", allocator);
59+
vector.allocateNew(VECTOR_LENGTH);
60+
vector.setValueCount(VECTOR_LENGTH);
61+
62+
// Pre-generate UUIDs for consistent benchmarking
63+
testUuids = new UUID[VECTOR_LENGTH];
64+
for (int i = 0; i < VECTOR_LENGTH; i++) {
65+
testUuids[i] = new UUID(i, i * 2L);
66+
}
67+
}
68+
69+
@TearDown
70+
public void tearDown() {
71+
vector.close();
72+
allocator.close();
73+
}
74+
75+
@Benchmark
76+
@BenchmarkMode(Mode.AverageTime)
77+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
78+
public void setWithUuidHolder() {
79+
UuidHolder holder = new UuidHolder();
80+
for (int i = 0; i < VECTOR_LENGTH; i++) {
81+
// Old version: get the holder populated with buffer reference, then set it back
82+
vector.get(i, holder);
83+
vector.setSafe(i, holder);
84+
}
85+
}
86+
87+
@Benchmark
88+
@BenchmarkMode(Mode.AverageTime)
89+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
90+
public void setWithNullableUuidHolder() {
91+
NullableUuidHolder holder = new NullableUuidHolder();
92+
for (int i = 0; i < VECTOR_LENGTH; i++) {
93+
// Old version: get the holder populated with buffer reference, then set it back
94+
holder.isSet = i % 3 == 0 ? 0 : 1;
95+
if (holder.isSet == 1) {
96+
vector.get(i, holder);
97+
}
98+
vector.setSafe(i, holder);
99+
}
100+
}
101+
102+
@Benchmark
103+
@BenchmarkMode(Mode.AverageTime)
104+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
105+
public void setUuidDirectly() {
106+
for (int i = 0; i < VECTOR_LENGTH; i++) {
107+
vector.setSafe(i, testUuids[i]);
108+
}
109+
}
110+
111+
@Benchmark
112+
@BenchmarkMode(Mode.AverageTime)
113+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
114+
public void setWithWriter() {
115+
UuidWriterImpl writer = new UuidWriterImpl(vector);
116+
for (int i = 0; i < VECTOR_LENGTH; i++) {
117+
if (i % 3 != 0) {
118+
writer.writeExtension(testUuids[i]);
119+
}
120+
}
121+
}
122+
123+
@Benchmark
124+
@BenchmarkMode(Mode.AverageTime)
125+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
126+
public void getWithUuidHolder() {
127+
UuidHolder holder = new UuidHolder();
128+
for (int i = 0; i < VECTOR_LENGTH; i++) {
129+
vector.get(i, holder);
130+
}
131+
}
132+
133+
@Benchmark
134+
@BenchmarkMode(Mode.AverageTime)
135+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
136+
public void getWithNullableUuidHolder() {
137+
NullableUuidHolder holder = new NullableUuidHolder();
138+
for (int i = 0; i < VECTOR_LENGTH; i++) {
139+
vector.get(i, holder);
140+
}
141+
}
142+
143+
@Benchmark
144+
@BenchmarkMode(Mode.AverageTime)
145+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
146+
public void getUuidDirectly() {
147+
for (int i = 0; i < VECTOR_LENGTH; i++) {
148+
UUID uuid = vector.getObject(i);
149+
}
150+
}
151+
152+
public static void main(String[] args) throws RunnerException {
153+
Options opt =
154+
new OptionsBuilder()
155+
.include(UuidVectorBenchmarks.class.getSimpleName())
156+
.forks(1)
157+
.addProfiler(GCProfiler.class)
158+
.build();
159+
160+
new Runner(opt).run();
161+
}
162+
// checkstyle:on: MissingJavadocMethod
163+
}

0 commit comments

Comments
 (0)