]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
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 | 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 | /** | |
40 | * Benchmarks for {@link IntVector}. | |
41 | */ | |
42 | @State(Scope.Benchmark) | |
43 | public class IntBenchmarks { | |
44 | ||
45 | private static final int VECTOR_LENGTH = 1024; | |
46 | ||
47 | private static final int ALLOCATOR_CAPACITY = 1024 * 1024; | |
48 | ||
49 | private BufferAllocator allocator; | |
50 | ||
51 | private IntVector vector; | |
52 | ||
53 | @Setup | |
54 | public void prepare() { | |
55 | allocator = new RootAllocator(ALLOCATOR_CAPACITY); | |
56 | vector = new IntVector("vector", allocator); | |
57 | vector.allocateNew(VECTOR_LENGTH); | |
58 | vector.setValueCount(VECTOR_LENGTH); | |
59 | } | |
60 | ||
61 | @TearDown | |
62 | public void tearDown() { | |
63 | vector.close(); | |
64 | allocator.close(); | |
65 | } | |
66 | ||
67 | @Benchmark | |
68 | @BenchmarkMode(Mode.AverageTime) | |
69 | @OutputTimeUnit(TimeUnit.MICROSECONDS) | |
70 | public void setWithValueHolder() { | |
71 | for (int i = 0; i < VECTOR_LENGTH; i++) { | |
72 | NullableIntHolder holder = new NullableIntHolder(); | |
73 | holder.isSet = i % 3 == 0 ? 0 : 1; | |
74 | if (holder.isSet == 1) { | |
75 | holder.value = i; | |
76 | } | |
77 | vector.setSafe(i, holder); | |
78 | } | |
79 | } | |
80 | ||
81 | @Benchmark | |
82 | @BenchmarkMode(Mode.AverageTime) | |
83 | @OutputTimeUnit(TimeUnit.MICROSECONDS) | |
84 | public void setIntDirectly() { | |
85 | for (int i = 0; i < VECTOR_LENGTH; i++) { | |
86 | vector.setSafe(i, i % 3 == 0 ? 0 : 1, i); | |
87 | } | |
88 | } | |
89 | ||
90 | @Benchmark | |
91 | @BenchmarkMode(Mode.AverageTime) | |
92 | @OutputTimeUnit(TimeUnit.MICROSECONDS) | |
93 | public void setWithWriter() { | |
94 | IntWriterImpl writer = new IntWriterImpl(vector); | |
95 | for (int i = 0; i < VECTOR_LENGTH; i++) { | |
96 | if (i % 3 != 0) { | |
97 | writer.writeInt(i); | |
98 | } | |
99 | } | |
100 | } | |
101 | ||
102 | public static void main(String [] args) throws RunnerException { | |
103 | Options opt = new OptionsBuilder() | |
104 | .include(IntBenchmarks.class.getSimpleName()) | |
105 | .forks(1) | |
106 | .build(); | |
107 | ||
108 | new Runner(opt).run(); | |
109 | } | |
110 | } |