Skip to content

Commit d3fce87

Browse files
author
Paultagoras
committed
WIP update to not use ClickHouseServerForTest
1 parent 3ecbe05 commit d3fce87

File tree

12 files changed

+238
-218
lines changed

12 files changed

+238
-218
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ profile.html
5656
performance/jmh-simple-results.json
5757
*.csv
5858
*.sql
59+
*.json

performance/pom.xml

Lines changed: 9 additions & 2 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>
@@ -188,8 +195,8 @@
188195
<argument>-classpath</argument>
189196
<classpath />
190197
<argument>com.clickhouse.benchmark.BenchmarkRunner</argument>
191-
<argument>--dataset=file://dataset_300k.csv</argument>
192-
<argument>.*</argument>
198+
<!-- <argument>&#45;&#45;dataset=file://dataset_300k.csv</argument>-->
199+
<!-- <argument>.*</argument>-->
193200
</arguments>
194201
</configuration>
195202
</execution>

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.clickhouse.benchmark.clients.InsertClient;
44
import com.clickhouse.benchmark.clients.QueryClient;
5-
import com.clickhouse.benchmark.data.DataSet;
65
import org.openjdk.jmh.annotations.Mode;
76
import org.openjdk.jmh.profile.GCProfiler;
87
import org.openjdk.jmh.profile.MemPoolProfiler;
@@ -18,7 +17,8 @@
1817
import java.util.Map;
1918
import java.util.concurrent.TimeUnit;
2019

21-
import static com.clickhouse.benchmark.clients.BenchmarkBase.DB_NAME;
20+
import static com.clickhouse.benchmark.TestEnvironment.DB_NAME;
21+
import static com.clickhouse.benchmark.TestEnvironment.isCloud;
2222

2323

2424
public class BenchmarkRunner {
@@ -38,13 +38,13 @@ public static void main(String[] args) throws Exception {
3838
.threads(1)
3939
.addProfiler(GCProfiler.class)
4040
.addProfiler(MemPoolProfiler.class)
41-
.warmupIterations(1)
41+
.warmupIterations(3)
4242
.warmupTime(TimeValue.seconds(10))
43-
.measurementIterations(3)
43+
.measurementIterations(10)
4444
.jvmArgs("-Xms8g", "-Xmx8g")
4545
.measurementTime(TimeValue.seconds(10))
4646
.resultFormat(ResultFormatType.JSON)
47-
.result("jmh-simple-results.json")
47+
.result(String.format("jmh-results-%s-%s.json", isCloud() ? "cloud" : "local", System.currentTimeMillis()))
4848
.build();
4949

5050
new Runner(opt).run();
@@ -67,16 +67,16 @@ public static Map<String, String> parseArguments(String[] args) {
6767
return argMap;
6868
}
6969

70-
public static String getSelectQuery(DataSet dataSet) {
71-
return "SELECT * FROM `" + DB_NAME + "`.`" + dataSet.getTableName() + "`";
70+
public static String getSelectQuery(String tableName) {
71+
return "SELECT * FROM `" + DB_NAME + "`.`" + tableName + "`";
7272
}
7373

74-
public static String getSelectCountQuery(DataSet dataSet) {
75-
return "SELECT COUNT(*) FROM `" + DB_NAME + "`.`" + dataSet.getTableName() + "`";
74+
public static String getSelectCountQuery(String tableName) {
75+
return String.format("SELECT COUNT(*) FROM `%s`.`%s`", DB_NAME, tableName);
7676
}
7777

78-
public static String getInsertQuery(DataSet dataSet) {
79-
return "INSERT INTO `" + DB_NAME + "`.`" + dataSet.getTableName() + "`";
78+
public static String getInsertQuery(String tableName) {
79+
return String.format("INSERT INTO `%s`.`%s`", DB_NAME, tableName);
8080
}
8181

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

0 commit comments

Comments
 (0)