|
| 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 | + |
| 18 | +package org.apache.arrow.vector; |
| 19 | + |
| 20 | +import java.util.concurrent.TimeUnit; |
| 21 | + |
| 22 | +import org.apache.arrow.memory.BufferAllocator; |
| 23 | +import org.apache.arrow.memory.RootAllocator; |
| 24 | +import org.apache.arrow.vector.complex.impl.IntWriterImpl; |
| 25 | +import org.apache.arrow.vector.holders.NullableIntHolder; |
| 26 | + |
| 27 | +import org.openjdk.jmh.annotations.Benchmark; |
| 28 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 29 | +import org.openjdk.jmh.annotations.Mode; |
| 30 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 31 | +import org.openjdk.jmh.annotations.Scope; |
| 32 | +import org.openjdk.jmh.annotations.Setup; |
| 33 | +import org.openjdk.jmh.annotations.State; |
| 34 | +import org.openjdk.jmh.annotations.TearDown; |
| 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 | +/** |
| 41 | + * Benchmarks for {@link IntVector}. |
| 42 | + */ |
| 43 | +@State(Scope.Benchmark) |
| 44 | +public class IntBenchmarks { |
| 45 | + |
| 46 | + private static final int VECTOR_LENGTH = 1024; |
| 47 | + |
| 48 | + private static final int ALLOCATOR_CAPACITY = 1024 * 1024; |
| 49 | + |
| 50 | + private BufferAllocator allocator; |
| 51 | + |
| 52 | + private IntVector vector; |
| 53 | + |
| 54 | + @Setup |
| 55 | + public void prepare() { |
| 56 | + allocator = new RootAllocator(ALLOCATOR_CAPACITY); |
| 57 | + vector = new IntVector("vector", allocator); |
| 58 | + vector.allocateNew(VECTOR_LENGTH); |
| 59 | + vector.setValueCount(VECTOR_LENGTH); |
| 60 | + } |
| 61 | + |
| 62 | + @TearDown |
| 63 | + public void tearDown() { |
| 64 | + vector.close(); |
| 65 | + allocator.close(); |
| 66 | + } |
| 67 | + |
| 68 | + @Benchmark |
| 69 | + @BenchmarkMode(Mode.AverageTime) |
| 70 | + @OutputTimeUnit(TimeUnit.MICROSECONDS) |
| 71 | + public void setWithValueHolder() { |
| 72 | + for (int i = 0; i < VECTOR_LENGTH; i++) { |
| 73 | + NullableIntHolder holder = new NullableIntHolder(); |
| 74 | + holder.isSet = i % 3 == 0 ? 0 : 1; |
| 75 | + if (holder.isSet == 1) { |
| 76 | + holder.value = i; |
| 77 | + } |
| 78 | + vector.setSafe(i, holder); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Benchmark |
| 83 | + @BenchmarkMode(Mode.AverageTime) |
| 84 | + @OutputTimeUnit(TimeUnit.MICROSECONDS) |
| 85 | + public void setIntDirectly() { |
| 86 | + for (int i = 0; i < VECTOR_LENGTH; i++) { |
| 87 | + vector.setSafe(i, i % 3 == 0 ? 0 : 1, i); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @Benchmark |
| 92 | + @BenchmarkMode(Mode.AverageTime) |
| 93 | + @OutputTimeUnit(TimeUnit.MICROSECONDS) |
| 94 | + public void setWithWriter() { |
| 95 | + IntWriterImpl writer = new IntWriterImpl(vector); |
| 96 | + for (int i = 0; i < VECTOR_LENGTH; i++) { |
| 97 | + if (i % 3 != 0) { |
| 98 | + writer.writeInt(i); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public static void main(String [] args) throws RunnerException { |
| 104 | + Options opt = new OptionsBuilder() |
| 105 | + .include(IntBenchmarks.class.getSimpleName()) |
| 106 | + .forks(1) |
| 107 | + .build(); |
| 108 | + |
| 109 | + new Runner(opt).run(); |
| 110 | + } |
| 111 | +} |
0 commit comments