Skip to content

Commit

Permalink
Print the query statement. (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
YangYumings authored Nov 28, 2024
1 parent f2826ee commit e1a45c1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutionException;
Expand All @@ -54,6 +56,8 @@ public abstract class BaseMode {
Executors.newScheduledThreadPool(2, new NamedThreadFactory("ShowResultPeriodically"));

private static final double NANO_TO_SECOND = 1000000000.0d;
private static final String RESULT_ITEM = "%-35s";
private static final String LATENCY_ITEM = "%-80s";

protected ExecutorService schemaExecutorService =
Executors.newFixedThreadPool(
Expand Down Expand Up @@ -101,6 +105,7 @@ public void run() {
Thread.currentThread().interrupt();
}
postCheck();
printSqlStatements();
scheduler.shutdownNow();
}

Expand Down Expand Up @@ -275,4 +280,38 @@ private static void measure(
measurement.outputCSV();
}
}

private static Map<Operation, String> sqlMap = new HashMap<>();

/**
* Each type of query is recorded once.
*
* @param operation
* @param sql
*/
public static void logSqlIfNotCollect(Operation operation, String sql) {
sqlMap.computeIfAbsent(operation, k -> sql);
}

/** Output all query statements */
private void printSqlStatements() {
StringBuilder stringBuilder = new StringBuilder("\n");
stringBuilder
.append(
"----------------------------------------------------------------------------SQL Statements Example---------------------------------------------------------------------")
.append('\n');
stringBuilder
.append(String.format(RESULT_ITEM, "Operation"))
.append(String.format(LATENCY_ITEM, "SQL"))
.append("\n");
for (Map.Entry<Operation, String> entry : sqlMap.entrySet()) {
stringBuilder
.append(String.format(RESULT_ITEM, entry.getKey()))
.append(String.format(LATENCY_ITEM, entry.getValue()))
.append("\n");
}
stringBuilder.append(
"------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
System.out.println(stringBuilder.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import cn.edu.tsinghua.iot.benchmark.iotdb200.ModelStrategy.TreeStrategy;
import cn.edu.tsinghua.iot.benchmark.iotdb200.utils.IoTDBUtils;
import cn.edu.tsinghua.iot.benchmark.measurement.Status;
import cn.edu.tsinghua.iot.benchmark.mode.BaseMode;
import cn.edu.tsinghua.iot.benchmark.schema.schemaImpl.DeviceSchema;
import cn.edu.tsinghua.iot.benchmark.tsdb.DBConfig;
import cn.edu.tsinghua.iot.benchmark.tsdb.IDatabase;
Expand Down Expand Up @@ -227,6 +228,7 @@ public Status preciseQuery(PreciseQuery preciseQuery) {
builder.append(getSimpleQuerySqlHead(preciseQuery.getDeviceSchema()));
modelStrategy.addPreciseQueryWhereClause(
String.valueOf(preciseQuery.getTimestamp()), preciseQuery.getDeviceSchema(), builder);
BaseMode.logSqlIfNotCollect(Operation.PRECISE_QUERY, builder.toString());
return executeQueryAndGetStatus(builder.toString(), Operation.PRECISE_QUERY);
}

Expand All @@ -250,6 +252,7 @@ public Status rangeQuery(RangeQuery rangeQuery) {
rangeQuery.getDeviceSchema(),
0,
builder);
BaseMode.logSqlIfNotCollect(Operation.RANGE_QUERY, builder.toString());
return executeQueryAndGetStatus(builder.toString(), Operation.RANGE_QUERY);
}

Expand All @@ -273,6 +276,7 @@ public Status valueRangeQuery(ValueRangeQuery valueRangeQuery) {
valueRangeQuery.getDeviceSchema(),
(int) valueRangeQuery.getValueThreshold(),
builder);
BaseMode.logSqlIfNotCollect(Operation.VALUE_RANGE_QUERY, builder.toString());
return executeQueryAndGetStatus(builder.toString(), Operation.VALUE_RANGE_QUERY);
}

Expand All @@ -299,6 +303,7 @@ public Status aggRangeQuery(AggRangeQuery aggRangeQuery) {
aggRangeQuery.getDeviceSchema(),
0,
builder);
BaseMode.logSqlIfNotCollect(Operation.AGG_RANGE_QUERY, builder.toString());
return executeQueryAndGetStatus(builder.toString(), Operation.AGG_RANGE_QUERY);
}

Expand All @@ -324,6 +329,7 @@ public Status aggValueQuery(AggValueQuery aggValueQuery) {
aggValueQuery.getDeviceSchema(),
(int) aggValueQuery.getValueThreshold(),
builder);
BaseMode.logSqlIfNotCollect(Operation.AGG_VALUE_QUERY, builder.toString());
return executeQueryAndGetStatus(builder.toString(), Operation.AGG_VALUE_QUERY);
}

Expand Down Expand Up @@ -351,6 +357,7 @@ public Status aggRangeValueQuery(AggRangeValueQuery aggRangeValueQuery) {
aggRangeValueQuery.getDeviceSchema(),
(int) aggRangeValueQuery.getValueThreshold(),
builder);
BaseMode.logSqlIfNotCollect(Operation.AGG_RANGE_VALUE_QUERY, builder.toString());
return executeQueryAndGetStatus(builder.toString(), Operation.AGG_RANGE_VALUE_QUERY);
}

Expand All @@ -363,6 +370,7 @@ public Status aggRangeValueQuery(AggRangeValueQuery aggRangeValueQuery) {
@Override
public Status groupByQuery(GroupByQuery groupByQuery) {
String sql = modelStrategy.getGroupByQuerySQL(groupByQuery, false);
BaseMode.logSqlIfNotCollect(Operation.GROUP_BY_QUERY, sql);
return executeQueryAndGetStatus(sql, Operation.GROUP_BY_QUERY);
}

Expand All @@ -376,6 +384,7 @@ public Status latestPointQuery(LatestPointQuery latestPointQuery) {
String latestPointSqlHead =
modelStrategy.getLatestPointQuerySql(latestPointQuery.getDeviceSchema());
String sql = modelStrategy.addGroupByClauseIfNecessary(latestPointSqlHead);
BaseMode.logSqlIfNotCollect(Operation.LATEST_POINT_QUERY, sql);
return executeQueryAndGetStatus(sql, Operation.LATEST_POINT_QUERY);
}

Expand All @@ -401,6 +410,7 @@ public Status rangeQueryOrderByDesc(RangeQuery rangeQuery) {
builder);
// ORDER BY
modelStrategy.addOrderByTimeDesc(builder);
BaseMode.logSqlIfNotCollect(Operation.RANGE_QUERY_ORDER_BY_TIME_DESC, builder.toString());
return executeQueryAndGetStatus(builder.toString(), Operation.RANGE_QUERY_ORDER_BY_TIME_DESC);
}

Expand All @@ -426,6 +436,7 @@ public Status valueRangeQueryOrderByDesc(ValueRangeQuery valueRangeQuery) {
builder);
// ORDER BY
modelStrategy.addOrderByTimeDesc(builder);
BaseMode.logSqlIfNotCollect(Operation.AGG_RANGE_VALUE_QUERY, builder.toString());
return executeQueryAndGetStatus(
builder.toString(), Operation.VALUE_RANGE_QUERY_ORDER_BY_TIME_DESC);
}
Expand All @@ -440,6 +451,7 @@ public Status valueRangeQueryOrderByDesc(ValueRangeQuery valueRangeQuery) {
@Override
public Status groupByQueryOrderByDesc(GroupByQuery groupByQuery) {
String sql = modelStrategy.getGroupByQuerySQL(groupByQuery, true);
BaseMode.logSqlIfNotCollect(Operation.GROUP_BY_QUERY_ORDER_BY_TIME_DESC, sql);
return executeQueryAndGetStatus(sql, Operation.GROUP_BY_QUERY_ORDER_BY_TIME_DESC);
}

Expand Down

0 comments on commit e1a45c1

Please sign in to comment.