Skip to content

Commit d44eec2

Browse files
mzitnikPaultagoras
authored andcommitted
Add JDBC Insert flow for Benchmarking
1 parent 98a4c11 commit d44eec2

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.clickhouse.data.ClickHouseOutputStream;
2323
import com.clickhouse.data.ClickHouseRecord;
2424
import com.clickhouse.data.format.ClickHouseRowBinaryProcessor;
25+
import com.clickhouse.jdbc.ClickHouseDriver;
2526
import org.openjdk.jmh.annotations.Level;
2627
import org.openjdk.jmh.annotations.Param;
2728
import org.openjdk.jmh.annotations.Scope;
@@ -35,9 +36,12 @@
3536
import java.io.InputStream;
3637
import java.math.BigInteger;
3738
import java.nio.ByteBuffer;
39+
import java.sql.Connection;
40+
import java.sql.SQLException;
3841
import java.util.ArrayList;
3942
import java.util.Collections;
4043
import java.util.List;
44+
import java.util.Properties;
4145

4246
import static com.clickhouse.benchmark.BenchmarkRunner.getSelectCountQuery;
4347
import static com.clickhouse.benchmark.BenchmarkRunner.getSyncQuery;
@@ -55,15 +59,20 @@ public class BenchmarkBase {
5559

5660
protected ClickHouseClient clientV1;
5761
protected Client clientV2;
62+
protected static Connection jdbcV1 = null;
63+
protected static Connection jdbcV2 = null;
64+
5865
@Setup(Level.Iteration)
59-
public void setUpIteration() {
66+
public void setUpIteration() throws SQLException {
6067
LOGGER.info("BenchmarkBase::setUpIteration");
6168
clientV1 = getClientV1();
6269
clientV2 = getClientV2();
70+
jdbcV1 = getJdbcV1();
71+
jdbcV2 = getJdbcV2();
6372
}
6473

6574
@TearDown(Level.Iteration)
66-
public void tearDownIteration() {
75+
public void tearDownIteration() throws SQLException {
6776
LOGGER.info("BenchmarkBase::tearDownIteration");
6877
if (clientV1 != null) {
6978
clientV1.close();
@@ -73,6 +82,14 @@ public void tearDownIteration() {
7382
clientV2.close();
7483
clientV2 = null;
7584
}
85+
if (jdbcV1 != null) {
86+
jdbcV1.close();
87+
jdbcV1 = null;
88+
}
89+
if (jdbcV2 != null) {
90+
jdbcV2.close();
91+
jdbcV2 = null;
92+
}
7693
}
7794

7895
@State(Scope.Benchmark)
@@ -241,6 +258,29 @@ protected static Client getClientV2(boolean includeDb) {
241258
.build();
242259
}
243260

261+
protected static Connection getJdbcV1() throws SQLException {
262+
Properties properties = new Properties();
263+
properties.put("user", getUsername());
264+
properties.put("password", getPassword());
265+
266+
ClickHouseNode node = getServer();
267+
LOGGER.info(String.format("clickhouse endpoint [%s:%s]", node.getHost(), node.getPort()));
268+
Connection jdbcV1 = new ClickHouseDriver().connect(String.format("jdbc:clickhouse://%s:%s?clickhouse.jdbc.v1=true", node.getHost(), node.getPort()), properties);
269+
return jdbcV1;
270+
}
271+
272+
protected static Connection getJdbcV2() throws SQLException {
273+
Properties properties = new Properties();
274+
properties.put("user", getUsername());
275+
properties.put("password", getPassword());
276+
277+
ClickHouseNode node = getServer();
278+
LOGGER.info(String.format("clickhouse endpoint [%s:%s]", node.getHost(), node.getPort()));
279+
280+
Connection jdbcV2 = new ClickHouseDriver().connect(String.format("jdbc:clickhouse://%s:%s", node.getHost(), node.getPort()), properties);
281+
return jdbcV2;
282+
}
283+
244284
public static void loadClickHouseRecords(DataState dataState) {
245285
syncQuery(dataState.tableNameFilled);
246286

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)