|
| 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.concurrent.TimeUnit; |
| 20 | +import org.apache.arrow.memory.ArrowBuf; |
| 21 | +import org.apache.arrow.memory.BufferAllocator; |
| 22 | +import org.apache.arrow.memory.RootAllocator; |
| 23 | +import org.apache.arrow.vector.holders.NullableVarCharHolder; |
| 24 | +import org.openjdk.jmh.annotations.Benchmark; |
| 25 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 26 | +import org.openjdk.jmh.annotations.Level; |
| 27 | +import org.openjdk.jmh.annotations.Mode; |
| 28 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 29 | +import org.openjdk.jmh.annotations.Param; |
| 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 BaseVariableWidthVector}. */ |
| 40 | +@State(Scope.Benchmark) |
| 41 | +public class VariableWidthVectorInlineValueBenchmarks { |
| 42 | + // checkstyle:off: MissingJavadocMethod |
| 43 | + |
| 44 | + private static final int VECTOR_CAPACITY = 16 * 1024; |
| 45 | + |
| 46 | + private static final int VECTOR_LENGTH = 1024; |
| 47 | + |
| 48 | + private static final int ALLOCATOR_CAPACITY = 1024 * 1024; |
| 49 | + |
| 50 | + private static final byte[] bytes = VariableWidthVectorBenchmarkConstants.SHORT_VALUE.getBytes(); |
| 51 | + private ArrowBuf arrowBuff; |
| 52 | + |
| 53 | + private BufferAllocator allocator; |
| 54 | + |
| 55 | + private VarCharVector vector; |
| 56 | + |
| 57 | + @Param({"1", "2", "10", "40"}) |
| 58 | + private int step; |
| 59 | + |
| 60 | + /** Setup benchmarks. */ |
| 61 | + @Setup(Level.Iteration) |
| 62 | + public void prepare() { |
| 63 | + allocator = new RootAllocator(ALLOCATOR_CAPACITY); |
| 64 | + vector = new VarCharVector("vector", allocator); |
| 65 | + vector.allocateNew(VECTOR_CAPACITY, VECTOR_LENGTH); |
| 66 | + arrowBuff = allocator.buffer(VECTOR_LENGTH); |
| 67 | + arrowBuff.setBytes(0, bytes, 0, bytes.length); |
| 68 | + } |
| 69 | + |
| 70 | + /** Tear down benchmarks. */ |
| 71 | + @TearDown(Level.Iteration) |
| 72 | + public void tearDown() { |
| 73 | + arrowBuff.close(); |
| 74 | + vector.close(); |
| 75 | + allocator.close(); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Test {@link BaseVariableWidthVector#getValueCapacity()}. |
| 80 | + * |
| 81 | + * @return useless. To avoid DCE by JIT. |
| 82 | + */ |
| 83 | + @Benchmark |
| 84 | + @BenchmarkMode(Mode.AverageTime) |
| 85 | + @OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 86 | + public int getValueCapacity() { |
| 87 | + return vector.getValueCapacity(); |
| 88 | + } |
| 89 | + |
| 90 | + @Benchmark |
| 91 | + @BenchmarkMode(Mode.AverageTime) |
| 92 | + @OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 93 | + public int setSafeFromArray() { |
| 94 | + for (int i = 0; i < 500; ++i) { |
| 95 | + vector.setSafe(i * step, bytes); |
| 96 | + } |
| 97 | + return vector.getBufferSize(); |
| 98 | + } |
| 99 | + |
| 100 | + @Benchmark |
| 101 | + @BenchmarkMode(Mode.AverageTime) |
| 102 | + @OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 103 | + public int setSafeFromNullableVarcharHolder() { |
| 104 | + NullableVarCharHolder nvch = new NullableVarCharHolder(); |
| 105 | + nvch.buffer = arrowBuff; |
| 106 | + nvch.start = 0; |
| 107 | + nvch.end = bytes.length; |
| 108 | + for (int i = 0; i < 50; ++i) { |
| 109 | + nvch.isSet = 0; |
| 110 | + for (int j = 0; j < 9; ++j) { |
| 111 | + int idx = 10 * i + j; |
| 112 | + vector.setSafe(idx, nvch); |
| 113 | + } |
| 114 | + nvch.isSet = 1; |
| 115 | + vector.setSafe(10 * (i + 1), nvch); |
| 116 | + } |
| 117 | + return vector.getBufferSize(); |
| 118 | + } |
| 119 | + |
| 120 | + public static void main(String[] args) throws RunnerException { |
| 121 | + Options opt = |
| 122 | + new OptionsBuilder() |
| 123 | + .include(VariableWidthVectorInlineValueBenchmarks.class.getSimpleName()) |
| 124 | + .forks(1) |
| 125 | + .build(); |
| 126 | + |
| 127 | + new Runner(opt).run(); |
| 128 | + } |
| 129 | + // checkstyle:on: MissingJavadocMethod |
| 130 | +} |
0 commit comments