Skip to content

Commit 779701e

Browse files
committed
Add JDBC Insert flow for Benchmarking
1 parent 0ea28d4 commit 779701e

File tree

2 files changed

+83
-4
lines changed

2 files changed

+83
-4
lines changed

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

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.clickhouse.client.ClickHouseNode;
1010
import com.clickhouse.client.ClickHouseProtocol;
1111
import com.clickhouse.client.ClickHouseResponse;
12+
import com.clickhouse.client.ClickHouseServerForTest;
1213
import com.clickhouse.client.api.Client;
1314
import com.clickhouse.client.api.enums.Protocol;
1415
import com.clickhouse.client.api.insert.InsertResponse;
@@ -18,6 +19,7 @@
1819
import com.clickhouse.data.ClickHouseOutputStream;
1920
import com.clickhouse.data.ClickHouseRecord;
2021
import com.clickhouse.data.format.ClickHouseRowBinaryProcessor;
22+
import com.clickhouse.jdbc.ClickHouseDriver;
2123
import org.openjdk.jmh.annotations.Level;
2224
import org.openjdk.jmh.annotations.Param;
2325
import org.openjdk.jmh.annotations.Scope;
@@ -31,9 +33,12 @@
3133
import java.io.InputStream;
3234
import java.math.BigInteger;
3335
import java.nio.ByteBuffer;
36+
import java.sql.Connection;
37+
import java.sql.SQLException;
3438
import java.util.ArrayList;
3539
import java.util.Collections;
3640
import java.util.List;
41+
import java.util.Properties;
3742

3843
import static com.clickhouse.benchmark.BenchmarkRunner.getSelectCountQuery;
3944
import static com.clickhouse.benchmark.BenchmarkRunner.getSyncQuery;
@@ -48,18 +53,21 @@
4853
@State(Scope.Benchmark)
4954
public class BenchmarkBase {
5055
private static final Logger LOGGER = LoggerFactory.getLogger(BenchmarkBase.class);
51-
5256
protected ClickHouseClient clientV1;
5357
protected Client clientV2;
58+
protected static Connection jdbcV1 = null;
59+
protected static Connection jdbcV2 = null;
60+
5461
@Setup(Level.Iteration)
55-
public void setUpIteration() {
62+
public void setUpIteration() throws SQLException {
5663
clientV1 = getClientV1();
5764
clientV2 = getClientV2();
58-
65+
jdbcV1 = getJdbcV1();
66+
jdbcV2 = getJdbcV2();
5967
}
6068

6169
@TearDown(Level.Iteration)
62-
public void tearDownIteration() {
70+
public void tearDownIteration() throws SQLException {
6371
if (clientV1 != null) {
6472
clientV1.close();
6573
clientV1 = null;
@@ -68,6 +76,14 @@ public void tearDownIteration() {
6876
clientV2.close();
6977
clientV2 = null;
7078
}
79+
if (jdbcV1 != null) {
80+
jdbcV1.close();
81+
jdbcV1 = null;
82+
}
83+
if (jdbcV2 != null) {
84+
jdbcV2.close();
85+
jdbcV2 = null;
86+
}
7187
}
7288

7389
@State(Scope.Benchmark)
@@ -224,6 +240,28 @@ protected static Client getClientV2(boolean includeDb) {
224240
.build();
225241
}
226242

243+
protected static Connection getJdbcV1() throws SQLException {
244+
Properties properties = new Properties();
245+
properties.put("user", getUsername());
246+
properties.put("password", getPassword());
247+
248+
ClickHouseNode node = ClickHouseServerForTest.getClickHouseNode(ClickHouseProtocol.HTTP, ClickHouseServerForTest.isCloud(), ClickHouseNode.builder().build());
249+
LOGGER.info(String.format("clickhouse endpoint [%s:%s]", node.getHost(), node.getPort()));
250+
Connection jdbcV1 = new ClickHouseDriver().connect(String.format("jdbc:clickhouse://%s:%s?clickhouse.jdbc.v1=true", node.getHost(), node.getPort()), properties);
251+
return jdbcV1;
252+
}
253+
protected static Connection getJdbcV2() throws SQLException {
254+
Properties properties = new Properties();
255+
properties.put("user", getUsername());
256+
properties.put("password", getPassword());
257+
258+
ClickHouseNode node = ClickHouseServerForTest.getClickHouseNode(ClickHouseProtocol.HTTP, ClickHouseServerForTest.isCloud(), ClickHouseNode.builder().build());
259+
LOGGER.info(String.format("clickhouse endpoint [%s:%s]", node.getHost(), node.getPort()));
260+
261+
Connection jdbcV2 = new ClickHouseDriver().connect(String.format("jdbc:clickhouse://%s:%s", node.getHost(), node.getPort()), properties);
262+
return jdbcV2;
263+
}
264+
227265
public static void loadClickHouseRecords(DataState dataState) {
228266
syncQuery(dataState.tableNameFilled);
229267

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
public class JDBCInsert extends BenchmarkBase {
14+
private static final Logger LOGGER = LoggerFactory.getLogger(JDBCInsert.class);
15+
void insetData(Connection connection, DataState dataState) throws SQLException {
16+
int size = dataState.dataSet.getSchema().getColumns().size();
17+
String names = dataState.dataSet.getSchema().getColumns().stream().map(column -> column.getColumnName()).collect(Collectors.joining(","));
18+
String values = dataState.dataSet.getSchema().getColumns().stream().map(column -> "?").collect(Collectors.joining(","));
19+
String sql = String.format("INSERT INTO %s (%s) VALUES (%s)", dataState.tableNameEmpty, names, values);
20+
LOGGER.info("SQL: " + sql);
21+
PreparedStatement preparedStatement = connection.prepareStatement(sql);
22+
for (List<Object> data : dataState.dataSet.getRowsOrdered()) {
23+
for (int j = 0; j < size; j++) {
24+
preparedStatement.setObject(j + 1, data.get(j));
25+
}
26+
preparedStatement.addBatch();
27+
}
28+
preparedStatement.executeBatch();
29+
}
30+
31+
@Benchmark
32+
public void insertJDBCV1(DataState dataState) throws SQLException {
33+
insetData(jdbcV1, dataState);
34+
}
35+
36+
@Benchmark
37+
public void insertJDBCV2(DataState dataState) throws SQLException {
38+
insetData(jdbcV2, dataState);
39+
}
40+
41+
}

0 commit comments

Comments
 (0)