1212import com .clickhouse .client .ClickHouseResponse ;
1313import com .clickhouse .client .ClickHouseServerForTest ;
1414import com .clickhouse .client .api .Client ;
15+ import com .clickhouse .client .api .data_formats .ClickHouseBinaryFormatReader ;
1516import com .clickhouse .client .api .enums .Protocol ;
1617import com .clickhouse .client .api .insert .InsertResponse ;
1718import com .clickhouse .client .api .metadata .TableSchema ;
1819import com .clickhouse .data .ClickHouseDataProcessor ;
20+ import com .clickhouse .client .api .query .GenericRecord ;
21+ import com .clickhouse .client .api .query .QueryResponse ;
1922import com .clickhouse .data .ClickHouseFormat ;
2023import com .clickhouse .data .ClickHouseOutputStream ;
2124import com .clickhouse .data .ClickHouseRecord ;
2225import com .clickhouse .data .format .ClickHouseRowBinaryProcessor ;
26+ import org .openjdk .jmh .annotations .Level ;
2327import org .openjdk .jmh .annotations .Param ;
2428import org .openjdk .jmh .annotations .Scope ;
29+ import org .openjdk .jmh .annotations .Setup ;
2530import org .openjdk .jmh .annotations .State ;
31+ import org .openjdk .jmh .annotations .TearDown ;
2632import org .slf4j .Logger ;
2733import org .slf4j .LoggerFactory ;
2834
2935import java .io .ByteArrayOutputStream ;
3036import java .io .InputStream ;
3137import java .util .ArrayList ;
3238import java .util .Collections ;
39+ import java .math .BigInteger ;
40+ import java .util .List ;
3341
3442import static com .clickhouse .client .ClickHouseServerForTest .isCloud ;
3543
44+ @ State (Scope .Benchmark )
3645public class BenchmarkBase {
3746 private static final Logger LOGGER = LoggerFactory .getLogger (BenchmarkBase .class );
3847 public static final String DB_NAME = "benchmarks" ;
3948
49+ protected ClickHouseClient clientV1 ;
50+ protected Client clientV2 ;
51+ @ Setup (Level .Iteration )
52+ public void setUpIteration () {
53+ clientV1 = getClientV1 ();
54+ clientV2 = getClientV2 ();
55+
56+ }
57+
58+ @ TearDown (Level .Iteration )
59+ public void tearDownIteration () {
60+ if (clientV1 != null ) {
61+ clientV1 .close ();
62+ clientV1 = null ;
63+ }
64+ if (clientV2 != null ) {
65+ clientV2 .close ();
66+ clientV2 = null ;
67+ }
68+ }
69+
4070 @ State (Scope .Benchmark )
4171 public static class DataState {
4272 @ Param ({"simple" })
4373 String datasetSourceName ;
4474 ClickHouseFormat format = ClickHouseFormat .JSONEachRow ;
75+ @ Param ({"1000000" })
76+ int limit ;
4577
4678 DataSet dataSet ;
4779 }
@@ -67,6 +99,7 @@ public void tearDown() {
6799 BaseIntegrationTest .teardownClickHouseContainer ();
68100 }
69101
102+
70103 //Connection parameters
71104 public static String getPassword () {
72105 return ClickHouseServerForTest .getPassword ();
@@ -77,34 +110,20 @@ public static String getUsername() {
77110 public static ClickHouseNode getServer () {
78111 return ClickHouseServerForTest .getClickHouseNode (ClickHouseProtocol .HTTP , isCloud (), ClickHouseNode .builder ().build ());
79112 }
80- public static void notNull (Object obj ) {
81- if (obj == null ) {
82- throw new IllegalStateException ( "Null value " );
113+ public static void isNotNull (Object obj , boolean doWeCare ) {
114+ if (obj == null && doWeCare ) {
115+ throw new RuntimeException ( "Object is null " );
83116 }
84117 }
85118
86- public static void runQuery (String query , boolean useDatabase ) {
87- ClickHouseNode node = getServer ();
88- try (Client client = new Client .Builder ()
89- .addEndpoint (Protocol .HTTP , node .getHost (), node .getPort (), isCloud ())
90- .setUsername (getUsername ())
91- .setPassword (getPassword ())
92- .compressClientRequest (true )
93- .setDefaultDatabase (useDatabase ? DB_NAME : "default" )
94- .build ()) {
95- client .queryAll (query );
119+ public static List <GenericRecord > runQuery (String query , boolean useDatabase ) {
120+ try (Client client = getClientV2 (useDatabase )) {
121+ return client .queryAll (query );
96122 }
97123 }
98124
99125 public static void insertData (String tableName , InputStream dataStream , ClickHouseFormat format ) {
100- ClickHouseNode node = getServer ();
101- try (Client client = new Client .Builder ()
102- .addEndpoint (Protocol .HTTP , node .getHost (), node .getPort (), isCloud ())
103- .setUsername (getUsername ())
104- .setPassword (getPassword ())
105- .compressClientRequest (true )
106- .setDefaultDatabase (DB_NAME )
107- .build ();
126+ try (Client client = getClientV2 ();
108127 InsertResponse response = client .insert (tableName , dataStream , format ).get ()) {
109128 LOGGER .info ("Rows inserted: {}" , response .getWrittenRows ());
110129 } catch (Exception e ) {
@@ -113,6 +132,40 @@ public static void insertData(String tableName, InputStream dataStream, ClickHou
113132 }
114133 }
115134
135+ public static void verifyRowsInsertedAndCleanup (DataSet dataSet ) {
136+ try {
137+ List <GenericRecord > records = runQuery ("SELECT count(*) FROM `" + dataSet .getTableName () + "`" , true );
138+ BigInteger count = records .get (0 ).getBigInteger (1 );
139+ if (count .longValue () != dataSet .getSize ()) {
140+ throw new IllegalStateException ("Rows written: " + count + " Expected " + dataSet .getSize () + " rows" );
141+ }
142+ runQuery ("TRUNCATE TABLE IF EXISTS `" + dataSet .getTableName () + "`" , true );
143+ } catch (Exception e ) {
144+ LOGGER .error ("Error: " , e );
145+ }
146+ }
147+
148+ protected static ClickHouseClient getClientV1 () {
149+ //We get a new client so that closing won't affect other subsequent calls
150+ return ClickHouseClient .newInstance (ClickHouseCredentials .fromUserAndPassword (getUsername (), getPassword ()), ClickHouseProtocol .HTTP );
151+ }
152+
153+ protected static Client getClientV2 () {
154+ return getClientV2 (true );
155+ }
156+ protected static Client getClientV2 (boolean useDatabase ) {
157+ ClickHouseNode node = getServer ();
158+ //We get a new client so that closing won't affect other subsequent calls
159+ return new Client .Builder ()
160+ .addEndpoint (Protocol .HTTP , node .getHost (), node .getPort (), isCloud ())
161+ .setUsername (getUsername ())
162+ .setPassword (getPassword ())
163+ .compressClientRequest (true )
164+ .setMaxRetries (0 )
165+ .setDefaultDatabase (useDatabase ? DB_NAME : "default" )
166+ .build ();
167+ }
168+
116169 public static void loadClickHouseRecords (String tableName , DataSet dataSet ) {
117170 ClickHouseNode node = getServer ();
118171
0 commit comments