Skip to content

Commit 33df298

Browse files
authored
Merge branch 'main' into add-logging
2 parents fe3439b + a6b069a commit 33df298

File tree

13 files changed

+302
-226
lines changed

13 files changed

+302
-226
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,5 @@ profile.html
5555
**/test/resources/test.properties
5656
performance/jmh-simple-results.json
5757
*.csv
58+
*.sql
59+
*.json

performance/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@
4747
<artifactId>testng</artifactId>
4848
<version>${testng.version}</version>
4949
</dependency>
50+
<!-- https://mvnrepository.com/artifact/org.testcontainers/clickhouse -->
51+
<dependency>
52+
<groupId>org.testcontainers</groupId>
53+
<artifactId>clickhouse</artifactId>
54+
<version>${testcontainers.version}</version>
55+
</dependency>
56+
5057

5158
<!-- Performance Test Dependencies -->
5259
<dependency>

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package com.clickhouse.benchmark;
22

3-
4-
import com.clickhouse.benchmark.clients.BenchmarkBase;
53
import com.clickhouse.benchmark.clients.InsertClient;
64
import com.clickhouse.benchmark.clients.QueryClient;
7-
import com.clickhouse.benchmark.data.DataSet;
85
import org.openjdk.jmh.annotations.Mode;
96
import org.openjdk.jmh.profile.GCProfiler;
107
import org.openjdk.jmh.profile.MemPoolProfiler;
@@ -20,6 +17,8 @@
2017
import java.util.Map;
2118
import java.util.concurrent.TimeUnit;
2219

20+
import static com.clickhouse.benchmark.TestEnvironment.DB_NAME;
21+
import static com.clickhouse.benchmark.TestEnvironment.isCloud;
2322

2423

2524
public class BenchmarkRunner {
@@ -28,9 +27,8 @@ public static void main(String[] args) throws Exception {
2827
LOGGER.info("Starting Benchmarks");
2928
Map<String, String> argMap = parseArguments(args);
3029

31-
3230
Options opt = new OptionsBuilder()
33-
.param("datasetSourceName", argMap.getOrDefault("dataset", "simple"))
31+
// .param("datasetSourceName", argMap.getOrDefault("dataset", "simple"))
3432
.include(QueryClient.class.getSimpleName())
3533
.include(InsertClient.class.getSimpleName())
3634
.forks(1) // must be a fork. No fork only for debugging
@@ -41,11 +39,11 @@ public static void main(String[] args) throws Exception {
4139
.addProfiler(MemPoolProfiler.class)
4240
.warmupIterations(3)
4341
.warmupTime(TimeValue.seconds(10))
44-
.measurementIterations(5)
42+
.measurementIterations(10)
4543
.jvmArgs("-Xms8g", "-Xmx8g")
4644
.measurementTime(TimeValue.seconds(10))
4745
.resultFormat(ResultFormatType.JSON)
48-
.result("jmh-simple-results.json")
46+
.result(String.format("jmh-results-%s-%s.json", isCloud() ? "cloud" : "local", System.currentTimeMillis()))
4947
.build();
5048

5149
new Runner(opt).run();
@@ -68,15 +66,19 @@ public static Map<String, String> parseArguments(String[] args) {
6866
return argMap;
6967
}
7068

71-
public static String getSelectQuery(DataSet dataSet) {
72-
return "SELECT * FROM `" + BenchmarkBase.DB_NAME + "`.`" + dataSet.getTableName() + "`";
69+
public static String getSelectQuery(String tableName) {
70+
return "SELECT * FROM `" + DB_NAME + "`.`" + tableName + "`";
71+
}
72+
73+
public static String getSelectCountQuery(String tableName) {
74+
return String.format("SELECT COUNT(*) FROM `%s`.`%s`", DB_NAME, tableName);
7375
}
7476

75-
public static String getSelectCountQuery(DataSet dataSet) {
76-
return "SELECT COUNT(*) FROM `" + BenchmarkBase.DB_NAME + "`.`" + dataSet.getTableName() + "`";
77+
public static String getInsertQuery(String tableName) {
78+
return String.format("INSERT INTO `%s`.`%s`", DB_NAME, tableName);
7779
}
7880

79-
public static String getInsertQuery(DataSet dataSet) {
80-
return "INSERT INTO `" + BenchmarkBase.DB_NAME + "`.`" + dataSet.getTableName() + "`";
81+
public static String getSyncQuery(String tableName) {
82+
return String.format("SYSTEM SYNC REPLICA `%s`.`%s`", DB_NAME, tableName);
8183
}
8284
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package com.clickhouse.benchmark;
2+
3+
import com.clickhouse.client.ClickHouseCredentials;
4+
import com.clickhouse.client.ClickHouseNode;
5+
import com.clickhouse.client.ClickHouseProtocol;
6+
import com.clickhouse.client.ClickHouseServerForTest;
7+
import com.clickhouse.client.config.ClickHouseClientOption;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
import org.testcontainers.clickhouse.ClickHouseContainer;
11+
import org.testcontainers.containers.wait.strategy.Wait;
12+
13+
import java.net.InetSocketAddress;
14+
import java.time.Duration;
15+
import java.util.Collections;
16+
17+
import static com.clickhouse.benchmark.clients.BenchmarkBase.runQuery;
18+
import static java.time.temporal.ChronoUnit.SECONDS;
19+
20+
public class TestEnvironment {
21+
private static final Logger LOGGER = LoggerFactory.getLogger(TestEnvironment.class);
22+
public static final String DB_NAME = "jmh_benchmarks_" + System.currentTimeMillis();
23+
private static final String CLICKHOUSE_DOCKER_IMAGE = "clickhouse/clickhouse-server:latest";
24+
private static ClickHouseNode serverNode;
25+
private static ClickHouseContainer container;
26+
27+
28+
//Environment Variables
29+
public static boolean isCloud() {
30+
return System.getenv("CLICKHOUSE_HOST") != null;
31+
}
32+
public static String getHost() {
33+
String host = System.getenv("CLICKHOUSE_HOST");
34+
if (host == null) {
35+
host = container.getHost();
36+
}
37+
38+
return host;
39+
}
40+
public static int getPort() {
41+
String port = System.getenv("CLICKHOUSE_PORT");
42+
if (port == null) {
43+
if (isCloud()) {//Default handling for ClickHouse Cloud
44+
port = "8443";
45+
} else {
46+
port = String.valueOf(container.getMappedPort(8123));
47+
}
48+
}
49+
50+
return Integer.parseInt(port);
51+
}
52+
public static String getPassword() {
53+
String password = System.getenv("CLICKHOUSE_PASSWORD");
54+
if (password == null) {
55+
if (isCloud()) {
56+
password = System.getenv("CLICKHOUSE_PASSWORD");
57+
} else {
58+
password = container.getPassword();
59+
}
60+
}
61+
return password;
62+
}
63+
public static String getUsername() {
64+
String username = System.getenv("CLICKHOUSE_USERNAME");
65+
if (username == null) {
66+
if (isCloud()) {
67+
username = "default";
68+
} else {
69+
username = container.getUsername();
70+
}
71+
}
72+
return username;
73+
}
74+
public static ClickHouseNode getServer() {
75+
return serverNode;
76+
}
77+
78+
79+
//Initialization and Teardown methods
80+
public static void setupEnvironment() {
81+
LOGGER.info("Initializing ClickHouse test environment...");
82+
83+
if (isCloud()) {
84+
LOGGER.info("Using ClickHouse Cloud");
85+
container = null;
86+
} else {
87+
LOGGER.info("Using ClickHouse Docker container");
88+
container = new ClickHouseContainer(CLICKHOUSE_DOCKER_IMAGE)
89+
.withPassword("testing_password")
90+
.withEnv("CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT", "1")
91+
.withExposedPorts(8123, 8443)
92+
.waitingFor(Wait.forHttp("/ping").forPort(8123).forStatusCode(200).withStartupTimeout(Duration.of(600, SECONDS)));
93+
container.start();
94+
}
95+
96+
serverNode = ClickHouseNode.builder(ClickHouseNode.builder().build())
97+
.address(ClickHouseProtocol.HTTP, new InetSocketAddress(getHost(), getPort()))
98+
.credentials(ClickHouseCredentials.fromUserAndPassword(getUsername(), getPassword()))
99+
.options(Collections.singletonMap(ClickHouseClientOption.SSL.getKey(), isCloud() ? "true" : "false"))
100+
.database(DB_NAME)
101+
.build();
102+
103+
createDatabase();
104+
}
105+
106+
public static void cleanupEnvironment() {
107+
if (isCloud()) {
108+
dropDatabase();
109+
}
110+
111+
if (container != null && container.isRunning()) {
112+
container.stop();
113+
container = null;
114+
}
115+
}
116+
117+
public static void createDatabase() {
118+
LOGGER.info("Creating database: {}", DB_NAME);
119+
runQuery(String.format("CREATE DATABASE IF NOT EXISTS %s", DB_NAME), false);
120+
}
121+
122+
public static void dropDatabase() {
123+
LOGGER.info("Dropping database: {}", DB_NAME);
124+
runQuery(String.format("DROP DATABASE IF EXISTS %s", DB_NAME));
125+
}
126+
}

0 commit comments

Comments
 (0)