Skip to content

Commit 892599e

Browse files
authored
Merge pull request #2242 from ClickHouse/add-jdbc-benchmarking
Add JDBC Insert flow for Benchmarking
2 parents db9fbe1 + 93962d7 commit 892599e

File tree

4 files changed

+152
-9
lines changed

4 files changed

+152
-9
lines changed

performance/src/test/com/clickhouse/benchmark/BenchmarkRunner.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.clickhouse.benchmark.clients.ConcurrentQueryClient;
66
import com.clickhouse.benchmark.clients.Deserializers;
77
import com.clickhouse.benchmark.clients.InsertClient;
8+
import com.clickhouse.benchmark.clients.JDBCInsert;
9+
import com.clickhouse.benchmark.clients.JDBCQuery;
810
import com.clickhouse.benchmark.clients.MixedWorkload;
911
import com.clickhouse.benchmark.clients.QueryClient;
1012
import com.clickhouse.benchmark.clients.Serializers;
@@ -36,14 +38,16 @@ public static void main(String[] args) throws Exception {
3638
Map<String, String> argMap = parseArguments(args);
3739

3840
Options opt = new OptionsBuilder()
39-
.include(QueryClient.class.getSimpleName())
40-
.include(InsertClient.class.getSimpleName())
41-
.include(ConcurrentInsertClient.class.getSimpleName())
42-
.include(ConcurrentQueryClient.class.getSimpleName())
43-
.include(Compression.class.getSimpleName())
44-
.include(Serializers.class.getSimpleName())
45-
.include(Deserializers.class.getSimpleName())
46-
.include(MixedWorkload.class.getSimpleName())
41+
.include(QueryClient.class.getName())
42+
.include(InsertClient.class.getName())
43+
.include(ConcurrentInsertClient.class.getName())
44+
.include(ConcurrentQueryClient.class.getName())
45+
.include(Compression.class.getName())
46+
.include(Serializers.class.getName())
47+
.include(Deserializers.class.getName())
48+
.include(MixedWorkload.class.getName())
49+
.include(JDBCQuery.class.getName())
50+
.include(JDBCInsert.class.getName())
4751
.forks(1) // must be a fork. No fork only for debugging
4852
.mode(Mode.SampleTime)
4953
.timeUnit(TimeUnit.MILLISECONDS)

performance/src/test/com/clickhouse/benchmark/clients/BenchmarkBase.java

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.clickhouse.client.ClickHouseNodeSelector;
1212
import com.clickhouse.client.ClickHouseProtocol;
1313
import com.clickhouse.client.ClickHouseResponse;
14+
import com.clickhouse.client.ClickHouseServerForTest;
1415
import com.clickhouse.client.api.Client;
1516
import com.clickhouse.client.api.ConnectionReuseStrategy;
1617
import com.clickhouse.client.api.enums.Protocol;
@@ -22,6 +23,7 @@
2223
import com.clickhouse.data.ClickHouseOutputStream;
2324
import com.clickhouse.data.ClickHouseRecord;
2425
import com.clickhouse.data.format.ClickHouseRowBinaryProcessor;
26+
import com.clickhouse.jdbc.ClickHouseDriver;
2527
import org.openjdk.jmh.annotations.Level;
2628
import org.openjdk.jmh.annotations.Param;
2729
import org.openjdk.jmh.annotations.Scope;
@@ -35,9 +37,12 @@
3537
import java.io.InputStream;
3638
import java.math.BigInteger;
3739
import java.nio.ByteBuffer;
40+
import java.sql.Connection;
41+
import java.sql.SQLException;
3842
import java.util.ArrayList;
3943
import java.util.Collections;
4044
import java.util.List;
45+
import java.util.Properties;
4146

4247
import static com.clickhouse.benchmark.BenchmarkRunner.getSelectCountQuery;
4348
import static com.clickhouse.benchmark.BenchmarkRunner.getSyncQuery;
@@ -52,14 +57,18 @@
5257
@State(Scope.Benchmark)
5358
public class BenchmarkBase {
5459
private static final Logger LOGGER = LoggerFactory.getLogger(BenchmarkBase.class);
55-
5660
protected ClickHouseClient clientV1;
5761
protected Client clientV2;
62+
protected static Connection jdbcV1;
63+
protected static Connection jdbcV2;
64+
5865
@Setup(Level.Iteration)
5966
public void setUpIteration() {
6067
LOGGER.info("BenchmarkBase::setUpIteration");
6168
clientV1 = getClientV1();
6269
clientV2 = getClientV2();
70+
jdbcV1 = getJdbcV1();
71+
jdbcV2 = getJdbcV2();
6372
}
6473

6574
@TearDown(Level.Iteration)
@@ -73,6 +82,22 @@ public void tearDownIteration() {
7382
clientV2.close();
7483
clientV2 = null;
7584
}
85+
if (jdbcV1 != null) {
86+
try {
87+
jdbcV1.close();
88+
} catch (SQLException e) {
89+
LOGGER.error(e.getMessage());
90+
}
91+
jdbcV1 = null;
92+
}
93+
if (jdbcV2 != null) {
94+
try {
95+
jdbcV2.close();
96+
} catch (SQLException e) {
97+
LOGGER.error(e.getMessage());
98+
}
99+
jdbcV2 = null;
100+
}
76101
}
77102

78103
@State(Scope.Benchmark)
@@ -240,6 +265,37 @@ protected static Client getClientV2(boolean includeDb) {
240265
.setDefaultDatabase(includeDb ? DB_NAME : "default")
241266
.build();
242267
}
268+
269+
protected static Connection getJdbcV1() {
270+
Properties properties = new Properties();
271+
properties.put("user", getUsername());
272+
properties.put("password", getPassword());
273+
274+
ClickHouseNode node = getServer();
275+
Connection jdbcV1 = null;
276+
try {
277+
jdbcV1 = new ClickHouseDriver().connect(String.format("jdbc:clickhouse://%s:%s?clickhouse.jdbc.v1=true", node.getHost(), node.getPort()), properties);
278+
} catch (SQLException e) {
279+
LOGGER.error(e.getMessage());
280+
}
281+
return jdbcV1;
282+
}
283+
284+
protected static Connection getJdbcV2() {
285+
Properties properties = new Properties();
286+
properties.put("user", getUsername());
287+
properties.put("password", getPassword());
288+
289+
ClickHouseNode node = getServer();
290+
Connection jdbcV2 = null;
291+
try {
292+
jdbcV2 = new ClickHouseDriver().connect(String.format("jdbc:clickhouse://%s:%s", node.getHost(), node.getPort()), properties);
293+
} catch (SQLException e) {
294+
LOGGER.error(e.getMessage());
295+
}
296+
297+
return jdbcV2;
298+
}
243299

244300
public static void loadClickHouseRecords(DataState dataState) {
245301
syncQuery(dataState.tableNameFilled);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.clickhouse.benchmark.clients;
2+
3+
import org.openjdk.jmh.annotations.Benchmark;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import java.sql.Connection;
8+
import java.sql.PreparedStatement;
9+
import java.sql.SQLException;
10+
import java.util.List;
11+
import java.util.stream.Collectors;
12+
13+
import static com.clickhouse.benchmark.TestEnvironment.DB_NAME;
14+
15+
public class JDBCInsert extends BenchmarkBase {
16+
private static final Logger LOGGER = LoggerFactory.getLogger(JDBCInsert.class);
17+
void insetData(Connection connection, DataState dataState) throws SQLException {
18+
int size = dataState.dataSet.getSchema().getColumns().size();
19+
String names = dataState.dataSet.getSchema().getColumns().stream().map(column -> column.getColumnName()).collect(Collectors.joining(","));
20+
String values = dataState.dataSet.getSchema().getColumns().stream().map(column -> "?").collect(Collectors.joining(","));
21+
String sql = String.format("INSERT INTO `%s`.`%s` (%s) VALUES (%s)", DB_NAME ,dataState.tableNameEmpty, names, values);
22+
LOGGER.info("SQL: " + sql);
23+
PreparedStatement preparedStatement = connection.prepareStatement(sql);
24+
for (List<Object> data : dataState.dataSet.getRowsOrdered()) {
25+
for (int j = 0; j < size; j++) {
26+
preparedStatement.setObject(j + 1, data.get(j));
27+
}
28+
preparedStatement.addBatch();
29+
}
30+
preparedStatement.executeBatch();
31+
}
32+
33+
@Benchmark
34+
public void insertJDBCV1(DataState dataState) throws SQLException {
35+
insetData(jdbcV1, dataState);
36+
}
37+
38+
@Benchmark
39+
public void insertJDBCV2(DataState dataState) throws SQLException {
40+
insetData(jdbcV2, dataState);
41+
}
42+
43+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.clickhouse.benchmark.clients;
2+
3+
import org.openjdk.jmh.annotations.Benchmark;
4+
import org.openjdk.jmh.infra.Blackhole;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
import java.sql.Connection;
9+
import java.sql.ResultSet;
10+
import java.sql.SQLException;
11+
import java.sql.Statement;
12+
13+
import static com.clickhouse.benchmark.BenchmarkRunner.getSelectQuery;
14+
15+
public class JDBCQuery extends BenchmarkBase {
16+
private static final Logger LOGGER = LoggerFactory.getLogger(JDBCQuery.class);
17+
void selectData(Connection connection, DataState dataState, Blackhole blackhole) throws SQLException {
18+
String sql = getSelectQuery(dataState.tableNameFilled);
19+
try (Statement stmt = connection.createStatement()) {
20+
try (ResultSet rs = stmt.executeQuery(sql)) {
21+
while (rs.next()) {
22+
for (int i = 1; i <= dataState.dataSet.getSchema().getColumns().size(); i++) {
23+
blackhole.consume(rs.getObject(i));
24+
}
25+
}
26+
}
27+
}
28+
}
29+
30+
@Benchmark
31+
public void selectJDBCV1(DataState dataState, Blackhole blackhole) throws SQLException {
32+
selectData(jdbcV1, dataState, blackhole);
33+
}
34+
35+
@Benchmark
36+
public void selectJDBCV2(DataState dataState, Blackhole blackhole) throws SQLException {
37+
selectData(jdbcV2, dataState, blackhole);
38+
}
39+
40+
}

0 commit comments

Comments
 (0)