|
| 1 | +package com.clickhouse.benchmark.clients; |
| 2 | + |
| 3 | +import com.clickhouse.client.ClickHouseConfig; |
| 4 | +import com.clickhouse.client.api.Client; |
| 5 | +import com.clickhouse.client.api.data_formats.RowBinaryWithNamesAndTypesFormatReader; |
| 6 | +import com.clickhouse.client.api.data_formats.internal.BinaryStreamReader; |
| 7 | +import com.clickhouse.client.api.query.QueryResponse; |
| 8 | +import com.clickhouse.client.api.query.QuerySettings; |
| 9 | +import com.clickhouse.client.config.ClickHouseClientOption; |
| 10 | +import com.clickhouse.data.ClickHouseColumn; |
| 11 | +import com.clickhouse.data.ClickHouseDataProcessor; |
| 12 | +import com.clickhouse.data.ClickHouseFormat; |
| 13 | +import com.clickhouse.data.ClickHouseInputStream; |
| 14 | +import com.clickhouse.data.ClickHouseRecord; |
| 15 | +import com.clickhouse.data.format.ClickHouseRowBinaryProcessor; |
| 16 | +import org.openjdk.jmh.annotations.Benchmark; |
| 17 | +import org.openjdk.jmh.annotations.Level; |
| 18 | +import org.openjdk.jmh.annotations.Setup; |
| 19 | +import org.openjdk.jmh.infra.Blackhole; |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | + |
| 23 | +import java.io.ByteArrayInputStream; |
| 24 | +import java.io.InputStream; |
| 25 | +import java.nio.ByteBuffer; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +public class Deserializers extends BenchmarkBase { |
| 31 | + private static final Logger LOGGER = LoggerFactory.getLogger(Deserializers.class); |
| 32 | + |
| 33 | + @Setup(Level.Iteration) |
| 34 | + public void setUpIteration(DataState dataState) { |
| 35 | + super.setUpIteration(); |
| 36 | + |
| 37 | + try (Client c = getClientV2(); QueryResponse r = c.query("SELECT * FROM " + dataState.tableNameFilled, new QuerySettings() |
| 38 | + .setFormat(ClickHouseFormat.RowBinaryWithNamesAndTypes)).get()){ |
| 39 | + dataState.datasetAsRowBinaryWithNamesAndTypes = ByteBuffer.wrap(r.getInputStream().readAllBytes()); |
| 40 | + LOGGER.info("Loaded {} from dataset", dataState.datasetAsRowBinaryWithNamesAndTypes.capacity()); |
| 41 | + } catch (Exception e ) { |
| 42 | + LOGGER.error("Failed to init data for components benchmark", e); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Benchmark |
| 47 | + public void DeserializerOutputStreamV1(DataState dataState, Blackhole blackhole) { |
| 48 | + InputStream input = new ByteArrayInputStream(dataState.datasetAsRowBinaryWithNamesAndTypes.array()); |
| 49 | + try { |
| 50 | + ClickHouseConfig config = new ClickHouseConfig(Collections.singletonMap(ClickHouseClientOption.FORMAT, ClickHouseFormat.RowBinaryWithNamesAndTypes)); |
| 51 | + ClickHouseDataProcessor p = new ClickHouseRowBinaryProcessor(config, |
| 52 | + ClickHouseInputStream.of(input), null, null, Collections.emptyMap()); |
| 53 | + List<ClickHouseColumn> columns = p.getColumns(); |
| 54 | + for (ClickHouseRecord record : p.records()) { |
| 55 | + for (int i = 0; i < columns.size(); i++) { |
| 56 | + blackhole.consume(record.getValue(i).asObject()); |
| 57 | + } |
| 58 | + } |
| 59 | + } catch (Exception e) { |
| 60 | + LOGGER.error("Error: ", e); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Benchmark |
| 65 | + public void DeserializerOutputStreamV2(DataState dataState, Blackhole blackhole) { |
| 66 | + InputStream input = new ByteArrayInputStream(dataState.datasetAsRowBinaryWithNamesAndTypes.array()); |
| 67 | + try { |
| 68 | + RowBinaryWithNamesAndTypesFormatReader r = new RowBinaryWithNamesAndTypesFormatReader(input, |
| 69 | + new QuerySettings() |
| 70 | + .setUseTimeZone("UTC") |
| 71 | + .setFormat(ClickHouseFormat.RowBinaryWithNamesAndTypes), new BinaryStreamReader.DefaultByteBufferAllocator()); |
| 72 | + |
| 73 | + Map<String, Object> row; |
| 74 | + while ((row = r.next()) != null) { |
| 75 | + for (String column : row.keySet()) { |
| 76 | + blackhole.consume(row.get(column)); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + } catch (Exception e) { |
| 81 | + LOGGER.error("Error: ", e); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments