diff --git a/itests/hive-jmh/src/main/java/org/apache/hive/benchmark/vectorization/VectorizationBench.java b/itests/hive-jmh/src/main/java/org/apache/hive/benchmark/vectorization/VectorizationBench.java new file mode 100644 index 0000000..47842aa --- /dev/null +++ b/itests/hive-jmh/src/main/java/org/apache/hive/benchmark/vectorization/VectorizationBench.java @@ -0,0 +1,112 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hive.benchmark.vectorization; + +import org.apache.hadoop.hive.ql.exec.vector.ColumnVector; +import org.apache.hadoop.hive.ql.exec.vector.DoubleColumnVector; +import org.apache.hadoop.hive.ql.exec.vector.LongColumnVector; +import org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch; +import org.apache.hadoop.hive.ql.exec.vector.expressions.VectorExpression; +import org.apache.hadoop.hive.ql.exec.vector.expressions.gen.DoubleColAddDoubleColumn; +import org.apache.hadoop.hive.ql.exec.vector.expressions.gen.LongColAddLongColumn; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.Param; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.RunnerException; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +import java.util.Random; + +@State(Scope.Benchmark) +public class VectorizationBench { + /** + * This test measures the performance for vectorization. + * + * This test uses JMH framework for benchmarking. + * You may execute this benchmark tool using JMH command line in different ways: + * + * To use the settings shown in the main() function, use: + * $ java -cp target/benchmarks.jar org.apache.hive.benchmark.vectorization.VectorizationBench + * + * To use the default settings used by JMH, use: + * $ java -jar target/benchmarks.jar org.apache.hive.benchmark.vectorization VectorizationBench + * + * To specify different parameters, use: + * - This command will use 10 warm-up iterations, 5 test iterations, and 2 forks. And it will + * display the Average Time (avgt) in Microseconds (us) + * - Benchmark mode. Available modes are: + * [Throughput/thrpt, AverageTime/avgt, SampleTime/sample, SingleShotTime/ss, All/all] + * - Output time unit. Available time units are: [m, s, ms, us, ns]. + * + * $ java -jar target/benchmarks.jar org.apache.hive.benchmark.vectorization VectorizationBench -wi 10 -i 5 -f 2 -bm avgt -tu us + */ + + private VectorizedRowBatch rowBatch; + private VectorExpression expression; + private int iterations = /*default = */10000000; + + @Param({"Long", "Double"}) + private String type; + + @Setup(Level.Trial) + public void initialRowBatch() { + Random random = new Random(); + ColumnVector inputColVector1, inputColVector2, outputColVector; + if (type.equalsIgnoreCase("Long")) { + inputColVector1 = new DoubleColumnVector(); + inputColVector2 = new DoubleColumnVector(); + outputColVector = new DoubleColumnVector(); + for (int i = 0; i < VectorizedRowBatch.DEFAULT_SIZE; i++) { + ((DoubleColumnVector) inputColVector1).vector[i] = random.nextDouble(); + } + ((DoubleColumnVector) inputColVector2).fill(random.nextDouble()); + expression = new DoubleColAddDoubleColumn(); + } else if (type.equalsIgnoreCase("Double")) { + inputColVector1 = new LongColumnVector(); + inputColVector2 = new LongColumnVector(); + outputColVector = new LongColumnVector(); + for (int i = 0; i < VectorizedRowBatch.DEFAULT_SIZE; i++) { + ((LongColumnVector) inputColVector1).vector[i] = random.nextInt(); + } + ((LongColumnVector) inputColVector2).fill(random.nextInt()); + expression = new LongColAddLongColumn(); + } else { + throw new RuntimeException("Unsupported type"); + } + rowBatch = new VectorizedRowBatch(3); + rowBatch.cols[0] = inputColVector1; + rowBatch.cols[1] = inputColVector2; + rowBatch.cols[2] = outputColVector; + } + + @Benchmark + public void evaluationBenchMark() { + for (int i = 0; i < iterations; i++) { + expression.evaluate(rowBatch); + } + } + + public static void main(String[] args) throws RunnerException { + Options opt = new OptionsBuilder().include(".*" + VectorizationBench.class.getSimpleName() + + ".*").forks(1).warmupIterations(5).measurementIterations(5).build(); + new Runner(opt).run(); + } +} \ No newline at end of file