-
Notifications
You must be signed in to change notification settings - Fork 4.1k
ARROW-6601: [Java] Improve JDBC adapter performance & add benchmark #5472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
java/performance/src/test/java/org/apache/arrow/adapter/JdbcAdapterBenchmarks.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| /* | ||
| * 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.adapter; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.DriverManager; | ||
| import java.sql.PreparedStatement; | ||
| import java.sql.ResultSet; | ||
| import java.sql.Statement; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import org.apache.arrow.adapter.jdbc.ArrowVectorIterator; | ||
| import org.apache.arrow.adapter.jdbc.JdbcToArrow; | ||
| import org.apache.arrow.adapter.jdbc.JdbcToArrowConfig; | ||
| import org.apache.arrow.adapter.jdbc.JdbcToArrowConfigBuilder; | ||
| import org.apache.arrow.memory.BaseAllocator; | ||
| import org.apache.arrow.memory.RootAllocator; | ||
| import org.apache.arrow.vector.IntVector; | ||
| import org.apache.arrow.vector.VectorSchemaRoot; | ||
| import org.junit.Test; | ||
| 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 avro adapter. | ||
| */ | ||
| @State(Scope.Benchmark) | ||
| public class JdbcAdapterBenchmarks { | ||
|
|
||
| private final int valueCount = 3000; | ||
| private JdbcToArrowConfig config; | ||
|
|
||
| private Connection conn = null; | ||
| private ResultSet resultSet = null; | ||
|
|
||
| private static final String CREATE_STATEMENT = | ||
| "CREATE TABLE test_table (f0 INT, f1 LONG, f2 VARCHAR, f3 BOOLEAN);"; | ||
| private static final String INSERT_STATEMENT = | ||
| "INSERT INTO test_table (f0, f1, f2, f3) VALUES (?, ?, ?, ?);"; | ||
| private static final String QUERY = "SELECT f0, f1, f2, f3 FROM test_table;"; | ||
| private static final String DROP_STATEMENT = "DROP TABLE test_table;"; | ||
|
|
||
| /** | ||
| * Setup benchmarks. | ||
| */ | ||
| @Setup | ||
| public void prepare() throws Exception { | ||
| BaseAllocator allocator = new RootAllocator(Integer.MAX_VALUE); | ||
| config = new JdbcToArrowConfigBuilder().setAllocator(allocator).setTargetBatchSize(1024).build(); | ||
|
|
||
| String url = "jdbc:h2:mem:JdbcAdapterBenchmarks"; | ||
| String driver = "org.h2.Driver"; | ||
| Class.forName(driver); | ||
| conn = DriverManager.getConnection(url); | ||
| try (Statement stmt = conn.createStatement()) { | ||
| stmt.executeUpdate(CREATE_STATEMENT); | ||
| } | ||
|
|
||
| for (int i = 0; i < valueCount; i++) { | ||
| // Insert data | ||
| try (PreparedStatement stmt = conn.prepareStatement(INSERT_STATEMENT)) { | ||
|
|
||
| stmt.setInt(1, i); | ||
| stmt.setLong(2, i); | ||
| stmt.setString(3, "test" + i); | ||
| stmt.setBoolean(4, i % 2 == 0); | ||
| stmt.executeUpdate(); | ||
| } | ||
| } | ||
|
|
||
| resultSet = conn.createStatement().executeQuery(QUERY); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Tear down benchmarks. | ||
| */ | ||
| @TearDown | ||
| public void tearDown() throws Exception { | ||
| try (Statement stmt = conn.createStatement()) { | ||
| stmt.executeUpdate(DROP_STATEMENT); | ||
| } finally { | ||
| if (conn != null) { | ||
| conn.close(); | ||
| conn = null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Test {@link JdbcToArrow#sqlToArrowVectorIterator(ResultSet, JdbcToArrowConfig)} | ||
| * @return useless. To avoid DCE by JIT. | ||
| */ | ||
| @Benchmark | ||
| @BenchmarkMode(Mode.AverageTime) | ||
| @OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
| public int testJdbcToArrow() throws Exception { | ||
| int valueCount = 0; | ||
| try (ArrowVectorIterator iter = JdbcToArrow.sqlToArrowVectorIterator(resultSet, config)) { | ||
| while (iter.hasNext()) { | ||
| VectorSchemaRoot root = iter.next(); | ||
| IntVector intVector = (IntVector) root.getFieldVectors().get(0); | ||
| valueCount += intVector.getValueCount(); | ||
| root.close(); | ||
| } | ||
| } | ||
| return valueCount; | ||
| } | ||
|
|
||
| @Test | ||
| public void evaluate() throws RunnerException { | ||
| Options opt = new OptionsBuilder() | ||
| .include(JdbcAdapterBenchmarks.class.getSimpleName()) | ||
| .forks(1) | ||
| .build(); | ||
|
|
||
| new Runner(opt).run(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you clarify why this is necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we remove JdbcToArrowUtils#allocateVectors, the vector is initialized with capacity 0, and when we invoke vector.getOffsetBuffer.get(index *4) it will throw Exception.
And the vector passed in may be null from iterator API, so add a null check also.