|
| 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