Skip to content

Commit 0e41e23

Browse files
committed
add documentation for OperationMetrics#completeOperation().
1 parent eb0bfaf commit 0e41e23

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

client-v2/src/main/java/com/clickhouse/client/api/metrics/OperationMetrics.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public String getQueryId() {
3636
return queryId;
3737
}
3838

39+
/**
40+
* Complete counting metrics on operation and stop all stopwatches.
41+
* Multiple calls may have side effects.
42+
* Note: should not be called by user code, except when created by user code.
43+
*/
3944
public void operationComplete() {
4045
for (Map.Entry<String, StopWatch> sw : clientStatistics.getStopWatches().entrySet()) {
4146
sw.getValue().stop();

client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,13 +1271,13 @@ public void testQueryMetrics() throws Exception {
12711271
.waitEndOfQuery(true)
12721272
.setQueryId(uuid);
12731273

1274-
QueryResponse response = client.query("SELECT * FROM " + DATASET_TABLE + " LIMIT 3", settings).get();
1274+
try (QueryResponse response = client.query("SELECT * FROM " + DATASET_TABLE + " LIMIT 3", settings).get()) {
1275+
// Stats should be available after the query is done
1276+
OperationMetrics metrics = response.getMetrics();
12751277

1276-
// Stats should be available after the query is done
1277-
OperationMetrics metrics = response.getMetrics();
1278-
1279-
Assert.assertEquals(metrics.getMetric(ServerMetrics.NUM_ROWS_READ).getLong(), 10); // 10 rows in the table
1280-
Assert.assertEquals(metrics.getMetric(ServerMetrics.RESULT_ROWS).getLong(), 3);
1278+
Assert.assertEquals(metrics.getMetric(ServerMetrics.NUM_ROWS_READ).getLong(), 10); // 10 rows in the table
1279+
Assert.assertEquals(metrics.getMetric(ServerMetrics.RESULT_ROWS).getLong(), 3);
1280+
}
12811281

12821282
StringBuilder insertStmtBuilder = new StringBuilder();
12831283
insertStmtBuilder.append("INSERT INTO default.").append(DATASET_TABLE).append(" VALUES ");
@@ -1288,17 +1288,26 @@ public void testQueryMetrics() throws Exception {
12881288
insertStmtBuilder.setLength(insertStmtBuilder.length() - 2);
12891289
insertStmtBuilder.append("), ");
12901290
}
1291-
response = client.query(insertStmtBuilder.toString(), settings).get();
1291+
try (QueryResponse response = client.query(insertStmtBuilder.toString(), settings).get()) {
12921292

1293-
metrics = response.getMetrics();
1293+
OperationMetrics metrics = response.getMetrics();
12941294

1295-
Assert.assertEquals(metrics.getMetric(ServerMetrics.NUM_ROWS_READ).getLong(), rowsToInsert); // 10 rows in the table
1296-
Assert.assertEquals(metrics.getMetric(ServerMetrics.RESULT_ROWS).getLong(), rowsToInsert);
1297-
Assert.assertEquals(response.getReadRows(), rowsToInsert);
1298-
Assert.assertTrue(metrics.getMetric(ClientMetrics.OP_DURATION).getLong() > 0);
1299-
Assert.assertEquals(metrics.getQueryId(), uuid);
1300-
Assert.assertEquals(response.getQueryId(), uuid);
1295+
Assert.assertEquals(metrics.getMetric(ServerMetrics.NUM_ROWS_READ).getLong(), rowsToInsert); // 10 rows in the table
1296+
Assert.assertEquals(metrics.getMetric(ServerMetrics.RESULT_ROWS).getLong(), rowsToInsert);
1297+
Assert.assertEquals(response.getReadRows(), rowsToInsert);
1298+
Assert.assertTrue(metrics.getMetric(ClientMetrics.OP_DURATION).getLong() > 0);
1299+
Assert.assertEquals(metrics.getQueryId(), uuid);
1300+
Assert.assertEquals(response.getQueryId(), uuid);
1301+
}
13011302

1303+
try (QueryResponse response = client.query("SELECT number FROM system.numbers LIMIT 30", settings).get()) {
1304+
// Stats should be available after the query is done
1305+
OperationMetrics metrics = response.getMetrics();
1306+
1307+
Assert.assertEquals(metrics.getMetric(ServerMetrics.NUM_ROWS_READ).getLong(), 30);
1308+
Assert.assertTrue(metrics.getMetric(ServerMetrics.ELAPSED_TIME).getLong() > 0);
1309+
Assert.assertTrue(metrics.getMetric(ServerMetrics.RESULT_ROWS).getLong() > 0);
1310+
}
13021311
}
13031312

13041313
private final static List<String> DATASET_COLUMNS = Arrays.asList(

0 commit comments

Comments
 (0)