From e734b404f93ed42c9c1688696d2e8da3e3c39877 Mon Sep 17 00:00:00 2001 From: liyafan82 Date: Fri, 31 May 2019 15:52:50 +0800 Subject: [PATCH 1/2] [ARROW-5461][Java] Add micro-benchmarks for Float8Vector and allocators --- java/performance/pom.xml | 153 ++++++++++++++++++ .../benchmark/micro/AllocatorBenchmarks.java | 98 +++++++++++ .../benchmark/micro/Float8Benchmarks.java | 95 +++++++++++ java/pom.xml | 1 + 4 files changed, 347 insertions(+) create mode 100644 java/performance/pom.xml create mode 100644 java/performance/src/main/java/org/apache/arrow/performance/benchmark/micro/AllocatorBenchmarks.java create mode 100644 java/performance/src/main/java/org/apache/arrow/performance/benchmark/micro/Float8Benchmarks.java diff --git a/java/performance/pom.xml b/java/performance/pom.xml new file mode 100644 index 00000000000..8b3e828aded --- /dev/null +++ b/java/performance/pom.xml @@ -0,0 +1,153 @@ + + + + 4.0.0 + + arrow-java-root + org.apache.arrow + 0.14.0-SNAPSHOT + + arrow-performance + jar + Arrow Performance Benchmarks + + + + org.openjdk.jmh + jmh-core + ${jmh.version} + + + org.openjdk.jmh + jmh-generator-annprocess + ${jmh.version} + provided + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.1 + runtime + + + org.apache.logging.log4j + log4j-core + 2.1 + runtime + + + org.apache.arrow + arrow-vector + ${project.version} + + + org.apache.arrow + arrow-memory + ${project.version} + + + + + UTF-8 + 1.21 + 1.8 + benchmarks + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + ${javac.target} + ${javac.target} + ${javac.target} + + + + org.apache.maven.plugins + maven-shade-plugin + 2.2 + + + package + + shade + + + ${uberjar.name} + + + org.openjdk.jmh.Main + + + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + + + + + + + + maven-clean-plugin + 2.5 + + + maven-deploy-plugin + 2.8.1 + + + maven-install-plugin + 2.5.1 + + + maven-jar-plugin + 2.4 + + + maven-javadoc-plugin + 2.9.1 + + + maven-resources-plugin + 2.6 + + + maven-site-plugin + 3.3 + + + maven-source-plugin + 2.2.1 + + + maven-surefire-plugin + 2.17 + + + + + + + diff --git a/java/performance/src/main/java/org/apache/arrow/performance/benchmark/micro/AllocatorBenchmarks.java b/java/performance/src/main/java/org/apache/arrow/performance/benchmark/micro/AllocatorBenchmarks.java new file mode 100644 index 00000000000..a66996216f8 --- /dev/null +++ b/java/performance/src/main/java/org/apache/arrow/performance/benchmark/micro/AllocatorBenchmarks.java @@ -0,0 +1,98 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.arrow.performance.benchmark.micro; + +import java.util.concurrent.TimeUnit; + +import org.apache.arrow.memory.AllocationListener; +import org.apache.arrow.memory.RootAllocator; +import org.apache.arrow.memory.rounding.RoundingPolicy; +import org.apache.arrow.memory.rounding.SegmentRoundingPolicy; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +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 io.netty.buffer.ArrowBuf; + +/** + * Benchmarks for allocators. + */ +public class AllocatorBenchmarks { + + /** + * Benchmark for the default allocator. + */ + @Benchmark + @BenchmarkMode(Mode.AverageTime) + @OutputTimeUnit(TimeUnit.MICROSECONDS) + public void defaultAllocatorBenchmark() { + final int bufferSize = 1024; + final int numBuffers = 1024; + + try (RootAllocator allocator = new RootAllocator(numBuffers * bufferSize)) { + ArrowBuf[] buffers = new ArrowBuf[numBuffers]; + + for (int i = 0; i < numBuffers; i++) { + buffers[i] = allocator.buffer(bufferSize); + } + + for (int i = 0; i < numBuffers; i++) { + buffers[i].close(); + } + } + } + + /** + * Benchmark for allocator with segment rounding policy. + */ + @Benchmark + @BenchmarkMode(Mode.AverageTime) + @OutputTimeUnit(TimeUnit.MICROSECONDS) + public void segmentRoundingPolicyBenchmark() { + final int bufferSize = 1024; + final int numBuffers = 1024; + final int segmentSize = 1024; + + RoundingPolicy policy = new SegmentRoundingPolicy(segmentSize); + try (RootAllocator allocator = new RootAllocator(AllocationListener.NOOP, bufferSize * numBuffers, policy)) { + ArrowBuf[] buffers = new ArrowBuf[numBuffers]; + + for (int i = 0; i < numBuffers; i++) { + buffers[i] = allocator.buffer(bufferSize); + } + + for (int i = 0; i < numBuffers; i++) { + buffers[i].close(); + } + } + } + + public static void main(String[] args) throws RunnerException { + Options opt = new OptionsBuilder() + .include(AllocatorBenchmarks.class.getSimpleName()) + .forks(1) + .build(); + + new Runner(opt).run(); + } +} diff --git a/java/performance/src/main/java/org/apache/arrow/performance/benchmark/micro/Float8Benchmarks.java b/java/performance/src/main/java/org/apache/arrow/performance/benchmark/micro/Float8Benchmarks.java new file mode 100644 index 00000000000..a9b4387853f --- /dev/null +++ b/java/performance/src/main/java/org/apache/arrow/performance/benchmark/micro/Float8Benchmarks.java @@ -0,0 +1,95 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.arrow.performance.benchmark.micro; + +import java.util.concurrent.TimeUnit; + +import org.apache.arrow.memory.BufferAllocator; +import org.apache.arrow.memory.RootAllocator; +import org.apache.arrow.vector.Float8Vector; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.TearDown; +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; + +/** + * Benchmarks for {@link Float8Vector}. + */ +@State(Scope.Benchmark) +public class Float8Benchmarks { + + private static final int VECTOR_LENGTH = 1024; + + private static final int ALLOCATOR_CAPACITY = 1024 * 1024; + + private BufferAllocator allocator; + + private Float8Vector vector; + + /** + * Setup benchmarks. + */ + @Setup + public void prepare() { + allocator = new RootAllocator(ALLOCATOR_CAPACITY); + vector = new Float8Vector("vector", allocator); + vector.allocateNew(VECTOR_LENGTH); + } + + /** + * Tear down benchmarks. + */ + @TearDown + public void tearDown() { + vector.close(); + allocator.close(); + } + + /** + * Test reading/writing on {@link Float8Vector}. + * @return useless. To avoid DCE by JIT. + */ + @Benchmark + @BenchmarkMode(Mode.AverageTime) + @OutputTimeUnit(TimeUnit.MICROSECONDS) + public double readWriteBenchmark() { + double sum = 0; + for (int i = 0; i < VECTOR_LENGTH; i++) { + vector.set(i, i + 10.0); + sum += vector.get(i); + } + return sum; + } + + public static void main(String[] args) throws RunnerException { + Options opt = new OptionsBuilder() + .include(Float8Benchmarks.class.getSimpleName()) + .forks(1) + .build(); + + new Runner(opt).run(); + } +} diff --git a/java/pom.xml b/java/pom.xml index ffa1503a02d..666162571bd 100644 --- a/java/pom.xml +++ b/java/pom.xml @@ -653,6 +653,7 @@ adapter/jdbc plasma flight + performance From fde0e0a21154afb0027ec654846b3637fd5558dc Mon Sep 17 00:00:00 2001 From: liyafan82 Date: Mon, 3 Jun 2019 11:18:20 +0800 Subject: [PATCH 2/2] [ARROW-5461][Java] Move benchmarks to tests --- .../org/apache/arrow/memory}/AllocatorBenchmarks.java | 8 ++++---- .../org/apache/arrow/vector}/Float8Benchmarks.java | 11 ++++++++--- 2 files changed, 12 insertions(+), 7 deletions(-) rename java/performance/src/{main/java/org/apache/arrow/performance/benchmark/micro => test/java/org/apache/arrow/memory}/AllocatorBenchmarks.java (93%) rename java/performance/src/{main/java/org/apache/arrow/performance/benchmark/micro => test/java/org/apache/arrow/vector}/Float8Benchmarks.java (86%) diff --git a/java/performance/src/main/java/org/apache/arrow/performance/benchmark/micro/AllocatorBenchmarks.java b/java/performance/src/test/java/org/apache/arrow/memory/AllocatorBenchmarks.java similarity index 93% rename from java/performance/src/main/java/org/apache/arrow/performance/benchmark/micro/AllocatorBenchmarks.java rename to java/performance/src/test/java/org/apache/arrow/memory/AllocatorBenchmarks.java index a66996216f8..81b1d19b3f9 100644 --- a/java/performance/src/main/java/org/apache/arrow/performance/benchmark/micro/AllocatorBenchmarks.java +++ b/java/performance/src/test/java/org/apache/arrow/memory/AllocatorBenchmarks.java @@ -15,14 +15,13 @@ * limitations under the License. */ -package org.apache.arrow.performance.benchmark.micro; +package org.apache.arrow.memory; import java.util.concurrent.TimeUnit; -import org.apache.arrow.memory.AllocationListener; -import org.apache.arrow.memory.RootAllocator; import org.apache.arrow.memory.rounding.RoundingPolicy; import org.apache.arrow.memory.rounding.SegmentRoundingPolicy; +import org.junit.Test; import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.BenchmarkMode; import org.openjdk.jmh.annotations.Mode; @@ -87,7 +86,8 @@ public void segmentRoundingPolicyBenchmark() { } } - public static void main(String[] args) throws RunnerException { + @Test + public void evaluate() throws RunnerException { Options opt = new OptionsBuilder() .include(AllocatorBenchmarks.class.getSimpleName()) .forks(1) diff --git a/java/performance/src/main/java/org/apache/arrow/performance/benchmark/micro/Float8Benchmarks.java b/java/performance/src/test/java/org/apache/arrow/vector/Float8Benchmarks.java similarity index 86% rename from java/performance/src/main/java/org/apache/arrow/performance/benchmark/micro/Float8Benchmarks.java rename to java/performance/src/test/java/org/apache/arrow/vector/Float8Benchmarks.java index a9b4387853f..9ab6e375eaf 100644 --- a/java/performance/src/main/java/org/apache/arrow/performance/benchmark/micro/Float8Benchmarks.java +++ b/java/performance/src/test/java/org/apache/arrow/vector/Float8Benchmarks.java @@ -15,13 +15,14 @@ * limitations under the License. */ -package org.apache.arrow.performance.benchmark.micro; +package org.apache.arrow.vector; import java.util.concurrent.TimeUnit; +import org.apache.arrow.memory.BoundsChecking; import org.apache.arrow.memory.BufferAllocator; import org.apache.arrow.memory.RootAllocator; -import org.apache.arrow.vector.Float8Vector; +import org.junit.Test; import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.BenchmarkMode; import org.openjdk.jmh.annotations.Mode; @@ -70,6 +71,9 @@ public void tearDown() { /** * Test reading/writing on {@link Float8Vector}. + * The performance of this benchmark is influenced by the states of two flags: + * 1. The flag for boundary checking. For details, please see {@link BoundsChecking}. + * 2. The flag for null checking in get methods. For details, please see {@link NullCheckingForGet}. * @return useless. To avoid DCE by JIT. */ @Benchmark @@ -84,7 +88,8 @@ public double readWriteBenchmark() { return sum; } - public static void main(String[] args) throws RunnerException { + @Test + public void evaluate() throws RunnerException { Options opt = new OptionsBuilder() .include(Float8Benchmarks.class.getSimpleName()) .forks(1)