|
| 1 | +/* |
| 2 | + * Copyright 2026 Bloomberg Finance L.P. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.bloomberg.selekt.jdbc.benchmarks; |
| 18 | + |
| 19 | +import com.bloomberg.selekt.jdbc.driver.SelektDriver; |
| 20 | +import java.io.File; |
| 21 | +import java.io.IOException; |
| 22 | +import java.nio.file.Files; |
| 23 | +import java.sql.Connection; |
| 24 | +import java.sql.Driver; |
| 25 | +import java.sql.PreparedStatement; |
| 26 | +import java.sql.ResultSet; |
| 27 | +import java.sql.SQLException; |
| 28 | +import java.sql.Statement; |
| 29 | +import java.util.Properties; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | +import org.openjdk.jmh.annotations.Benchmark; |
| 32 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 33 | +import org.openjdk.jmh.annotations.Fork; |
| 34 | +import org.openjdk.jmh.annotations.Level; |
| 35 | +import org.openjdk.jmh.annotations.Measurement; |
| 36 | +import org.openjdk.jmh.annotations.Mode; |
| 37 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 38 | +import org.openjdk.jmh.annotations.Param; |
| 39 | +import org.openjdk.jmh.annotations.Scope; |
| 40 | +import org.openjdk.jmh.annotations.Setup; |
| 41 | +import org.openjdk.jmh.annotations.State; |
| 42 | +import org.openjdk.jmh.annotations.TearDown; |
| 43 | +import org.openjdk.jmh.annotations.Warmup; |
| 44 | +import org.openjdk.jmh.infra.Blackhole; |
| 45 | + |
| 46 | +@BenchmarkMode(Mode.AverageTime) |
| 47 | +@OutputTimeUnit(TimeUnit.MICROSECONDS) |
| 48 | +@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) |
| 49 | +@Measurement(iterations = 7, time = 1, timeUnit = TimeUnit.SECONDS) |
| 50 | +@Fork(1) |
| 51 | +@State(Scope.Thread) |
| 52 | +public class JdbcColumnLabelBenchmark { |
| 53 | + private static final int ROW_COUNT = 1_000; |
| 54 | + private static final Driver DRIVER = new SelektDriver(); |
| 55 | + |
| 56 | + @Param({"4", "16", "64", "256"}) |
| 57 | + int columnCount; |
| 58 | + |
| 59 | + private File databaseFile; |
| 60 | + private Connection connection; |
| 61 | + private PreparedStatement query; |
| 62 | + private String[] labels; |
| 63 | + |
| 64 | + @Setup(Level.Trial) |
| 65 | + public void setUp() throws IOException, SQLException { |
| 66 | + databaseFile = Files.createTempFile("selekt-column-label-bench", ".db").toFile(); |
| 67 | + databaseFile.deleteOnExit(); |
| 68 | + connection = DRIVER.connect("jdbc:sqlite:" + databaseFile.getAbsolutePath(), new Properties()); |
| 69 | + try (Statement statement = connection.createStatement()) { |
| 70 | + statement.execute("CREATE TABLE bench (value INTEGER NOT NULL)"); |
| 71 | + } |
| 72 | + connection.setAutoCommit(false); |
| 73 | + try (PreparedStatement insert = connection.prepareStatement("INSERT INTO bench VALUES (?)")) { |
| 74 | + for (int row = 0; row < ROW_COUNT; ++row) { |
| 75 | + insert.setInt(1, row); |
| 76 | + insert.addBatch(); |
| 77 | + } |
| 78 | + insert.executeBatch(); |
| 79 | + connection.commit(); |
| 80 | + } finally { |
| 81 | + connection.setAutoCommit(true); |
| 82 | + } |
| 83 | + |
| 84 | + labels = new String[columnCount]; |
| 85 | + final StringBuilder sql = new StringBuilder("SELECT "); |
| 86 | + for (int column = 0; column < columnCount; ++column) { |
| 87 | + if (column != 0) { |
| 88 | + sql.append(','); |
| 89 | + } |
| 90 | + labels[column] = "column_" + column; |
| 91 | + sql.append("value AS ").append(labels[column]); |
| 92 | + } |
| 93 | + query = connection.prepareStatement(sql.append(" FROM bench").toString()); |
| 94 | + } |
| 95 | + |
| 96 | + @TearDown(Level.Trial) |
| 97 | + public void tearDown() throws SQLException { |
| 98 | + if (query != null) { |
| 99 | + query.close(); |
| 100 | + } |
| 101 | + if (connection != null) { |
| 102 | + connection.close(); |
| 103 | + } |
| 104 | + if (databaseFile != null) { |
| 105 | + databaseFile.delete(); |
| 106 | + new File(databaseFile.getPath() + "-journal").delete(); |
| 107 | + new File(databaseFile.getPath() + "-wal").delete(); |
| 108 | + new File(databaseFile.getPath() + "-shm").delete(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Benchmark |
| 113 | + public void byIndex(final Blackhole blackhole) throws SQLException { |
| 114 | + try (ResultSet resultSet = query.executeQuery()) { |
| 115 | + while (resultSet.next()) { |
| 116 | + for (int column = 1; column <= columnCount; ++column) { |
| 117 | + blackhole.consume(resultSet.getInt(column)); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Benchmark |
| 124 | + public void byLabel(final Blackhole blackhole) throws SQLException { |
| 125 | + try (ResultSet resultSet = query.executeQuery()) { |
| 126 | + while (resultSet.next()) { |
| 127 | + for (int column = 0; column < columnCount; ++column) { |
| 128 | + blackhole.consume(resultSet.getInt(labels[column])); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments