|
| 1 | +package com.clickhouse.benchmark.clients; |
| 2 | + |
| 3 | +import com.clickhouse.benchmark.BenchmarkRunner; |
| 4 | +import com.clickhouse.client.ClickHouseClient; |
| 5 | +import com.clickhouse.client.ClickHouseResponse; |
| 6 | +import com.clickhouse.client.api.Client; |
| 7 | +import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader; |
| 8 | +import com.clickhouse.client.api.insert.InsertResponse; |
| 9 | +import com.clickhouse.client.api.insert.InsertSettings; |
| 10 | +import com.clickhouse.client.api.query.QueryResponse; |
| 11 | +import com.clickhouse.client.config.ClickHouseClientOption; |
| 12 | +import com.clickhouse.data.ClickHouseFormat; |
| 13 | +import com.clickhouse.data.ClickHouseRecord; |
| 14 | +import org.openjdk.jmh.annotations.Benchmark; |
| 15 | +import org.openjdk.jmh.annotations.Group; |
| 16 | +import org.openjdk.jmh.annotations.Level; |
| 17 | +import org.openjdk.jmh.annotations.Scope; |
| 18 | +import org.openjdk.jmh.annotations.Setup; |
| 19 | +import org.openjdk.jmh.annotations.State; |
| 20 | +import org.openjdk.jmh.annotations.TearDown; |
| 21 | +import org.openjdk.jmh.annotations.Threads; |
| 22 | +import org.slf4j.Logger; |
| 23 | +import org.slf4j.LoggerFactory; |
| 24 | + |
| 25 | +import static com.clickhouse.benchmark.TestEnvironment.getServer; |
| 26 | + |
| 27 | +@Threads(2) |
| 28 | +@State(Scope.Benchmark) |
| 29 | +public class MixedWorkload extends BenchmarkBase { |
| 30 | + private static final Logger LOGGER = LoggerFactory.getLogger(MixedWorkload.class); |
| 31 | + |
| 32 | + private ClickHouseClient clientV1Shared; |
| 33 | + private Client clientV2Shared; |
| 34 | + @Setup(Level.Trial) |
| 35 | + public void setUpIteration() { |
| 36 | + clientV1Shared = getClientV1(); |
| 37 | + clientV2Shared = getClientV2(); |
| 38 | + } |
| 39 | + |
| 40 | + @TearDown(Level.Trial) |
| 41 | + public void tearDownIteration() { |
| 42 | + if (clientV1Shared != null) { |
| 43 | + clientV1Shared.close(); |
| 44 | + clientV1Shared = null; |
| 45 | + } |
| 46 | + if (clientV2Shared != null) { |
| 47 | + clientV2Shared.close(); |
| 48 | + clientV2Shared = null; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @TearDown(Level.Iteration) |
| 53 | + public void teardownIteration(DataState dataState) { |
| 54 | + truncateTable(dataState.tableNameEmpty); |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + @Benchmark |
| 59 | + @Group("mixed_v1") |
| 60 | + public void insertV1(DataState dataState) { |
| 61 | + try { |
| 62 | + ClickHouseFormat format = dataState.dataSet.getFormat(); |
| 63 | + try (ClickHouseResponse response = clientV1Shared.read(getServer()) |
| 64 | + .write() |
| 65 | + .option(ClickHouseClientOption.ASYNC, false) |
| 66 | + .format(format) |
| 67 | + .query(BenchmarkRunner.getInsertQuery(dataState.tableNameEmpty)) |
| 68 | + .data(out -> { |
| 69 | + for (byte[] bytes: dataState.dataSet.getBytesList(format)) { |
| 70 | + out.write(bytes); |
| 71 | + } |
| 72 | + }).executeAndWait()) { |
| 73 | + response.getSummary(); |
| 74 | + } |
| 75 | + } catch (Exception e) { |
| 76 | + LOGGER.error("Error: ", e); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Benchmark |
| 81 | + @Group("mixed_v1") |
| 82 | + public void queryV1(DataState dataState) { |
| 83 | + try { |
| 84 | + try (ClickHouseResponse response = clientV1Shared.read(getServer()) |
| 85 | + .query(BenchmarkRunner.getSelectQuery(dataState.tableNameFilled)) |
| 86 | + .format(ClickHouseFormat.RowBinaryWithNamesAndTypes) |
| 87 | + .option(ClickHouseClientOption.ASYNC, false) |
| 88 | + .executeAndWait()) { |
| 89 | + for (ClickHouseRecord record: response.records()) {//Compiler optimization avoidance |
| 90 | + for (int i = 0; i < dataState.dataSet.getSchema().getColumns().size(); i++) { |
| 91 | + isNotNull(record.getValue(i), false); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } catch (Exception e) { |
| 96 | + LOGGER.error("Error: ", e); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + @Benchmark |
| 103 | + @Group("mixed_v2") |
| 104 | + public void insertV2(DataState dataState) { |
| 105 | + try { |
| 106 | + ClickHouseFormat format = dataState.dataSet.getFormat(); |
| 107 | + try (InsertResponse response = clientV2Shared.insert(dataState.tableNameEmpty, out -> { |
| 108 | + for (byte[] bytes: dataState.dataSet.getBytesList(format)) { |
| 109 | + out.write(bytes); |
| 110 | + } |
| 111 | + out.close(); |
| 112 | + }, format, new InsertSettings()).get()) { |
| 113 | + response.getWrittenRows(); |
| 114 | + } |
| 115 | + } catch (Exception e) { |
| 116 | + LOGGER.error("Error: ", e); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @Benchmark |
| 121 | + @Group("mixed_v2") |
| 122 | + public void queryV2(DataState dataState) { |
| 123 | + try { |
| 124 | + try(QueryResponse response = clientV2Shared.query(BenchmarkRunner.getSelectQuery(dataState.tableNameEmpty)).get()) { |
| 125 | + ClickHouseBinaryFormatReader reader = clientV2Shared.newBinaryFormatReader(response); |
| 126 | + while (reader.next() != null) {//Compiler optimization avoidance |
| 127 | + for (int i = 1; i <= dataState.dataSet.getSchema().getColumns().size(); i++) { |
| 128 | + isNotNull(reader.readValue(1), false); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } catch (Exception e) { |
| 133 | + LOGGER.error("Error: ", e); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments