|  | 
|  | 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.compare.ApproxEqualsVisitor; | 
|  | 25 | +import org.apache.arrow.vector.compare.Range; | 
|  | 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 floating point vectors. | 
|  | 41 | + */ | 
|  | 42 | +@State(Scope.Benchmark) | 
|  | 43 | +public class FloatingPointBenchmarks { | 
|  | 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 Float4Vector floatVector1; | 
|  | 52 | + | 
|  | 53 | +  private Float4Vector floatVector2; | 
|  | 54 | + | 
|  | 55 | +  private Float8Vector doubleVector1; | 
|  | 56 | + | 
|  | 57 | +  private Float8Vector doubleVector2; | 
|  | 58 | + | 
|  | 59 | +  private ApproxEqualsVisitor floatVisitor; | 
|  | 60 | + | 
|  | 61 | +  private ApproxEqualsVisitor doubleVisitor; | 
|  | 62 | + | 
|  | 63 | +  private Range range; | 
|  | 64 | + | 
|  | 65 | +  /** | 
|  | 66 | +   * Setup benchmarks. | 
|  | 67 | +   */ | 
|  | 68 | +  @Setup | 
|  | 69 | +  public void prepare() { | 
|  | 70 | +    allocator = new RootAllocator(ALLOCATOR_CAPACITY); | 
|  | 71 | +    floatVector1 = new Float4Vector("vector", allocator); | 
|  | 72 | +    floatVector2 = new Float4Vector("vector", allocator); | 
|  | 73 | +    doubleVector1 = new Float8Vector("vector", allocator); | 
|  | 74 | +    doubleVector2 = new Float8Vector("vector", allocator); | 
|  | 75 | + | 
|  | 76 | +    floatVector1.allocateNew(VECTOR_LENGTH); | 
|  | 77 | +    floatVector2.allocateNew(VECTOR_LENGTH); | 
|  | 78 | +    doubleVector1.allocateNew(VECTOR_LENGTH); | 
|  | 79 | +    doubleVector2.allocateNew(VECTOR_LENGTH); | 
|  | 80 | + | 
|  | 81 | +    for (int i = 0; i < VECTOR_LENGTH; i++) { | 
|  | 82 | +      if (i % 3 == 0) { | 
|  | 83 | +        floatVector1.setNull(i); | 
|  | 84 | +        floatVector2.setNull(i); | 
|  | 85 | +        doubleVector1.setNull(i); | 
|  | 86 | +        doubleVector2.setNull(i); | 
|  | 87 | +      } else { | 
|  | 88 | +        floatVector1.set(i, i * i); | 
|  | 89 | +        floatVector2.set(i, i * i); | 
|  | 90 | +        doubleVector1.set(i, i * i); | 
|  | 91 | +        doubleVector2.set(i, i * i); | 
|  | 92 | +      } | 
|  | 93 | +    } | 
|  | 94 | +    floatVector1.setValueCount(VECTOR_LENGTH); | 
|  | 95 | +    floatVector2.setValueCount(VECTOR_LENGTH); | 
|  | 96 | +    doubleVector1.setValueCount(VECTOR_LENGTH); | 
|  | 97 | +    doubleVector2.setValueCount(VECTOR_LENGTH); | 
|  | 98 | + | 
|  | 99 | +    floatVisitor = new ApproxEqualsVisitor(floatVector1, floatVector2, 0.01f, 0.01); | 
|  | 100 | +    doubleVisitor = new ApproxEqualsVisitor(doubleVector1, doubleVector2, 0.01f, 0.01); | 
|  | 101 | +    range = new Range(0, 0, VECTOR_LENGTH); | 
|  | 102 | +  } | 
|  | 103 | + | 
|  | 104 | +  /** | 
|  | 105 | +   * Tear down benchmarks. | 
|  | 106 | +   */ | 
|  | 107 | +  @TearDown | 
|  | 108 | +  public void tearDown() { | 
|  | 109 | +    floatVector1.close(); | 
|  | 110 | +    floatVector2.close(); | 
|  | 111 | +    doubleVector1.close(); | 
|  | 112 | +    doubleVector2.close(); | 
|  | 113 | +    allocator.close(); | 
|  | 114 | +  } | 
|  | 115 | + | 
|  | 116 | +  @Benchmark | 
|  | 117 | +  @BenchmarkMode(Mode.AverageTime) | 
|  | 118 | +  @OutputTimeUnit(TimeUnit.MICROSECONDS) | 
|  | 119 | +  public int approxEqualsBenchmark() { | 
|  | 120 | +    boolean floatResult =  floatVisitor.visit(floatVector1, range); | 
|  | 121 | +    boolean doubleResult = doubleVisitor.visit(doubleVector1, range); | 
|  | 122 | +    return (floatResult ? 1 : 0) + (doubleResult ? 1 : 0); | 
|  | 123 | +  } | 
|  | 124 | + | 
|  | 125 | +  public static void main(String [] args) throws RunnerException { | 
|  | 126 | +    Options opt = new OptionsBuilder() | 
|  | 127 | +            .include(FloatingPointBenchmarks.class.getSimpleName()) | 
|  | 128 | +            .forks(1) | 
|  | 129 | +            .build(); | 
|  | 130 | + | 
|  | 131 | +    new Runner(opt).run(); | 
|  | 132 | +  } | 
|  | 133 | +} | 
|  | 134 | + | 
0 commit comments