diff --git a/src/client/inc/tscUtil.h b/src/client/inc/tscUtil.h index e11748efbeb58c22c15fde12d6500a5f304d439f..8d8287e53f690cb3217cbe72e7e3afd0d2c5eb51 100644 --- a/src/client/inc/tscUtil.h +++ b/src/client/inc/tscUtil.h @@ -190,6 +190,7 @@ void tscFieldInfoClear(SFieldInfo* pFieldInfo); void tscFieldInfoCopy(SFieldInfo* pFieldInfo, const SFieldInfo* pSrc, const SArray* pExprList); static FORCE_INLINE int32_t tscNumOfFields(SQueryInfo* pQueryInfo) { return pQueryInfo->fieldsInfo.numOfOutput; } +int32_t tscGetFirstInvisibleFieldPos(SQueryInfo* pQueryInfo); int32_t tscFieldInfoCompare(const SFieldInfo* pFieldInfo1, const SFieldInfo* pFieldInfo2, int32_t *diffSize); void tscInsertPrimaryTsSourceColumn(SQueryInfo* pQueryInfo, uint64_t uid); diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 7a24df8b409ee8ffeb8d834757116f778deecc2a..14dfee905e1231739b29c4965091e3cd0910fc08 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -6943,9 +6943,7 @@ static int32_t doAddGroupbyColumnsOnDemand(SSqlCmd* pCmd, SQueryInfo* pQueryInfo s = &pSchema[colIndex]; } } - - size_t size = tscNumOfExprs(pQueryInfo); - + if (TSDB_COL_IS_TAG(pColIndex->flag)) { int32_t f = TSDB_FUNC_TAG; @@ -6953,8 +6951,10 @@ static int32_t doAddGroupbyColumnsOnDemand(SSqlCmd* pCmd, SQueryInfo* pQueryInfo f = TSDB_FUNC_TAGPRJ; } + int32_t pos = tscGetFirstInvisibleFieldPos(pQueryInfo); + SColumnIndex index = {.tableIndex = pQueryInfo->groupbyExpr.tableIndex, .columnIndex = colIndex}; - SExprInfo* pExpr = tscExprAppend(pQueryInfo, f, &index, s->type, s->bytes, getNewResColId(pCmd), s->bytes, true); + SExprInfo* pExpr = tscExprInsert(pQueryInfo, pos, f, &index, s->type, s->bytes, getNewResColId(pCmd), s->bytes, true); memset(pExpr->base.aliasName, 0, sizeof(pExpr->base.aliasName)); tstrncpy(pExpr->base.aliasName, s->name, sizeof(pExpr->base.aliasName)); @@ -6964,13 +6964,15 @@ static int32_t doAddGroupbyColumnsOnDemand(SSqlCmd* pCmd, SQueryInfo* pQueryInfo // NOTE: tag column does not add to source column list SColumnList ids = createColumnList(1, 0, pColIndex->colIndex); - insertResultField(pQueryInfo, (int32_t)size, &ids, s->bytes, (int8_t)s->type, s->name, pExpr); + insertResultField(pQueryInfo, pos, &ids, s->bytes, (int8_t)s->type, s->name, pExpr); } else { // if this query is "group by" normal column, time window query is not allowed if (isTimeWindowQuery(pQueryInfo)) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); } + size_t size = tscNumOfExprs(pQueryInfo); + bool hasGroupColumn = false; for (int32_t j = 0; j < size; ++j) { SExprInfo* pExpr = tscExprGet(pQueryInfo, j); diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index 1a953cfd3dc3bb3f50d591a6f712cfafb9ea28ed..2be11a406a3b193147bbe33a8f49f62d3eda108d 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -2093,6 +2093,22 @@ TAOS_FIELD tscCreateField(int8_t type, const char* name, int16_t bytes) { return f; } +int32_t tscGetFirstInvisibleFieldPos(SQueryInfo* pQueryInfo) { + if (pQueryInfo->fieldsInfo.numOfOutput <= 0 || pQueryInfo->fieldsInfo.internalField == NULL) { + return 0; + } + + for (int32_t i = 0; i < pQueryInfo->fieldsInfo.numOfOutput; ++i) { + SInternalField* pField = taosArrayGet(pQueryInfo->fieldsInfo.internalField, i); + if (!pField->visible) { + return i; + } + } + + return pQueryInfo->fieldsInfo.numOfOutput; +} + + SInternalField* tscFieldInfoAppend(SFieldInfo* pFieldInfo, TAOS_FIELD* pField) { assert(pFieldInfo != NULL); pFieldInfo->numOfOutput++; diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulConnection.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulConnection.java index 12a0ab57e2c35c7f1f550dd213db19a0effd4ebc..e818736096355c4937e5af0470b77c95486c86db 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulConnection.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulConnection.java @@ -18,7 +18,7 @@ public class RestfulConnection extends AbstractConnection { private final String url; private final String database; private final String token; - /******************************************************/ + private boolean isClosed; private final DatabaseMetaData metadata; diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java index a88dc411f333fea82fa5310ee87f263b117f7e70..21c76f73b287e55ef14f5d70cf6a911a9cb543db 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java @@ -88,17 +88,24 @@ public class RestfulStatement extends AbstractStatement { } private String getUrl() throws SQLException { + String dbname = conn.getClientInfo(TSDBDriver.PROPERTY_KEY_DBNAME); + if (dbname == null || dbname.trim().isEmpty()) { + dbname = ""; + } else { + dbname = "/" + dbname.toLowerCase(); + } TimestampFormat timestampFormat = TimestampFormat.valueOf(conn.getClientInfo(TSDBDriver.PROPERTY_KEY_TIMESTAMP_FORMAT).trim().toUpperCase()); String url; + switch (timestampFormat) { case TIMESTAMP: - url = "http://" + conn.getHost() + ":" + conn.getPort() + "/rest/sqlt"; + url = "http://" + conn.getHost() + ":" + conn.getPort() + "/rest/sqlt" + dbname; break; case UTC: - url = "http://" + conn.getHost() + ":" + conn.getPort() + "/rest/sqlutc"; + url = "http://" + conn.getHost() + ":" + conn.getPort() + "/rest/sqlutc" + dbname; break; default: - url = "http://" + conn.getHost() + ":" + conn.getPort() + "/rest/sql"; + url = "http://" + conn.getHost() + ":" + conn.getPort() + "/rest/sql" + dbname; } return url; } diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/cases/MultiConnectionWithDifferentDbTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/cases/MultiConnectionWithDifferentDbTest.java new file mode 100644 index 0000000000000000000000000000000000000000..18a2c32aca0535567dd42e886bc87ae618596a40 --- /dev/null +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/cases/MultiConnectionWithDifferentDbTest.java @@ -0,0 +1,101 @@ +package com.taosdata.jdbc.cases; + +import org.junit.Before; +import org.junit.Test; + +import java.sql.*; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class MultiConnectionWithDifferentDbTest { + + private static String host = "127.0.0.1"; + private static String db1 = "db1"; + private static String db2 = "db2"; + + private long ts; + + @Test + public void test() { + List threads = IntStream.range(1, 3).mapToObj(i -> new Thread(new Runnable() { + @Override + public void run() { + for (int j = 0; j < 10; j++) { + queryDb(); + try { + TimeUnit.SECONDS.sleep(1); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + private void queryDb() { + String url = "jdbc:TAOS-RS://" + host + ":6041/db" + i + "?user=root&password=taosdata"; + try (Connection connection = DriverManager.getConnection(url)) { + Statement stmt = connection.createStatement(); + + ResultSet rs = stmt.executeQuery("select * from weather"); + assertNotNull(rs); + rs.next(); + long actual = rs.getTimestamp("ts").getTime(); + assertEquals(ts, actual); + + int f1 = rs.getInt("f1"); + assertEquals(i, f1); + + String loc = i == 1 ? "beijing" : "shanghai"; + String loc_actual = rs.getString("loc"); + assertEquals(loc, loc_actual); + + stmt.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + }, "thread-" + i)).collect(Collectors.toList()); + + threads.forEach(Thread::start); + + for (Thread t : threads) { + try { + t.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + } + + @Before + public void before() { + ts = System.currentTimeMillis(); + + try { + Connection conn = DriverManager.getConnection("jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata"); + + Statement stmt = conn.createStatement(); + stmt.execute("drop database if exists " + db1); + stmt.execute("create database if not exists " + db1); + stmt.execute("use " + db1); + stmt.execute("create table weather(ts timestamp, f1 int) tags(loc nchar(10))"); + stmt.execute("insert into t1 using weather tags('beijing') values(" + ts + ", 1)"); + + stmt.execute("drop database if exists " + db2); + stmt.execute("create database if not exists " + db2); + stmt.execute("use " + db2); + stmt.execute("create table weather(ts timestamp, f1 int) tags(loc nchar(10))"); + stmt.execute("insert into t1 using weather tags('shanghai') values(" + ts + ", 2)"); + + conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + +} diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/DatabaseSpecifiedTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/DatabaseSpecifiedTest.java new file mode 100644 index 0000000000000000000000000000000000000000..9fe51e7203fac7133783e47fd5b0cc07f33b2494 --- /dev/null +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/DatabaseSpecifiedTest.java @@ -0,0 +1,69 @@ +package com.taosdata.jdbc.rs; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.sql.*; + +import static org.junit.Assert.*; + +public class DatabaseSpecifiedTest { + + private static String host = "127.0.0.1"; + private static String dbname = "test_db_spec"; + + private Connection connection; + private long ts; + + @Test + public void test() throws SQLException { + // when + connection = DriverManager.getConnection("jdbc:TAOS-RS://" + host + ":6041/" + dbname + "?user=root&password=taosdata"); + try (Statement stmt = connection.createStatement();) { + ResultSet rs = stmt.executeQuery("select * from weather"); + + //then + assertNotNull(rs); + rs.next(); + long now = rs.getTimestamp("ts").getTime(); + assertEquals(ts, now); + int f1 = rs.getInt(2); + assertEquals(1, f1); + String loc = rs.getString("loc"); + assertEquals("beijing", loc); + } + connection.close(); + } + + @Before + public void before() { + ts = System.currentTimeMillis(); + try { + Connection connection = DriverManager.getConnection("jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata"); + Statement stmt = connection.createStatement(); + + stmt.execute("drop database if exists " + dbname); + stmt.execute("create database if not exists " + dbname); + stmt.execute("use " + dbname); + stmt.execute("create table weather(ts timestamp, f1 int) tags(loc nchar(10))"); + stmt.execute("insert into t1 using weather tags('beijing') values( " + ts + ", 1)"); + + stmt.close(); + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + @After + public void after() { + try { + if (connection != null) + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + +} diff --git a/src/kit/shell/inc/shell.h b/src/kit/shell/inc/shell.h index c9d58fd3e1e6b428ce54a4db19d470d28036f445..f207a866ddc712165340c06b026aa99081f91c81 100644 --- a/src/kit/shell/inc/shell.h +++ b/src/kit/shell/inc/shell.h @@ -55,6 +55,8 @@ typedef struct SShellArguments { int abort; int port; int pktLen; + int pktNum; + char* pktType; char* netTestRole; } SShellArguments; diff --git a/src/kit/shell/src/shellLinux.c b/src/kit/shell/src/shellLinux.c index 859542cde958a634de6f5da38d09bcdb6b174044..93783b205560604c9d25c9f5dc2e73a239a67b8e 100644 --- a/src/kit/shell/src/shellLinux.c +++ b/src/kit/shell/src/shellLinux.c @@ -50,6 +50,8 @@ static struct argp_option options[] = { {"timezone", 'z', "TIMEZONE", 0, "Time zone of the shell, default is local."}, {"netrole", 'n', "NETROLE", 0, "Net role when network connectivity test, default is startup, options: client|server|rpc|startup|sync|speen|fqdn."}, {"pktlen", 'l', "PKTLEN", 0, "Packet length used for net test, default is 1000 bytes."}, + {"pktnum", 'N', "PKTNUM", 0, "Packet numbers used for net test, default is 100."}, + {"pkttype", 'S', "PKTTYPE", 0, "Packet type used for net test, default is TCP."}, {0}}; static error_t parse_opt(int key, char *arg, struct argp_state *state) { @@ -106,7 +108,7 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) { arguments->is_raw_time = true; break; case 'f': - if (wordexp(arg, &full_path, 0) != 0) { + if ((0 == strlen(arg)) || (wordexp(arg, &full_path, 0) != 0)) { fprintf(stderr, "Invalid path %s\n", arg); return -1; } @@ -146,6 +148,17 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) { return -1; } break; + case 'N': + if (arg) { + arguments->pktNum = atoi(arg); + } else { + fprintf(stderr, "Invalid packet number\n"); + return -1; + } + break; + case 'S': + arguments->pktType = arg; + break; case OPT_ABORT: arguments->abort = 1; break; diff --git a/src/kit/shell/src/shellMain.c b/src/kit/shell/src/shellMain.c index 4e00b0d8ff02819ee08a5ba537028e3835fcc945..5c9dc0995dacecebd10b7f2b77e216ca97157db0 100644 --- a/src/kit/shell/src/shellMain.c +++ b/src/kit/shell/src/shellMain.c @@ -85,6 +85,8 @@ SShellArguments args = { .threadNum = 5, .commands = NULL, .pktLen = 1000, + .pktNum = 100, + .pktType = "TCP", .netTestRole = NULL }; @@ -118,7 +120,7 @@ int main(int argc, char* argv[]) { printf("Failed to init taos"); exit(EXIT_FAILURE); } - taosNetTest(args.netTestRole, args.host, args.port, args.pktLen); + taosNetTest(args.netTestRole, args.host, args.port, args.pktLen, args.pktNum, args.pktType); exit(0); } diff --git a/src/kit/shell/src/shellWindows.c b/src/kit/shell/src/shellWindows.c index d7f96631b675512f9e8a2c6a075d2e0c022199c5..abec34b84c5ff65d6cb13492028cd36321d2d0ca 100644 --- a/src/kit/shell/src/shellWindows.c +++ b/src/kit/shell/src/shellWindows.c @@ -55,9 +55,13 @@ void printHelp() { printf("%s%s\n", indent, "-t"); printf("%s%s%s\n", indent, indent, "Time zone of the shell, default is local."); printf("%s%s\n", indent, "-n"); - printf("%s%s%s\n", indent, indent, "Net role when network connectivity test, default is startup, options: client|server|rpc|startup|sync."); + printf("%s%s%s\n", indent, indent, "Net role when network connectivity test, default is startup, options: client|server|rpc|startup|sync|speed|fqdn."); printf("%s%s\n", indent, "-l"); printf("%s%s%s\n", indent, indent, "Packet length used for net test, default is 1000 bytes."); + printf("%s%s\n", indent, "-N"); + printf("%s%s%s\n", indent, indent, "Packet numbers used for net test, default is 100."); + printf("%s%s\n", indent, "-S"); + printf("%s%s%s\n", indent, indent, "Packet type used for net test, default is TCP."); printf("%s%s\n", indent, "-V"); printf("%s%s%s\n", indent, indent, "Print program version."); diff --git a/src/kit/taosdemo/taosdemo.c b/src/kit/taosdemo/taosdemo.c index 38dc47ad017b55bc8ef3042bb0bc9939728e9d9e..30d8a1e67524e692ef468bd34bc2240fdaed7991 100644 --- a/src/kit/taosdemo/taosdemo.c +++ b/src/kit/taosdemo/taosdemo.c @@ -443,6 +443,7 @@ typedef struct SThreadInfo_S { uint64_t start_table_from; uint64_t end_table_to; int64_t ntables; + int64_t tables_created; uint64_t data_of_rate; int64_t start_time; char* cols; @@ -639,6 +640,7 @@ SArguments g_args = { static SDbs g_Dbs; static int64_t g_totalChildTables = 0; +static int64_t g_actualChildTables = 0; static SQueryMetaInfo g_queryInfo; static FILE * g_fpOfInsertResult = NULL; @@ -964,6 +966,7 @@ static void parse_args(int argc, char *argv[], SArguments *arguments) { exit(EXIT_FAILURE); } arguments->num_of_tables = atoi(argv[++i]); + g_totalChildTables = arguments->num_of_tables; } else if (strcmp(argv[i], "-n") == 0) { if ((argc == i+1) || (!isStringNumber(argv[i+1]))) { @@ -1134,7 +1137,7 @@ static void parse_args(int argc, char *argv[], SArguments *arguments) { exit(EXIT_FAILURE); } } else if ((strcmp(argv[i], "--version") == 0) || - (strcmp(argv[i], "-V") == 0)){ + (strcmp(argv[i], "-V") == 0)) { printVersion(); exit(0); } else if (strcmp(argv[i], "--help") == 0) { @@ -1345,14 +1348,14 @@ static void selectAndGetResult( } } -static char *rand_bool_str(){ +static char *rand_bool_str() { static int cursor; cursor++; if (cursor > (MAX_PREPARED_RAND - 1)) cursor = 0; return g_randbool_buff + ((cursor % MAX_PREPARED_RAND) * BOOL_BUFF_LEN); } -static int32_t rand_bool(){ +static int32_t rand_bool() { static int cursor; cursor++; if (cursor > (MAX_PREPARED_RAND - 1)) cursor = 0; @@ -1485,7 +1488,7 @@ static char *demo_phase_float_str() { return g_rand_phase_buff + ((cursor % MAX_PREPARED_RAND) * FLOAT_BUFF_LEN); } -static float UNUSED_FUNC demo_phase_float(){ +static float UNUSED_FUNC demo_phase_float() { static int cursor; cursor++; if (cursor > (MAX_PREPARED_RAND - 1)) cursor = 0; @@ -1564,7 +1567,7 @@ static void init_rand_data() { g_randdouble_buff = calloc(1, DOUBLE_BUFF_LEN * MAX_PREPARED_RAND); assert(g_randdouble_buff); - for (int i = 0; i < MAX_PREPARED_RAND; i++){ + for (int i = 0; i < MAX_PREPARED_RAND; i++) { g_randint[i] = (int)(taosRandom() % 65535); sprintf(g_randint_buff + i * INT_BUFF_LEN, "%d", g_randint[i]); @@ -3276,6 +3279,7 @@ static void* createTable(void *sarg) pThreadInfo->db_name, g_args.tb_prefix, i, pThreadInfo->cols); + batchNum ++; } else { if (stbInfo == NULL) { free(pThreadInfo->buffer); @@ -3325,13 +3329,14 @@ static void* createTable(void *sarg) len = 0; if (0 != queryDbExec(pThreadInfo->taos, pThreadInfo->buffer, - NO_INSERT_TYPE, false)){ + NO_INSERT_TYPE, false)) { errorPrint2("queryDbExec() failed. buffer:\n%s\n", pThreadInfo->buffer); free(pThreadInfo->buffer); return NULL; } + pThreadInfo->tables_created += batchNum; - uint64_t currentPrintTime = taosGetTimestampMs(); + uint64_t currentPrintTime = taosGetTimestampMs(); if (currentPrintTime - lastPrintTime > 30*1000) { printf("thread[%d] already create %"PRIu64" - %"PRIu64" tables\n", pThreadInfo->threadID, pThreadInfo->start_table_from, i); @@ -3401,6 +3406,7 @@ static int startMultiThreadCreateChildTable( pThreadInfo->use_metric = true; pThreadInfo->cols = cols; pThreadInfo->minDelay = UINT64_MAX; + pThreadInfo->tables_created = 0; pthread_create(pids + i, NULL, createTable, pThreadInfo); } @@ -3411,6 +3417,8 @@ static int startMultiThreadCreateChildTable( for (int i = 0; i < threads; i++) { threadInfo *pThreadInfo = infos + i; taos_close(pThreadInfo->taos); + + g_actualChildTables += pThreadInfo->tables_created; } free(pids); @@ -3437,7 +3445,6 @@ static void createChildTables() { verbosePrint("%s() LN%d: %s\n", __func__, __LINE__, g_Dbs.db[i].superTbls[j].colsOfCreateChildTable); uint64_t startFrom = 0; - g_totalChildTables += g_Dbs.db[i].superTbls[j].childTblCount; verbosePrint("%s() LN%d: create %"PRId64" child tables from %"PRIu64"\n", __func__, __LINE__, g_totalChildTables, startFrom); @@ -4232,6 +4239,7 @@ static bool getMetaFromInsertJsonFile(cJSON* root) { goto PARSE_OVER; } g_Dbs.db[i].superTbls[j].childTblCount = count->valueint; + g_totalChildTables += g_Dbs.db[i].superTbls[j].childTblCount; cJSON *dataSource = cJSON_GetObjectItem(stbInfo, "data_source"); if (dataSource && dataSource->type == cJSON_String @@ -4936,7 +4944,7 @@ static bool getMetaFromQueryJsonFile(cJSON* root) { cJSON *result = cJSON_GetObjectItem(sql, "result"); if (result != NULL && result->type == cJSON_String - && result->valuestring != NULL){ + && result->valuestring != NULL) { tstrncpy(g_queryInfo.superQueryInfo.result[j], result->valuestring, MAX_FILE_NAME_LEN); } else if (NULL == result) { @@ -7586,7 +7594,7 @@ static void startMultiThreadInsertData(int threads, char* db_name, } int ret = taos_stmt_prepare(pThreadInfo->stmt, stmtBuffer, 0); - if (ret != 0){ + if (ret != 0) { free(pids); free(infos); free(stmtBuffer); @@ -7932,18 +7940,30 @@ static int insertTestProcess() { double start; double end; - // create child tables - start = taosGetTimestampMs(); - createChildTables(); - end = taosGetTimestampMs(); - if (g_totalChildTables > 0) { - fprintf(stderr, "Spent %.4f seconds to create %"PRId64" tables with %d thread(s)\n\n", - (end - start)/1000.0, g_totalChildTables, g_Dbs.threadCountByCreateTbl); + fprintf(stderr, + "creating %"PRId64" table(s) with %d thread(s)\n\n", + g_totalChildTables, g_Dbs.threadCountByCreateTbl); + if (g_fpOfInsertResult) { + fprintf(g_fpOfInsertResult, + "creating %"PRId64" table(s) with %d thread(s)\n\n", + g_totalChildTables, g_Dbs.threadCountByCreateTbl); + } + + // create child tables + start = taosGetTimestampMs(); + createChildTables(); + end = taosGetTimestampMs(); + + fprintf(stderr, + "\nSpent %.4f seconds to create %"PRId64" table(s) with %d thread(s), actual %"PRId64" table(s) created\n\n", + (end - start)/1000.0, g_totalChildTables, + g_Dbs.threadCountByCreateTbl, g_actualChildTables); if (g_fpOfInsertResult) { fprintf(g_fpOfInsertResult, - "Spent %.4f seconds to create %"PRId64" tables with %d thread(s)\n\n", - (end - start)/1000.0, g_totalChildTables, g_Dbs.threadCountByCreateTbl); + "\nSpent %.4f seconds to create %"PRId64" table(s) with %d thread(s), actual %"PRId64" table(s) created\n\n", + (end - start)/1000.0, g_totalChildTables, + g_Dbs.threadCountByCreateTbl, g_actualChildTables); } } diff --git a/src/tsdb/src/tsdbRead.c b/src/tsdb/src/tsdbRead.c index fe5ad89312becfac1d08a663057805c8667b52bc..71a9009c4581dae86861a67de850e275cae352f7 100644 --- a/src/tsdb/src/tsdbRead.c +++ b/src/tsdb/src/tsdbRead.c @@ -1572,7 +1572,7 @@ static void mergeTwoRowFromMem(STsdbQueryHandle* pQueryHandle, int32_t capacity, int32_t numOfColsOfRow1 = 0; if (pSchema1 == NULL) { - pSchema1 = tsdbGetTableSchemaByVersion(pTable, dataRowVersion(row1)); + pSchema1 = tsdbGetTableSchemaByVersion(pTable, memRowVersion(row1)); } if(isRow1DataRow) { numOfColsOfRow1 = schemaNCols(pSchema1); @@ -1584,7 +1584,7 @@ static void mergeTwoRowFromMem(STsdbQueryHandle* pQueryHandle, int32_t capacity, if(row2) { isRow2DataRow = isDataRow(row2); if (pSchema2 == NULL) { - pSchema2 = tsdbGetTableSchemaByVersion(pTable, dataRowVersion(row2)); + pSchema2 = tsdbGetTableSchemaByVersion(pTable, memRowVersion(row2)); } if(isRow2DataRow) { numOfColsOfRow2 = schemaNCols(pSchema2); diff --git a/src/util/inc/tnettest.h b/src/util/inc/tnettest.h index b7585bd7155421d1f22e5f989dc7d1ae6f8be491..8a03b67628ffd460a4aa95ad4de8110b71472496 100644 --- a/src/util/inc/tnettest.h +++ b/src/util/inc/tnettest.h @@ -20,7 +20,7 @@ extern "C" { #endif -void taosNetTest(char *role, char *host, int port, int pkgLen); +void taosNetTest(char *role, char *host, int32_t port, int32_t pkgLen, int32_t pkgNum, char *pkgType); #ifdef __cplusplus } diff --git a/src/util/src/tnettest.c b/src/util/src/tnettest.c index 0bab7b7e6623ebaa5de6d6511fa1c43719372ef5..ed91f95180d8ce4a03e0cce02e64b324bcee59d9 100644 --- a/src/util/src/tnettest.c +++ b/src/util/src/tnettest.c @@ -27,6 +27,10 @@ #include "syncMsg.h" #define MAX_PKG_LEN (64 * 1000) +#define MAX_SPEED_PKG_LEN (1024 * 1024 * 1024) +#define MIN_SPEED_PKG_LEN 1024 +#define MAX_SPEED_PKG_NUM 10000 +#define MIN_SPEED_PKG_NUM 1 #define BUFFER_SIZE (MAX_PKG_LEN + 1024) extern int32_t tsRpcMaxUdpSize; @@ -466,6 +470,7 @@ static void taosNetTestRpc(char *host, int32_t startPort, int32_t pkgLen) { sendpkgLen = pkgLen; } + tsRpcForceTcp = 1; int32_t ret = taosNetCheckRpc(host, port, sendpkgLen, spi, NULL); if (ret < 0) { printf("failed to test TCP port:%d\n", port); @@ -479,6 +484,7 @@ static void taosNetTestRpc(char *host, int32_t startPort, int32_t pkgLen) { sendpkgLen = pkgLen; } + tsRpcForceTcp = 0; ret = taosNetCheckRpc(host, port, pkgLen, spi, NULL); if (ret < 0) { printf("failed to test UDP port:%d\n", port); @@ -542,12 +548,110 @@ static void taosNetTestServer(char *host, int32_t startPort, int32_t pkgLen) { } } -void taosNetTest(char *role, char *host, int32_t port, int32_t pkgLen) { +static void taosNetTestFqdn(char *host) { + int code = 0; + uint64_t startTime = taosGetTimestampUs(); + uint32_t ip = taosGetIpv4FromFqdn(host); + if (ip == 0xffffffff) { + uError("failed to get IP address from %s since %s", host, strerror(errno)); + code = -1; + } + uint64_t endTime = taosGetTimestampUs(); + uint64_t el = endTime - startTime; + printf("check convert fqdn spend, status: %d\tcost: %" PRIu64 " us\n", code, el); + return; +} + +static void taosNetCheckSpeed(char *host, int32_t port, int32_t pkgLen, + int32_t pkgNum, char *pkgType) { + // record config + int32_t compressTmp = tsCompressMsgSize; + int32_t maxUdpSize = tsRpcMaxUdpSize; + int32_t forceTcp = tsRpcForceTcp; + + if (0 == strcmp("tcp", pkgType)){ + tsRpcForceTcp = 1; + tsRpcMaxUdpSize = 0; // force tcp + } else { + tsRpcForceTcp = 0; + tsRpcMaxUdpSize = INT_MAX; + } + tsCompressMsgSize = -1; + + SRpcEpSet epSet; + SRpcMsg reqMsg; + SRpcMsg rspMsg; + void * pRpcConn; + char secretEncrypt[32] = {0}; + char spi = 0; + pRpcConn = taosNetInitRpc(secretEncrypt, spi); + if (NULL == pRpcConn) { + uError("failed to init client rpc"); + return; + } + printf("check net spend, host:%s port:%d pkgLen:%d pkgNum:%d pkgType:%s\n\n", host, port, pkgLen, pkgNum, pkgType); + int32_t totalSucc = 0; + uint64_t startT = taosGetTimestampUs(); + for (int32_t i = 1; i <= pkgNum; i++) { + uint64_t startTime = taosGetTimestampUs(); + + memset(&epSet, 0, sizeof(SRpcEpSet)); + epSet.inUse = 0; + epSet.numOfEps = 1; + epSet.port[0] = port; + strcpy(epSet.fqdn[0], host); + + reqMsg.msgType = TSDB_MSG_TYPE_NETWORK_TEST; + reqMsg.pCont = rpcMallocCont(pkgLen); + reqMsg.contLen = pkgLen; + reqMsg.code = 0; + reqMsg.handle = NULL; // rpc handle returned to app + reqMsg.ahandle = NULL; // app handle set by client + strcpy(reqMsg.pCont, "nettest speed"); + + rpcSendRecv(pRpcConn, &epSet, &reqMsg, &rspMsg); + + int code = 0; + if ((rspMsg.code != 0) || (rspMsg.msgType != TSDB_MSG_TYPE_NETWORK_TEST + 1)) { + uError("ret code 0x%x %s", rspMsg.code, tstrerror(rspMsg.code)); + code = -1; + }else{ + totalSucc ++; + } + + rpcFreeCont(rspMsg.pCont); + + uint64_t endTime = taosGetTimestampUs(); + uint64_t el = endTime - startTime; + printf("progress:%5d/%d\tstatus:%d\tcost:%8.2lf ms\tspeed:%8.2lf MB/s\n", i, pkgNum, code, el/1000.0, pkgLen/(el/1000000.0)/1024.0/1024.0); + } + int64_t endT = taosGetTimestampUs(); + uint64_t elT = endT - startT; + printf("\ntotal succ:%5d/%d\tcost:%8.2lf ms\tspeed:%8.2lf MB/s\n", totalSucc, pkgNum, elT/1000.0, pkgLen/(elT/1000000.0)/1024.0/1024.0*totalSucc); + + rpcClose(pRpcConn); + + // return config + tsCompressMsgSize = compressTmp; + tsRpcMaxUdpSize = maxUdpSize; + tsRpcForceTcp = forceTcp; + return; +} + +void taosNetTest(char *role, char *host, int32_t port, int32_t pkgLen, + int32_t pkgNum, char *pkgType) { tscEmbedded = 1; if (host == NULL) host = tsLocalFqdn; if (port == 0) port = tsServerPort; - if (pkgLen <= 10) pkgLen = 1000; - if (pkgLen > MAX_PKG_LEN) pkgLen = MAX_PKG_LEN; + if (0 == strcmp("speed", role)){ + if (pkgLen <= MIN_SPEED_PKG_LEN) pkgLen = MIN_SPEED_PKG_LEN; + if (pkgLen > MAX_SPEED_PKG_LEN) pkgLen = MAX_SPEED_PKG_LEN; + if (pkgNum <= MIN_SPEED_PKG_NUM) pkgNum = MIN_SPEED_PKG_NUM; + if (pkgNum > MAX_SPEED_PKG_NUM) pkgNum = MAX_SPEED_PKG_NUM; + }else{ + if (pkgLen <= 10) pkgLen = 1000; + if (pkgLen > MAX_PKG_LEN) pkgLen = MAX_PKG_LEN; + } if (0 == strcmp("client", role)) { taosNetTestClient(host, port, pkgLen); @@ -560,6 +664,12 @@ void taosNetTest(char *role, char *host, int32_t port, int32_t pkgLen) { taosNetCheckSync(host, port); } else if (0 == strcmp("startup", role)) { taosNetTestStartup(host, port); + } else if (0 == strcmp("speed", role)) { + tscEmbedded = 0; + char type[10] = {0}; + taosNetCheckSpeed(host, port, pkgLen, pkgNum, strtolower(type, pkgType)); + }else if (0 == strcmp("fqdn", role)) { + taosNetTestFqdn(host); } else { taosNetTestStartup(host, port); } diff --git a/tests/examples/c/apitest.c b/tests/examples/c/apitest.c index 01169715f3e8b5b9d6e212b4b317ecca5fa4dbcd..22ae419a94107ea4601e3f5d36dc436fa21d6d87 100644 --- a/tests/examples/c/apitest.c +++ b/tests/examples/c/apitest.c @@ -8,7 +8,7 @@ #include static void prepare_data(TAOS* taos) { - TAOS_RES *result; + TAOS_RES* result; result = taos_query(taos, "drop database if exists test;"); taos_free_result(result); usleep(100000); @@ -41,24 +41,25 @@ static void prepare_data(TAOS* taos) { result = taos_query(taos, "create table t9 using meters tags(9);"); taos_free_result(result); - result = taos_query(taos, "insert into t0 values('2020-01-01 00:00:00.000', 0)" - " ('2020-01-01 00:01:00.000', 0)" - " ('2020-01-01 00:02:00.000', 0)" - " t1 values('2020-01-01 00:00:00.000', 0)" - " ('2020-01-01 00:01:00.000', 0)" - " ('2020-01-01 00:02:00.000', 0)" - " ('2020-01-01 00:03:00.000', 0)" - " t2 values('2020-01-01 00:00:00.000', 0)" - " ('2020-01-01 00:01:00.000', 0)" - " ('2020-01-01 00:01:01.000', 0)" - " ('2020-01-01 00:01:02.000', 0)" - " t3 values('2020-01-01 00:01:02.000', 0)" - " t4 values('2020-01-01 00:01:02.000', 0)" - " t5 values('2020-01-01 00:01:02.000', 0)" - " t6 values('2020-01-01 00:01:02.000', 0)" - " t7 values('2020-01-01 00:01:02.000', 0)" - " t8 values('2020-01-01 00:01:02.000', 0)" - " t9 values('2020-01-01 00:01:02.000', 0)"); + result = taos_query(taos, + "insert into t0 values('2020-01-01 00:00:00.000', 0)" + " ('2020-01-01 00:01:00.000', 0)" + " ('2020-01-01 00:02:00.000', 0)" + " t1 values('2020-01-01 00:00:00.000', 0)" + " ('2020-01-01 00:01:00.000', 0)" + " ('2020-01-01 00:02:00.000', 0)" + " ('2020-01-01 00:03:00.000', 0)" + " t2 values('2020-01-01 00:00:00.000', 0)" + " ('2020-01-01 00:01:00.000', 0)" + " ('2020-01-01 00:01:01.000', 0)" + " ('2020-01-01 00:01:02.000', 0)" + " t3 values('2020-01-01 00:01:02.000', 0)" + " t4 values('2020-01-01 00:01:02.000', 0)" + " t5 values('2020-01-01 00:01:02.000', 0)" + " t6 values('2020-01-01 00:01:02.000', 0)" + " t7 values('2020-01-01 00:01:02.000', 0)" + " t8 values('2020-01-01 00:01:02.000', 0)" + " t9 values('2020-01-01 00:01:02.000', 0)"); int affected = taos_affected_rows(result); if (affected != 18) { printf("\033[31m%d rows affected by last insert statement, but it should be 18\033[0m\n", affected); @@ -77,7 +78,7 @@ static int print_result(TAOS_RES* res, int blockFetch) { if (blockFetch) { int rows = 0; while ((rows = taos_fetch_block(res, &row))) { - //for (int i = 0; i < rows; i++) { + // for (int i = 0; i < rows; i++) { // char temp[256]; // taos_print_row(temp, row + i, fields, num_fields); // puts(temp); @@ -126,9 +127,9 @@ static void verify_query(TAOS* taos) { TAOS_RES* res = taos_query(taos, "select * from meters"); check_row_count(__LINE__, res, 18); - printf("result precision is: %d\n", taos_result_precision(res)); + printf("result precision is: %d\n", taos_result_precision(res)); int c = taos_field_count(res); - printf("field count is: %d\n", c); + printf("field count is: %d\n", c); int* lengths = taos_fetch_lengths(res); for (int i = 0; i < c; i++) { printf("length of column %d is %d\n", i, lengths[i]); @@ -149,7 +150,7 @@ static void verify_query(TAOS* taos) { taos_free_result(res); } -void subscribe_callback(TAOS_SUB* tsub, TAOS_RES *res, void* param, int code) { +void subscribe_callback(TAOS_SUB* tsub, TAOS_RES* res, void* param, int code) { int rows = print_result(res, *(int*)param); printf("%d rows consumed in subscribe_callback\n", rows); } @@ -164,7 +165,7 @@ static void verify_subscribe(TAOS* taos) { res = taos_consume(tsub); check_row_count(__LINE__, res, 0); - TAOS_RES *result; + TAOS_RES* result; result = taos_query(taos, "insert into t0 values('2020-01-01 00:02:00.001', 0);"); taos_free_result(result); result = taos_query(taos, "insert into t8 values('2020-01-01 00:01:03.000', 0);"); @@ -250,7 +251,9 @@ void verify_prepare(TAOS* taos) { taos_select_db(taos, "test"); // create table - const char* sql = "create table m1 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin binary(40), blob nchar(10))"; + const char* sql = + "create table m1 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin " + "binary(40), blob nchar(10), u1 tinyint unsigned, u2 smallint unsigned, u4 int unsigned, u8 bigint unsigned)"; result = taos_query(taos, sql); code = taos_errno(result); if (code != 0) { @@ -262,20 +265,24 @@ void verify_prepare(TAOS* taos) { // insert 10 records struct { - int64_t ts; - int8_t b; - int8_t v1; - int16_t v2; - int32_t v4; - int64_t v8; - float f4; - double f8; - char bin[40]; - char blob[80]; + int64_t ts; + int8_t b; + int8_t v1; + int16_t v2; + int32_t v4; + int64_t v8; + float f4; + double f8; + char bin[40]; + char blob[80]; + uint8_t u1; + uint16_t u2; + uint32_t u4; + uint64_t u8; } v = {0}; TAOS_STMT* stmt = taos_stmt_init(taos); - TAOS_BIND params[10]; + TAOS_BIND params[14]; params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP; params[0].buffer_length = sizeof(v.ts); params[0].buffer = &v.ts; @@ -337,11 +344,35 @@ void verify_prepare(TAOS* taos) { params[9].length = ¶ms[9].buffer_length; params[9].is_null = NULL; + params[10].buffer_type = TSDB_DATA_TYPE_UTINYINT; + params[10].buffer_length = sizeof(v.u1); + params[10].buffer = &v.u1; + params[10].length = ¶ms[10].buffer_length; + params[10].is_null = NULL; + + params[11].buffer_type = TSDB_DATA_TYPE_USMALLINT; + params[11].buffer_length = sizeof(v.u2); + params[11].buffer = &v.u2; + params[11].length = ¶ms[11].buffer_length; + params[11].is_null = NULL; + + params[12].buffer_type = TSDB_DATA_TYPE_UINT; + params[12].buffer_length = sizeof(v.u4); + params[12].buffer = &v.u4; + params[12].length = ¶ms[12].buffer_length; + params[12].is_null = NULL; + + params[13].buffer_type = TSDB_DATA_TYPE_UBIGINT; + params[13].buffer_length = sizeof(v.u8); + params[13].buffer = &v.u8; + params[13].length = ¶ms[13].buffer_length; + params[13].is_null = NULL; + int is_null = 1; - sql = "insert into m1 values(?,?,?,?,?,?,?,?,?,?)"; + sql = "insert into m1 values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; code = taos_stmt_prepare(stmt, sql, 0); - if (code != 0){ + if (code != 0) { printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt)); taos_stmt_close(stmt); return; @@ -362,6 +393,10 @@ void verify_prepare(TAOS* taos) { for (int j = 0; j < sizeof(v.bin); ++j) { v.bin[j] = (char)(i + '0'); } + v.u1 = (uint8_t)i; + v.u2 = (uint16_t)(i * 2); + v.u4 = (uint32_t)(i * 4); + v.u8 = (uint64_t)(i * 8); taos_stmt_bind_param(stmt, params); taos_stmt_add_batch(stmt); @@ -390,7 +425,7 @@ void verify_prepare(TAOS* taos) { TAOS_ROW row; int rows = 0; int num_fields = taos_num_fields(result); - TAOS_FIELD *fields = taos_fetch_fields(result); + TAOS_FIELD* fields = taos_fetch_fields(result); // fetch the records row by row while ((row = taos_fetch_row(result))) { @@ -422,7 +457,9 @@ void verify_prepare2(TAOS* taos) { taos_select_db(taos, "test"); // create table - const char* sql = "create table m1 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin binary(40), blob nchar(10))"; + const char* sql = + "create table m1 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin " + "binary(40), blob nchar(10))"; result = taos_query(taos, sql); code = taos_errno(result); if (code != 0) { @@ -434,31 +471,31 @@ void verify_prepare2(TAOS* taos) { // insert 10 records struct { - int64_t ts[10]; - int8_t b[10]; - int8_t v1[10]; - int16_t v2[10]; - int32_t v4[10]; - int64_t v8[10]; - float f4[10]; - double f8[10]; - char bin[10][40]; - char blob[10][80]; + int64_t ts[10]; + int8_t b[10]; + int8_t v1[10]; + int16_t v2[10]; + int32_t v4[10]; + int64_t v8[10]; + float f4[10]; + double f8[10]; + char bin[10][40]; + char blob[10][80]; } v; - int32_t *t8_len = malloc(sizeof(int32_t) * 10); - int32_t *t16_len = malloc(sizeof(int32_t) * 10); - int32_t *t32_len = malloc(sizeof(int32_t) * 10); - int32_t *t64_len = malloc(sizeof(int32_t) * 10); - int32_t *float_len = malloc(sizeof(int32_t) * 10); - int32_t *double_len = malloc(sizeof(int32_t) * 10); - int32_t *bin_len = malloc(sizeof(int32_t) * 10); - int32_t *blob_len = malloc(sizeof(int32_t) * 10); + int32_t* t8_len = malloc(sizeof(int32_t) * 10); + int32_t* t16_len = malloc(sizeof(int32_t) * 10); + int32_t* t32_len = malloc(sizeof(int32_t) * 10); + int32_t* t64_len = malloc(sizeof(int32_t) * 10); + int32_t* float_len = malloc(sizeof(int32_t) * 10); + int32_t* double_len = malloc(sizeof(int32_t) * 10); + int32_t* bin_len = malloc(sizeof(int32_t) * 10); + int32_t* blob_len = malloc(sizeof(int32_t) * 10); - TAOS_STMT* stmt = taos_stmt_init(taos); + TAOS_STMT* stmt = taos_stmt_init(taos); TAOS_MULTI_BIND params[10]; - char is_null[10] = {0}; - + char is_null[10] = {0}; + params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP; params[0].buffer_length = sizeof(v.ts[0]); params[0].buffer = v.ts; @@ -538,12 +575,12 @@ void verify_prepare2(TAOS* taos) { } code = taos_stmt_set_tbname(stmt, "m1"); - if (code != 0){ + if (code != 0) { printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt)); taos_stmt_close(stmt); return; } - + int64_t ts = 1591060628000; for (int i = 0; i < 10; ++i) { v.ts[i] = ts++; @@ -558,7 +595,7 @@ void verify_prepare2(TAOS* taos) { v.f8[i] = (double)(i * 80); for (int j = 0; j < sizeof(v.bin[0]); ++j) { v.bin[i][j] = (char)(i + '0'); - } + } strcpy(v.blob[i], "一二三四五六七八九十"); t8_len[i] = sizeof(int8_t); @@ -573,7 +610,7 @@ void verify_prepare2(TAOS* taos) { taos_stmt_bind_param_batch(stmt, params); taos_stmt_add_batch(stmt); - + if (taos_stmt_execute(stmt) != 0) { printf("\033[31mfailed to execute insert statement.error:%s\033[0m\n", taos_stmt_errstr(stmt)); taos_stmt_close(stmt); @@ -587,7 +624,7 @@ void verify_prepare2(TAOS* taos) { taos_stmt_prepare(stmt, "SELECT * FROM m1 WHERE v1 > ? AND v2 < ?", 0); TAOS_BIND qparams[2]; - int8_t v1 = 5; + int8_t v1 = 5; int16_t v2 = 15; qparams[0].buffer_type = TSDB_DATA_TYPE_TINYINT; qparams[0].buffer_length = sizeof(v1); @@ -604,7 +641,7 @@ void verify_prepare2(TAOS* taos) { taos_stmt_bind_param(stmt, qparams); if (taos_stmt_execute(stmt) != 0) { printf("\033[31mfailed to execute select statement.error:%s\033[0m\n", taos_stmt_errstr(stmt)); - taos_stmt_close(stmt); + taos_stmt_close(stmt); return; } @@ -613,7 +650,7 @@ void verify_prepare2(TAOS* taos) { TAOS_ROW row; int rows = 0; int num_fields = taos_num_fields(result); - TAOS_FIELD *fields = taos_fetch_fields(result); + TAOS_FIELD* fields = taos_fetch_fields(result); // fetch the records row by row while ((row = taos_fetch_row(result))) { @@ -654,7 +691,9 @@ void verify_prepare3(TAOS* taos) { taos_select_db(taos, "test"); // create table - const char* sql = "create stable st1 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin binary(40), blob nchar(10)) tags (id1 int, id2 binary(40))"; + const char* sql = + "create stable st1 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin " + "binary(40), blob nchar(10)) tags (id1 int, id2 binary(40))"; result = taos_query(taos, sql); code = taos_errno(result); if (code != 0) { @@ -666,10 +705,10 @@ void verify_prepare3(TAOS* taos) { TAOS_BIND tags[2]; - int32_t id1 = 1; - char id2[40] = "abcdefghijklmnopqrstuvwxyz0123456789"; + int32_t id1 = 1; + char id2[40] = "abcdefghijklmnopqrstuvwxyz0123456789"; uintptr_t id2_len = strlen(id2); - + tags[0].buffer_type = TSDB_DATA_TYPE_INT; tags[0].buffer_length = sizeof(int); tags[0].buffer = &id1; @@ -682,34 +721,33 @@ void verify_prepare3(TAOS* taos) { tags[1].length = &id2_len; tags[1].is_null = NULL; - // insert 10 records struct { - int64_t ts[10]; - int8_t b[10]; - int8_t v1[10]; - int16_t v2[10]; - int32_t v4[10]; - int64_t v8[10]; - float f4[10]; - double f8[10]; - char bin[10][40]; - char blob[10][80]; + int64_t ts[10]; + int8_t b[10]; + int8_t v1[10]; + int16_t v2[10]; + int32_t v4[10]; + int64_t v8[10]; + float f4[10]; + double f8[10]; + char bin[10][40]; + char blob[10][80]; } v; - int32_t *t8_len = malloc(sizeof(int32_t) * 10); - int32_t *t16_len = malloc(sizeof(int32_t) * 10); - int32_t *t32_len = malloc(sizeof(int32_t) * 10); - int32_t *t64_len = malloc(sizeof(int32_t) * 10); - int32_t *float_len = malloc(sizeof(int32_t) * 10); - int32_t *double_len = malloc(sizeof(int32_t) * 10); - int32_t *bin_len = malloc(sizeof(int32_t) * 10); - int32_t *blob_len = malloc(sizeof(int32_t) * 10); + int32_t* t8_len = malloc(sizeof(int32_t) * 10); + int32_t* t16_len = malloc(sizeof(int32_t) * 10); + int32_t* t32_len = malloc(sizeof(int32_t) * 10); + int32_t* t64_len = malloc(sizeof(int32_t) * 10); + int32_t* float_len = malloc(sizeof(int32_t) * 10); + int32_t* double_len = malloc(sizeof(int32_t) * 10); + int32_t* bin_len = malloc(sizeof(int32_t) * 10); + int32_t* blob_len = malloc(sizeof(int32_t) * 10); - TAOS_STMT* stmt = taos_stmt_init(taos); + TAOS_STMT* stmt = taos_stmt_init(taos); TAOS_MULTI_BIND params[10]; - char is_null[10] = {0}; - + char is_null[10] = {0}; + params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP; params[0].buffer_length = sizeof(v.ts[0]); params[0].buffer = v.ts; @@ -780,27 +818,26 @@ void verify_prepare3(TAOS* taos) { params[9].is_null = is_null; params[9].num = 10; - sql = "insert into ? using st1 tags(?,?) values(?,?,?,?,?,?,?,?,?,?)"; code = taos_stmt_prepare(stmt, sql, 0); - if (code != 0){ + if (code != 0) { printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt)); taos_stmt_close(stmt); - return; + return; } code = taos_stmt_set_tbname_tags(stmt, "m1", tags); - if (code != 0){ + if (code != 0) { printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt)); - taos_stmt_close(stmt); + taos_stmt_close(stmt); return; } - + int64_t ts = 1591060628000; for (int i = 0; i < 10; ++i) { v.ts[i] = ts++; is_null[i] = 0; - + v.b[i] = (int8_t)i % 2; v.v1[i] = (int8_t)i; v.v2[i] = (int16_t)(i * 2); @@ -810,7 +847,7 @@ void verify_prepare3(TAOS* taos) { v.f8[i] = (double)(i * 80); for (int j = 0; j < sizeof(v.bin[0]); ++j) { v.bin[i][j] = (char)(i + '0'); - } + } strcpy(v.blob[i], "一二三四五六七八九十"); t8_len[i] = sizeof(int8_t); @@ -825,10 +862,10 @@ void verify_prepare3(TAOS* taos) { taos_stmt_bind_param_batch(stmt, params); taos_stmt_add_batch(stmt); - + if (taos_stmt_execute(stmt) != 0) { printf("\033[31mfailed to execute insert statement.error:%s\033[0m\n", taos_stmt_errstr(stmt)); - taos_stmt_close(stmt); + taos_stmt_close(stmt); return; } taos_stmt_close(stmt); @@ -839,7 +876,7 @@ void verify_prepare3(TAOS* taos) { TAOS_BIND qparams[2]; - int8_t v1 = 5; + int8_t v1 = 5; int16_t v2 = 15; qparams[0].buffer_type = TSDB_DATA_TYPE_TINYINT; qparams[0].buffer_length = sizeof(v1); @@ -856,7 +893,7 @@ void verify_prepare3(TAOS* taos) { taos_stmt_bind_param(stmt, qparams); if (taos_stmt_execute(stmt) != 0) { printf("\033[31mfailed to execute select statement.error:%s\033[0m\n", taos_stmt_errstr(stmt)); - taos_stmt_close(stmt); + taos_stmt_close(stmt); return; } @@ -865,7 +902,7 @@ void verify_prepare3(TAOS* taos) { TAOS_ROW row; int rows = 0; int num_fields = taos_num_fields(result); - TAOS_FIELD *fields = taos_fetch_fields(result); + TAOS_FIELD* fields = taos_fetch_fields(result); // fetch the records row by row while ((row = taos_fetch_row(result))) { @@ -888,8 +925,7 @@ void verify_prepare3(TAOS* taos) { free(blob_len); } -void retrieve_callback(void *param, TAOS_RES *tres, int numOfRows) -{ +void retrieve_callback(void* param, TAOS_RES* tres, int numOfRows) { if (numOfRows > 0) { printf("%d rows async retrieved\n", numOfRows); taos_fetch_rows_a(tres, retrieve_callback, param); @@ -903,8 +939,7 @@ void retrieve_callback(void *param, TAOS_RES *tres, int numOfRows) } } -void select_callback(void *param, TAOS_RES *tres, int code) -{ +void select_callback(void* param, TAOS_RES* tres, int code) { if (code == 0 && tres) { taos_fetch_rows_a(tres, retrieve_callback, param); } else { @@ -918,11 +953,11 @@ void verify_async(TAOS* taos) { usleep(1000000); } -void stream_callback(void *param, TAOS_RES *res, TAOS_ROW row) { +void stream_callback(void* param, TAOS_RES* res, TAOS_ROW row) { if (res == NULL || row == NULL) { return; } - + int num_fields = taos_num_fields(res); TAOS_FIELD* fields = taos_fetch_fields(res); @@ -934,14 +969,9 @@ void stream_callback(void *param, TAOS_RES *res, TAOS_ROW row) { void verify_stream(TAOS* taos) { prepare_data(taos); - TAOS_STREAM* strm = taos_open_stream( - taos, - "select count(*) from meters interval(1m)", - stream_callback, - 0, - NULL, - NULL); - printf("waiting for stream data\n"); + TAOS_STREAM* strm = + taos_open_stream(taos, "select count(*) from meters interval(1m)", stream_callback, 0, NULL, NULL); + printf("waiting for stream data\n"); usleep(100000); TAOS_RES* result = taos_query(taos, "insert into t0 values(now, 0)(now+5s,1)(now+10s, 2);"); taos_free_result(result); @@ -950,7 +980,7 @@ void verify_stream(TAOS* taos) { } int32_t verify_schema_less(TAOS* taos) { - TAOS_RES *result; + TAOS_RES* result; result = taos_query(taos, "drop database if exists test;"); taos_free_result(result); usleep(100000); @@ -972,49 +1002,51 @@ int32_t verify_schema_less(TAOS* taos) { "st,t1=4i64,t2=5f64,t3=\"t4\" c1=3i64,c3=L\"passitagain\",c2=true,c4=5f64 1626006833642000000ns", "ste,t2=5f64,t3=L\"ste2\" c3=\"iamszhou\",c4=false 1626056811843316532ns", "ste,t2=5f64,t3=L\"ste2\" c3=\"iamszhou\",c4=false,c5=32i8,c6=64i16,c7=32i32,c8=88.88f32 1626056812843316532ns", - "st,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64,c6=7u64 1626006933640000000ns", - "stf,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64,c6=7u64 1626006933640000000ns", - "stf,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin_stf\",c2=false,c5=5f64,c6=7u64 1626006933641000000ns" - }; + "st,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64,c6=7u64 " + "1626006933640000000ns", + "stf,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64,c6=7u64 " + "1626006933640000000ns", + "stf,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin_stf\",c2=false,c5=5f64,c6=7u64 " + "1626006933641000000ns"}; - code = taos_insert_lines(taos, lines , sizeof(lines)/sizeof(char*)); + code = taos_insert_lines(taos, lines, sizeof(lines) / sizeof(char*)); char* lines2[] = { "stg,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000ns", - "stg,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64 1626006833640000000ns" - }; + "stg,t1=4i64,t3=\"t4\",t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin\",c2=true,c4=5f64,c5=5f64 1626006833640000000ns"}; code = taos_insert_lines(taos, &lines2[0], 1); code = taos_insert_lines(taos, &lines2[1], 1); char* lines3[] = { - "sth,t1=4i64,t2=5f64,t4=5f64,ID=\"childtable\" c1=3i64,c3=L\"passitagin_stf\",c2=false,c5=5f64,c6=7u64 1626006933641ms", - "sth,t1=4i64,t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin_stf\",c2=false,c5=5f64,c6=7u64 1626006933654ms" - }; + "sth,t1=4i64,t2=5f64,t4=5f64,ID=\"childtable\" c1=3i64,c3=L\"passitagin_stf\",c2=false,c5=5f64,c6=7u64 " + "1626006933641ms", + "sth,t1=4i64,t2=5f64,t4=5f64 c1=3i64,c3=L\"passitagin_stf\",c2=false,c5=5f64,c6=7u64 1626006933654ms"}; code = taos_insert_lines(taos, lines3, 2); - char* lines4[] = { - "st123456,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000ns", - "dgtyqodr,t2=5f64,t3=L\"ste\" c1=tRue,c2=4i64,c3=\"iam\" 1626056811823316532ns" - }; + char* lines4[] = {"st123456,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000ns", + "dgtyqodr,t2=5f64,t3=L\"ste\" c1=tRue,c2=4i64,c3=\"iam\" 1626056811823316532ns"}; code = taos_insert_lines(taos, lines4, 2); char* lines5[] = { - "zqlbgs,id=\"zqlbgs_39302_21680\",t0=f,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t5=11.12345f32,t6=22.123456789f64,t7=\"binaryTagValue\",t8=L\"ncharTagValue\" c0=f,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=11.12345f32,c6=22.123456789f64,c7=\"binaryColValue\",c8=L\"ncharColValue\",c9=7u64 1626006833639000000ns", - "zqlbgs,t9=f,id=\"zqlbgs_39302_21680\",t0=f,t1=127i8,t11=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t5=11.12345f32,t6=22.123456789f64,t7=\"binaryTagValue\",t8=L\"ncharTagValue\",t10=L\"ncharTagValue\" c10=f,c0=f,c1=127i8,c12=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=11.12345f32,c6=22.123456789f64,c7=\"binaryColValue\",c8=L\"ncharColValue\",c9=7u64,c11=L\"ncharColValue\" 1626006833639000000ns" - }; + "zqlbgs,id=\"zqlbgs_39302_21680\",t0=f,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t5=11." + "12345f32,t6=22.123456789f64,t7=\"binaryTagValue\",t8=L\"ncharTagValue\" " + "c0=f,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=11.12345f32,c6=22.123456789f64,c7=" + "\"binaryColValue\",c8=L\"ncharColValue\",c9=7u64 1626006833639000000ns", + "zqlbgs,t9=f,id=\"zqlbgs_39302_21680\",t0=f,t1=127i8,t11=127i8,t2=32767i16,t3=2147483647i32,t4=" + "9223372036854775807i64,t5=11.12345f32,t6=22.123456789f64,t7=\"binaryTagValue\",t8=L\"ncharTagValue\",t10=" + "L\"ncharTagValue\" " + "c10=f,c0=f,c1=127i8,c12=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=11.12345f32,c6=22." + "123456789f64,c7=\"binaryColValue\",c8=L\"ncharColValue\",c9=7u64,c11=L\"ncharColValue\" 1626006833639000000ns"}; code = taos_insert_lines(taos, &lines5[0], 1); code = taos_insert_lines(taos, &lines5[1], 1); - - char* lines6[] = { - "st123456,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000ns", - "dgtyqodr,t2=5f64,t3=L\"ste\" c1=tRue,c2=4i64,c3=\"iam\" 1626056811823316532ns" - }; + char* lines6[] = {"st123456,t1=3i64,t2=4f64,t3=\"t3\" c1=3i64,c3=L\"passit\",c2=false,c4=4f64 1626006833639000000ns", + "dgtyqodr,t2=5f64,t3=L\"ste\" c1=tRue,c2=4i64,c3=\"iam\" 1626056811823316532ns"}; code = taos_insert_lines(taos, lines6, 2); return (code); } -int main(int argc, char *argv[]) { +int main(int argc, char* argv[]) { const char* host = "127.0.0.1"; const char* user = "root"; const char* passwd = "taosdata"; @@ -1034,7 +1066,6 @@ int main(int argc, char *argv[]) { printf("************ verify schema-less *************\n"); verify_schema_less(taos); - printf("************ verify query *************\n"); verify_query(taos); @@ -1051,9 +1082,9 @@ int main(int argc, char *argv[]) { verify_prepare2(taos); printf("************ verify prepare3 *************\n"); verify_prepare3(taos); - + printf("************ verify stream *************\n"); - verify_stream(taos); + // verify_stream(taos); printf("done\n"); taos_close(taos); taos_cleanup(); diff --git a/tests/examples/c/asyncdemo.c b/tests/examples/c/asyncdemo.c index f2a96dd8256782960f9ad114229cd47714c9d1d9..78e41ddf5cad70ddb430dfdd5832e92d2800d030 100644 --- a/tests/examples/c/asyncdemo.c +++ b/tests/examples/c/asyncdemo.c @@ -20,9 +20,9 @@ #include #include +#include #include #include -#include #include @@ -33,14 +33,14 @@ int tablesSelectProcessed = 0; int64_t st, et; typedef struct { - int id; - TAOS *taos; - char name[16]; - time_t timeStamp; - int value; - int rowsInserted; - int rowsTried; - int rowsRetrieved; + int id; + TAOS * taos; + char name[16]; + time_t timeStamp; + int value; + int rowsInserted; + int rowsTried; + int rowsRetrieved; } STable; void taos_insert_call_back(void *param, TAOS_RES *tres, int code); @@ -48,7 +48,7 @@ void taos_select_call_back(void *param, TAOS_RES *tres, int code); void taos_error(TAOS *taos); static void queryDB(TAOS *taos, char *command) { - int i; + int i; TAOS_RES *pSql = NULL; int32_t code = -1; @@ -57,12 +57,12 @@ static void queryDB(TAOS *taos, char *command) { taos_free_result(pSql); pSql = NULL; } - + pSql = taos_query(taos, command); code = taos_errno(pSql); if (0 == code) { break; - } + } } if (code != 0) { @@ -76,15 +76,14 @@ static void queryDB(TAOS *taos, char *command) { taos_free_result(pSql); } -int main(int argc, char *argv[]) -{ - TAOS *taos; - struct timeval systemTime; - int i; - char sql[1024] = { 0 }; - char prefix[20] = { 0 }; - char db[128] = { 0 }; - STable *tableList; +int main(int argc, char *argv[]) { + TAOS * taos; + struct timeval systemTime; + int i; + char sql[1024] = {0}; + char prefix[20] = {0}; + char db[128] = {0}; + STable * tableList; if (argc != 5) { printf("usage: %s server-ip dbname rowsPerTable numOfTables\n", argv[0]); @@ -101,8 +100,7 @@ int main(int argc, char *argv[]) memset(tableList, 0, size); taos = taos_connect(argv[1], "root", "taosdata", NULL, 0); - if (taos == NULL) - taos_error(taos); + if (taos == NULL) taos_error(taos); printf("success to connect to server\n"); @@ -122,7 +120,7 @@ int main(int argc, char *argv[]) sprintf(tableList[i].name, "%s%d", prefix, i); sprintf(sql, "create table %s%d (ts timestamp, volume bigint)", prefix, i); queryDB(taos, sql); - } + } gettimeofday(&systemTime, NULL); for (i = 0; i < numOfTables; ++i) @@ -138,7 +136,7 @@ int main(int argc, char *argv[]) tablesInsertProcessed = 0; tablesSelectProcessed = 0; - for (i = 0; irowsTried++; - if (code < 0) { + if (code < 0) { printf("%s insert failed, code:%d, rows:%d\n", pTable->name, code, pTable->rowsTried); - } - else if (code == 0) { + } else if (code == 0) { printf("%s not inserted\n", pTable->name); - } - else { + } else { pTable->rowsInserted++; } if (pTable->rowsTried < points) { // for this demo, insert another record - sprintf(sql, "insert into %s values(%ld, %d)", pTable->name, 1546300800000+pTable->rowsTried*1000, pTable->rowsTried); + sprintf(sql, "insert into %s values(%ld, %d)", pTable->name, 1546300800000 + pTable->rowsTried * 1000, + pTable->rowsTried); taos_query_a(pTable->taos, sql, taos_insert_call_back, (void *)pTable); - } - else { + } else { printf("%d rows data are inserted into %s\n", points, pTable->name); tablesInsertProcessed++; if (tablesInsertProcessed >= numOfTables) { gettimeofday(&systemTime, NULL); et = systemTime.tv_sec * 1000000 + systemTime.tv_usec; - printf("%lld mseconds to insert %d data points\n", (et - st) / 1000, points*numOfTables); + printf("%lld mseconds to insert %d data points\n", (et - st) / 1000, points * numOfTables); } } - + taos_free_result(tres); } -void taos_retrieve_call_back(void *param, TAOS_RES *tres, int numOfRows) -{ - STable *pTable = (STable *)param; +void taos_retrieve_call_back(void *param, TAOS_RES *tres, int numOfRows) { + STable * pTable = (STable *)param; struct timeval systemTime; if (numOfRows > 0) { - - for (int i = 0; iname, numOfRows); + } else { + if (numOfRows < 0) printf("%s retrieve failed, code:%d\n", pTable->name, numOfRows); - //taos_free_result(tres); + // taos_free_result(tres); printf("%d rows data retrieved from %s\n", pTable->rowsRetrieved, pTable->name); tablesSelectProcessed++; @@ -272,19 +261,15 @@ void taos_retrieve_call_back(void *param, TAOS_RES *tres, int numOfRows) taos_free_result(tres); } - - } -void taos_select_call_back(void *param, TAOS_RES *tres, int code) -{ +void taos_select_call_back(void *param, TAOS_RES *tres, int code) { STable *pTable = (STable *)param; if (code == 0 && tres) { // asynchronous API to fetch a batch of records taos_fetch_rows_a(tres, taos_retrieve_call_back, pTable); - } - else { + } else { printf("%s select failed, code:%d\n", pTable->name, code); taos_free_result(tres); taos_cleanup(); diff --git a/tests/examples/c/demo.c b/tests/examples/c/demo.c index f8c69d0043591afa8f5e32e80bd35e9413e60e76..55d962888871c2ba175daef85f1084a1e28a0da1 100644 --- a/tests/examples/c/demo.c +++ b/tests/examples/c/demo.c @@ -16,14 +16,14 @@ // TAOS standard API example. The same syntax as MySQL, but only a subset // to compile: gcc -o demo demo.c -ltaos +#include #include #include #include -#include #include // TAOS header file static void queryDB(TAOS *taos, char *command) { - int i; + int i; TAOS_RES *pSql = NULL; int32_t code = -1; @@ -32,12 +32,12 @@ static void queryDB(TAOS *taos, char *command) { taos_free_result(pSql); pSql = NULL; } - + pSql = taos_query(taos, command); code = taos_errno(pSql); if (0 == code) { break; - } + } } if (code != 0) { @@ -53,7 +53,7 @@ static void queryDB(TAOS *taos, char *command) { void Test(TAOS *taos, char *qstr, int i); int main(int argc, char *argv[]) { - char qstr[1024]; + char qstr[1024]; // connect to server if (argc < 2) { @@ -63,7 +63,7 @@ int main(int argc, char *argv[]) { TAOS *taos = taos_connect(argv[1], "root", "taosdata", NULL, 0); if (taos == NULL) { - printf("failed to connect to server, reason:%s\n", "null taos"/*taos_errstr(taos)*/); + printf("failed to connect to server, reason:%s\n", "null taos" /*taos_errstr(taos)*/); exit(1); } for (int i = 0; i < 100; i++) { @@ -72,28 +72,30 @@ int main(int argc, char *argv[]) { taos_close(taos); taos_cleanup(); } -void Test(TAOS *taos, char *qstr, int index) { +void Test(TAOS *taos, char *qstr, int index) { printf("==================test at %d\n================================", index); queryDB(taos, "drop database if exists demo"); queryDB(taos, "create database demo"); TAOS_RES *result; queryDB(taos, "use demo"); - queryDB(taos, "create table m1 (ts timestamp, ti tinyint, si smallint, i int, bi bigint, f float, d double, b binary(10))"); + queryDB(taos, + "create table m1 (ts timestamp, ti tinyint, si smallint, i int, bi bigint, f float, d double, b binary(10))"); printf("success to create table\n"); int i = 0; for (i = 0; i < 10; ++i) { - sprintf(qstr, "insert into m1 values (%" PRId64 ", %d, %d, %d, %d, %f, %lf, '%s')", (uint64_t)(1546300800000 + i * 1000), i, i, i, i*10000000, i*1.0, i*2.0, "hello"); + sprintf(qstr, "insert into m1 values (%" PRId64 ", %d, %d, %d, %d, %f, %lf, '%s')", + (uint64_t)(1546300800000 + i * 1000), i, i, i, i * 10000000, i * 1.0, i * 2.0, "hello"); printf("qstr: %s\n", qstr); - + // note: how do you wanna do if taos_query returns non-NULL // if (taos_query(taos, qstr)) { // printf("insert row: %i, reason:%s\n", i, taos_errstr(taos)); // } TAOS_RES *result1 = taos_query(taos, qstr); if (result1 == NULL || taos_errno(result1) != 0) { - printf("failed to insert row, reason:%s\n", taos_errstr(result1)); + printf("failed to insert row, reason:%s\n", taos_errstr(result1)); taos_free_result(result1); exit(1); } else { @@ -107,7 +109,7 @@ void Test(TAOS *taos, char *qstr, int index) { sprintf(qstr, "SELECT * FROM m1"); result = taos_query(taos, qstr); if (result == NULL || taos_errno(result) != 0) { - printf("failed to select, reason:%s\n", taos_errstr(result)); + printf("failed to select, reason:%s\n", taos_errstr(result)); taos_free_result(result); exit(1); } @@ -130,4 +132,3 @@ void Test(TAOS *taos, char *qstr, int index) { taos_free_result(result); printf("====demo end====\n\n"); } - diff --git a/tests/examples/c/epoll.c b/tests/examples/c/epoll.c index 284268ac4328b5bc814ab8d30931ec92c5c11523..0fb8754de666d7067ef3dcbf9b7797592ca5b61b 100644 --- a/tests/examples/c/epoll.c +++ b/tests/examples/c/epoll.c @@ -21,103 +21,101 @@ #ifdef __APPLE__ #include "osEok.h" -#else // __APPLE__ +#else // __APPLE__ #include -#endif // __APPLE__ -#include -#include -#include -#include -#include -#include -#include -#include +#endif // __APPLE__ #include -#include #include -#include -#include +#include #include #include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include #define D(fmt, ...) fprintf(stderr, "%s[%d]%s(): " fmt "\n", basename(__FILE__), __LINE__, __func__, ##__VA_ARGS__) -#define A(statement, fmt, ...) do { \ - if (statement) break; \ - fprintf(stderr, "%s[%d]%s(): assert [%s] failed: %d[%s]: " fmt "\n", \ - basename(__FILE__), __LINE__, __func__, \ - #statement, errno, strerror(errno), \ - ##__VA_ARGS__); \ - abort(); \ -} while (0) +#define A(statement, fmt, ...) \ + do { \ + if (statement) break; \ + fprintf(stderr, "%s[%d]%s(): assert [%s] failed: %d[%s]: " fmt "\n", basename(__FILE__), __LINE__, __func__, \ + #statement, errno, strerror(errno), ##__VA_ARGS__); \ + abort(); \ + } while (0) -#define E(fmt, ...) do { \ - fprintf(stderr, "%s[%d]%s(): %d[%s]: " fmt "\n", \ - basename(__FILE__), __LINE__, __func__, \ - errno, strerror(errno), \ - ##__VA_ARGS__); \ -} while (0) +#define E(fmt, ...) \ + do { \ + fprintf(stderr, "%s[%d]%s(): %d[%s]: " fmt "\n", basename(__FILE__), __LINE__, __func__, errno, strerror(errno), \ + ##__VA_ARGS__); \ + } while (0) #include "os.h" -typedef struct ep_s ep_t; +typedef struct ep_s ep_t; struct ep_s { - int ep; + int ep; - pthread_mutex_t lock; - int sv[2]; // 0 for read, 1 for write; - pthread_t thread; + pthread_mutex_t lock; + int sv[2]; // 0 for read, 1 for write; + pthread_t thread; - volatile unsigned int stopping:1; - volatile unsigned int waiting:1; - volatile unsigned int wakenup:1; + volatile unsigned int stopping : 1; + volatile unsigned int waiting : 1; + volatile unsigned int wakenup : 1; }; static int ep_dummy = 0; -static ep_t* ep_create(void); +static ep_t *ep_create(void); static void ep_destroy(ep_t *ep); -static void* routine(void* arg); -static int open_listen(unsigned short port); +static void *routine(void *arg); +static int open_listen(unsigned short port); -typedef struct fde_s fde_t; +typedef struct fde_s fde_t; struct fde_s { - int skt; + int skt; void (*on_event)(ep_t *ep, struct epoll_event *events, fde_t *client); }; static void listen_event(ep_t *ep, struct epoll_event *ev, fde_t *client); static void null_event(ep_t *ep, struct epoll_event *ev, fde_t *client); -#define usage(arg0, fmt, ...) do { \ - if (fmt[0]) { \ - fprintf(stderr, "" fmt "\n", ##__VA_ARGS__); \ - } \ - fprintf(stderr, "usage:\n"); \ - fprintf(stderr, " %s -l : specify listenning port\n", arg0); \ -} while (0) +#define usage(arg0, fmt, ...) \ + do { \ + if (fmt[0]) { \ + fprintf(stderr, "" fmt "\n", ##__VA_ARGS__); \ + } \ + fprintf(stderr, "usage:\n"); \ + fprintf(stderr, " %s -l : specify listenning port\n", arg0); \ + } while (0) int main(int argc, char *argv[]) { char *prg = basename(argv[0]); - if (argc==1) { + if (argc == 1) { usage(prg, ""); return 0; } - ep_t* ep = ep_create(); + ep_t *ep = ep_create(); A(ep, "failed"); - for (int i=1; i=argc) { + if (i >= argc) { usage(prg, "expecting after -l, but got nothing"); - return 1; // confirmed potential leakage + return 1; // confirmed potential leakage } arg = argv[i]; int port = atoi(arg); int skt = open_listen(port); - if (skt==-1) continue; - fde_t *client = (fde_t*)calloc(1, sizeof(*client)); + if (skt == -1) continue; + fde_t *client = (fde_t *)calloc(1, sizeof(*client)); if (!client) { E("out of memory"); close(skt); @@ -126,32 +124,32 @@ int main(int argc, char *argv[]) { client->skt = skt; client->on_event = listen_event; struct epoll_event ev = {0}; - ev.events = EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLRDHUP; + ev.events = EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLRDHUP; ev.data.ptr = client; - A(0==epoll_ctl(ep->ep, EPOLL_CTL_ADD, skt, &ev), ""); + A(0 == epoll_ctl(ep->ep, EPOLL_CTL_ADD, skt, &ev), ""); continue; } usage(prg, "unknown argument: [%s]", arg); return 1; } - char *line = NULL; - size_t linecap = 0; + char * line = NULL; + size_t linecap = 0; ssize_t linelen; while ((linelen = getline(&line, &linecap, stdin)) > 0) { - line[strlen(line)-1] = '\0'; - if (0==strcmp(line, "exit")) break; - if (0==strcmp(line, "quit")) break; - if (line==strstr(line, "close")) { + line[strlen(line) - 1] = '\0'; + if (0 == strcmp(line, "exit")) break; + if (0 == strcmp(line, "quit")) break; + if (line == strstr(line, "close")) { int fd = 0; sscanf(line, "close %d", &fd); - if (fd<=2) { + if (fd <= 2) { fprintf(stderr, "fd [%d] invalid\n", fd); continue; } - A(0==epoll_ctl(ep->ep, EPOLL_CTL_DEL, fd, NULL), ""); + A(0 == epoll_ctl(ep->ep, EPOLL_CTL_DEL, fd, NULL), ""); continue; } - if (strlen(line)==0) continue; + if (strlen(line) == 0) continue; fprintf(stderr, "unknown cmd:[%s]\n", line); } ep_destroy(ep); @@ -159,69 +157,69 @@ int main(int argc, char *argv[]) { return 0; } -ep_t* ep_create(void) { - ep_t *ep = (ep_t*)calloc(1, sizeof(*ep)); +ep_t *ep_create(void) { + ep_t *ep = (ep_t *)calloc(1, sizeof(*ep)); A(ep, "out of memory"); - A(-1!=(ep->ep = epoll_create(1)), ""); + A(-1 != (ep->ep = epoll_create(1)), ""); ep->sv[0] = -1; ep->sv[1] = -1; - A(0==socketpair(AF_LOCAL, SOCK_STREAM, 0, ep->sv), ""); - A(0==pthread_mutex_init(&ep->lock, NULL), ""); - A(0==pthread_mutex_lock(&ep->lock), ""); + A(0 == socketpair(AF_LOCAL, SOCK_STREAM, 0, ep->sv), ""); + A(0 == pthread_mutex_init(&ep->lock, NULL), ""); + A(0 == pthread_mutex_lock(&ep->lock), ""); struct epoll_event ev = {0}; - ev.events = EPOLLIN; + ev.events = EPOLLIN; ev.data.ptr = &ep_dummy; - A(0==epoll_ctl(ep->ep, EPOLL_CTL_ADD, ep->sv[0], &ev), ""); - A(0==pthread_create(&ep->thread, NULL, routine, ep), ""); - A(0==pthread_mutex_unlock(&ep->lock), ""); + A(0 == epoll_ctl(ep->ep, EPOLL_CTL_ADD, ep->sv[0], &ev), ""); + A(0 == pthread_create(&ep->thread, NULL, routine, ep), ""); + A(0 == pthread_mutex_unlock(&ep->lock), ""); return ep; } static void ep_destroy(ep_t *ep) { A(ep, "invalid argument"); ep->stopping = 1; - A(1==send(ep->sv[1], "1", 1, 0), ""); - A(0==pthread_join(ep->thread, NULL), ""); - A(0==pthread_mutex_destroy(&ep->lock), ""); - A(0==close(ep->sv[0]), ""); - A(0==close(ep->sv[1]), ""); - A(0==close(ep->ep), ""); + A(1 == send(ep->sv[1], "1", 1, 0), ""); + A(0 == pthread_join(ep->thread, NULL), ""); + A(0 == pthread_mutex_destroy(&ep->lock), ""); + A(0 == close(ep->sv[0]), ""); + A(0 == close(ep->sv[1]), ""); + A(0 == close(ep->ep), ""); free(ep); } -static void* routine(void* arg) { +static void *routine(void *arg) { A(arg, "invalid argument"); - ep_t *ep = (ep_t*)arg; + ep_t *ep = (ep_t *)arg; while (!ep->stopping) { struct epoll_event evs[10]; memset(evs, 0, sizeof(evs)); - A(0==pthread_mutex_lock(&ep->lock), ""); - A(ep->waiting==0, "internal logic error"); + A(0 == pthread_mutex_lock(&ep->lock), ""); + A(ep->waiting == 0, "internal logic error"); ep->waiting = 1; - A(0==pthread_mutex_unlock(&ep->lock), ""); + A(0 == pthread_mutex_unlock(&ep->lock), ""); - int r = epoll_wait(ep->ep, evs, sizeof(evs)/sizeof(evs[0]), -1); - A(r>0, "indefinite epoll_wait shall not timeout:[%d]", r); + int r = epoll_wait(ep->ep, evs, sizeof(evs) / sizeof(evs[0]), -1); + A(r > 0, "indefinite epoll_wait shall not timeout:[%d]", r); - A(0==pthread_mutex_lock(&ep->lock), ""); - A(ep->waiting==1, "internal logic error"); + A(0 == pthread_mutex_lock(&ep->lock), ""); + A(ep->waiting == 1, "internal logic error"); ep->waiting = 0; - A(0==pthread_mutex_unlock(&ep->lock), ""); + A(0 == pthread_mutex_unlock(&ep->lock), ""); - for (int i=0; idata.ptr == &ep_dummy) { char c = '\0'; - A(1==recv(ep->sv[0], &c, 1, 0), "internal logic error"); - A(0==pthread_mutex_lock(&ep->lock), ""); + A(1 == recv(ep->sv[0], &c, 1, 0), "internal logic error"); + A(0 == pthread_mutex_lock(&ep->lock), ""); ep->wakenup = 0; - A(0==pthread_mutex_unlock(&ep->lock), ""); + A(0 == pthread_mutex_unlock(&ep->lock), ""); continue; } A(ev->data.ptr, "internal logic error"); - fde_t *client = (fde_t*)ev->data.ptr; + fde_t *client = (fde_t *)ev->data.ptr; client->on_event(ep, ev, client); continue; } @@ -232,7 +230,7 @@ static void* routine(void* arg) { static int open_listen(unsigned short port) { int r = 0; int skt = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - if (skt==-1) { + if (skt == -1) { E("socket() failed"); return -1; } @@ -241,7 +239,7 @@ static int open_listen(unsigned short port) { si.sin_family = AF_INET; si.sin_addr.s_addr = inet_addr("0.0.0.0"); si.sin_port = htons(port); - r = bind(skt, (struct sockaddr*)&si, sizeof(si)); + r = bind(skt, (struct sockaddr *)&si, sizeof(si)); if (r) { E("bind(%u) failed", port); break; @@ -257,7 +255,7 @@ static int open_listen(unsigned short port) { if (r) { E("getsockname() failed"); } - A(len==sizeof(si), "internal logic error"); + A(len == sizeof(si), "internal logic error"); D("listenning at: %d", ntohs(si.sin_port)); return skt; } while (0); @@ -268,10 +266,10 @@ static int open_listen(unsigned short port) { static void listen_event(ep_t *ep, struct epoll_event *ev, fde_t *client) { A(ev->events & EPOLLIN, "internal logic error"); struct sockaddr_in si = {0}; - socklen_t silen = sizeof(si); - int skt = accept(client->skt, (struct sockaddr*)&si, &silen); - A(skt!=-1, "internal logic error"); - fde_t *server = (fde_t*)calloc(1, sizeof(*server)); + socklen_t silen = sizeof(si); + int skt = accept(client->skt, (struct sockaddr *)&si, &silen); + A(skt != -1, "internal logic error"); + fde_t *server = (fde_t *)calloc(1, sizeof(*server)); if (!server) { close(skt); return; @@ -279,26 +277,25 @@ static void listen_event(ep_t *ep, struct epoll_event *ev, fde_t *client) { server->skt = skt; server->on_event = null_event; struct epoll_event ee = {0}; - ee.events = EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLRDHUP; + ee.events = EPOLLIN | EPOLLERR | EPOLLHUP | EPOLLRDHUP; ee.data.ptr = server; - A(0==epoll_ctl(ep->ep, EPOLL_CTL_ADD, skt, &ee), ""); + A(0 == epoll_ctl(ep->ep, EPOLL_CTL_ADD, skt, &ee), ""); } static void null_event(ep_t *ep, struct epoll_event *ev, fde_t *client) { if (ev->events & EPOLLIN) { char buf[8192]; - int n = recv(client->skt, buf, sizeof(buf), 0); - A(n>=0 && n<=sizeof(buf), "internal logic error:[%d]", n); - A(n==fwrite(buf, 1, n, stdout), "internal logic error"); + int n = recv(client->skt, buf, sizeof(buf), 0); + A(n >= 0 && n <= sizeof(buf), "internal logic error:[%d]", n); + A(n == fwrite(buf, 1, n, stdout), "internal logic error"); } if (ev->events & (EPOLLERR | EPOLLHUP | EPOLLRDHUP)) { - A(0==pthread_mutex_lock(&ep->lock), ""); - A(0==epoll_ctl(ep->ep, EPOLL_CTL_DEL, client->skt, NULL), ""); - A(0==pthread_mutex_unlock(&ep->lock), ""); + A(0 == pthread_mutex_lock(&ep->lock), ""); + A(0 == epoll_ctl(ep->ep, EPOLL_CTL_DEL, client->skt, NULL), ""); + A(0 == pthread_mutex_unlock(&ep->lock), ""); close(client->skt); client->skt = -1; client->on_event = NULL; free(client); } } - diff --git a/tests/examples/c/makefile b/tests/examples/c/makefile index be6efbe32a3fd1ac326ac119667a67acd0dcce89..b06fe551dbde4b16e3ea197ca4c2bc1711ef63bb 100644 --- a/tests/examples/c/makefile +++ b/tests/examples/c/makefile @@ -17,14 +17,11 @@ exe: gcc $(CFLAGS) ./stream.c -o $(ROOT)stream $(LFLAGS) gcc $(CFLAGS) ./subscribe.c -o $(ROOT)subscribe $(LFLAGS) gcc $(CFLAGS) ./apitest.c -o $(ROOT)apitest $(LFLAGS) - gcc $(CFLAGS) ./stmt.c -o $(ROOT)stmt $(LFLAGS) clean: rm $(ROOT)asyncdemo rm $(ROOT)demo rm $(ROOT)prepare - rm $(ROOT)batchprepare rm $(ROOT)stream rm $(ROOT)subscribe rm $(ROOT)apitest - rm $(ROOT)stmt diff --git a/tests/examples/c/prepare.c b/tests/examples/c/prepare.c index 723b340a923c0bf326599e8090f8c6142a249053..1b1475eead51f87527c8d168df5f1daa12fe3c1f 100644 --- a/tests/examples/c/prepare.c +++ b/tests/examples/c/prepare.c @@ -1,4 +1,4 @@ -// TAOS standard API example. The same syntax as MySQL, but only a subet +// TAOS standard API example. The same syntax as MySQL, but only a subet // to compile: gcc -o prepare prepare.c -ltaos #include @@ -6,14 +6,12 @@ #include #include "taos.h" - void taosMsleep(int mseconds); -int main(int argc, char *argv[]) -{ - TAOS *taos; - TAOS_RES *result; - int code; +int main(int argc, char *argv[]) { + TAOS * taos; + TAOS_RES * result; + int code; TAOS_STMT *stmt; // connect to server @@ -26,9 +24,9 @@ int main(int argc, char *argv[]) if (taos == NULL) { printf("failed to connect to db, reason:%s\n", taos_errstr(taos)); exit(1); - } + } - result = taos_query(taos, "drop database demo"); + result = taos_query(taos, "drop database demo"); taos_free_result(result); result = taos_query(taos, "create database demo"); @@ -44,7 +42,9 @@ int main(int argc, char *argv[]) taos_free_result(result); // create table - const char* sql = "create table m1 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin binary(40), blob nchar(10))"; + const char *sql = + "create table m1 (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin " + "binary(40), blob nchar(10))"; result = taos_query(taos, sql); code = taos_errno(result); if (code != 0) { @@ -59,16 +59,16 @@ int main(int argc, char *argv[]) // insert 10 records struct { - int64_t ts; - int8_t b; - int8_t v1; - int16_t v2; - int32_t v4; - int64_t v8; - float f4; - double f8; - char bin[40]; - char blob[80]; + int64_t ts; + int8_t b; + int8_t v1; + int16_t v2; + int32_t v4; + int64_t v8; + float f4; + double f8; + char bin[40]; + char blob[80]; } v = {0}; stmt = taos_stmt_init(taos); @@ -138,7 +138,7 @@ int main(int argc, char *argv[]) sql = "insert into m1 values(?,?,?,?,?,?,?,?,?,?)"; code = taos_stmt_prepare(stmt, sql, 0); - if (code != 0){ + if (code != 0) { printf("failed to execute taos_stmt_prepare. code:0x%x\n", code); } v.ts = 1591060628000; @@ -203,4 +203,3 @@ int main(int argc, char *argv[]) return 0; } - diff --git a/tests/examples/c/schemaless.c b/tests/examples/c/schemaless.c index 3ea199c9144950526e4bbf59b9356753e2a88da6..89059501a9d6130d394e58c09466532c37f04cd1 100644 --- a/tests/examples/c/schemaless.c +++ b/tests/examples/c/schemaless.c @@ -1,6 +1,6 @@ +#include "os.h" #include "taos.h" #include "taoserror.h" -#include "os.h" #include #include @@ -12,15 +12,12 @@ int numSuperTables = 8; int numChildTables = 4; int numRowsPerChildTable = 2048; -void shuffle(char**lines, size_t n) -{ - if (n > 1) - { +void shuffle(char** lines, size_t n) { + if (n > 1) { size_t i; - for (i = 0; i < n - 1; i++) - { + for (i = 0; i < n - 1; i++) { size_t j = i + rand() / (RAND_MAX / (n - i) + 1); - char* t = lines[j]; + char* t = lines[j]; lines[j] = lines[i]; lines[i] = t; } @@ -34,7 +31,7 @@ static int64_t getTimeInUs() { } int main(int argc, char* argv[]) { - TAOS_RES *result; + TAOS_RES* result; const char* host = "127.0.0.1"; const char* user = "root"; const char* passwd = "taosdata"; @@ -59,12 +56,16 @@ int main(int argc, char* argv[]) { (void)taos_select_db(taos, "db"); - time_t ct = time(0); + time_t ct = time(0); int64_t ts = ct * 1000; - char* lineFormat = "sta%d,t0=true,t1=127i8,t2=32767i16,t3=%di32,t4=9223372036854775807i64,t9=11.12345f32,t10=22.123456789f64,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=255u8,c6=32770u16,c7=2147483699u32,c8=9223372036854775899u64,c9=11.12345f32,c10=22.123456789f64,c11=\"binaryValue\",c12=L\"ncharValue\" %lldms"; + char* lineFormat = + "sta%d,t0=true,t1=127i8,t2=32767i16,t3=%di32,t4=9223372036854775807i64,t9=11.12345f32,t10=22.123456789f64,t11=" + "\"binaryTagValue\",t12=L\"ncharTagValue\" " + "c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=255u8,c6=32770u16,c7=2147483699u32," + "c8=9223372036854775899u64,c9=11.12345f32,c10=22.123456789f64,c11=\"binaryValue\",c12=L\"ncharValue\" %lldms"; char** lines = calloc(numSuperTables * numChildTables * numRowsPerChildTable, sizeof(char*)); - int l = 0; + int l = 0; for (int i = 0; i < numSuperTables; ++i) { for (int j = 0; j < numChildTables; ++j) { for (int k = 0; k < numRowsPerChildTable; ++k) { @@ -78,121 +79,142 @@ int main(int argc, char* argv[]) { shuffle(lines, numSuperTables * numChildTables * numRowsPerChildTable); printf("%s\n", "begin taos_insert_lines"); - int64_t begin = getTimeInUs(); + int64_t begin = getTimeInUs(); int32_t code = taos_insert_lines(taos, lines, numSuperTables * numChildTables * numRowsPerChildTable); int64_t end = getTimeInUs(); - printf("code: %d, %s. time used: %"PRId64"\n", code, tstrerror(code), end-begin); + printf("code: %d, %s. time used: %" PRId64 "\n", code, tstrerror(code), end - begin); char* lines_000_0[] = { - "sta1,id=sta1_1,t0=true,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t5=255u8,t6=32770u16,t7=2147483699u32,t8=9223372036854775899u64,t9=11.12345f32,t10=22.123456789f64,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=255u8,c6=32770u16,c7=2147483699u32,c8=9223372036854775899u64,c9=11.12345f32,c10=22.123456789f64,c11=\"binaryValue\",c12=L\"ncharValue\" 1626006833639000us" - }; - - code = taos_insert_lines(taos, lines_000_0 , sizeof(lines_000_0)/sizeof(char*)); + "sta1,id=sta1_1,t0=true,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t5=255u8,t6=32770u16,t7=" + "2147483699u32,t8=9223372036854775899u64,t9=11.12345f32,t10=22.123456789f64,t11=\"binaryTagValue\",t12=" + "L\"ncharTagValue\" " + "c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=255u8,c6=32770u16,c7=2147483699u32," + "c8=9223372036854775899u64,c9=11.12345f32,c10=22.123456789f64,c11=\"binaryValue\",c12=L\"ncharValue\" " + "1626006833639000us"}; + + code = taos_insert_lines(taos, lines_000_0, sizeof(lines_000_0) / sizeof(char*)); if (0 == code) { printf("taos_insert_lines() lines_000_0 should return error\n"); return -1; } char* lines_000_1[] = { - "sta2,id=\"sta2_1\",t0=true,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t5=255u8,t6=32770u16,t7=2147483699u32,t8=9223372036854775899u64,t9=11.12345f32,t10=22.123456789f64,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=255u8,c6=32770u16,c7=2147483699u32,c8=9223372036854775899u64,c9=11.12345f32,c10=22.123456789f64,c11=\"binaryValue\",c12=L\"ncharValue\" 1626006833639001" - }; - - code = taos_insert_lines(taos, lines_000_1 , sizeof(lines_000_1)/sizeof(char*)); + "sta2,id=\"sta2_1\",t0=true,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t5=255u8,t6=32770u16," + "t7=2147483699u32,t8=9223372036854775899u64,t9=11.12345f32,t10=22.123456789f64,t11=\"binaryTagValue\",t12=" + "L\"ncharTagValue\" " + "c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=255u8,c6=32770u16,c7=2147483699u32," + "c8=9223372036854775899u64,c9=11.12345f32,c10=22.123456789f64,c11=\"binaryValue\",c12=L\"ncharValue\" " + "1626006833639001"}; + + code = taos_insert_lines(taos, lines_000_1, sizeof(lines_000_1) / sizeof(char*)); if (0 == code) { printf("taos_insert_lines() lines_000_1 should return error\n"); return -1; } char* lines_000_2[] = { - "sta3,id=\"sta3_1\",t0=true,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t9=11.12345f32,t10=22.123456789f64,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=255u8,c6=32770u16,c7=2147483699u32,c8=9223372036854775899u64,c9=11.12345f32,c10=22.123456789f64,c11=\"binaryValue\",c12=L\"ncharValue\" 0" - }; + "sta3,id=\"sta3_1\",t0=true,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t9=11.12345f32,t10=" + "22.123456789f64,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" " + "c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c5=255u8,c6=32770u16,c7=2147483699u32," + "c8=9223372036854775899u64,c9=11.12345f32,c10=22.123456789f64,c11=\"binaryValue\",c12=L\"ncharValue\" 0"}; - code = taos_insert_lines(taos, lines_000_2 , sizeof(lines_000_2)/sizeof(char*)); + code = taos_insert_lines(taos, lines_000_2, sizeof(lines_000_2) / sizeof(char*)); if (0 != code) { printf("taos_insert_lines() lines_000_2 return code:%d (%s)\n", code, (char*)tstrerror(code)); return -1; } char* lines_001_0[] = { - "sta4,t0=true,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t9=11.12345f32,t10=22.123456789f64,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c9=11.12345f32,c10=22.123456789f64,c11=\"binaryValue\",c12=L\"ncharValue\" 1626006833639000us", + "sta4,t0=true,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t9=11.12345f32,t10=22.123456789f64," + "t11=\"binaryTagValue\",t12=L\"ncharTagValue\" " + "c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c9=11.12345f32,c10=22.123456789f64,c11=" + "\"binaryValue\",c12=L\"ncharValue\" 1626006833639000us", }; - code = taos_insert_lines(taos, lines_001_0 , sizeof(lines_001_0)/sizeof(char*)); + code = taos_insert_lines(taos, lines_001_0, sizeof(lines_001_0) / sizeof(char*)); if (0 != code) { printf("taos_insert_lines() lines_001_0 return code:%d (%s)\n", code, (char*)tstrerror(code)); return -1; } char* lines_001_1[] = { - "sta5,id=\"sta5_1\",t0=true,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t9=11.12345f32,t10=22.123456789f64,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c9=11.12345f32,c10=22.123456789f64,c11=\"binaryValue\",c12=L\"ncharValue\" 1626006833639001" - }; + "sta5,id=\"sta5_1\",t0=true,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t9=11.12345f32,t10=" + "22.123456789f64,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" " + "c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c9=11.12345f32,c10=22.123456789f64,c11=" + "\"binaryValue\",c12=L\"ncharValue\" 1626006833639001"}; - code = taos_insert_lines(taos, lines_001_1 , sizeof(lines_001_1)/sizeof(char*)); + code = taos_insert_lines(taos, lines_001_1, sizeof(lines_001_1) / sizeof(char*)); if (0 != code) { printf("taos_insert_lines() lines_001_1 return code:%d (%s)\n", code, (char*)tstrerror(code)); return -1; } char* lines_001_2[] = { - "sta6,id=\"sta6_1\",t0=true,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t9=11.12345f32,t10=22.123456789f64,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c9=11.12345f32,c10=22.123456789f64,c11=\"binaryValue\",c12=L\"ncharValue\" 0" - }; + "sta6,id=\"sta6_1\",t0=true,t1=127i8,t2=32767i16,t3=2147483647i32,t4=9223372036854775807i64,t9=11.12345f32,t10=" + "22.123456789f64,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" " + "c0=true,c1=127i8,c2=32767i16,c3=2147483647i32,c4=9223372036854775807i64,c9=11.12345f32,c10=22.123456789f64,c11=" + "\"binaryValue\",c12=L\"ncharValue\" 0"}; - code = taos_insert_lines(taos, lines_001_2 , sizeof(lines_001_2)/sizeof(char*)); + code = taos_insert_lines(taos, lines_001_2, sizeof(lines_001_2) / sizeof(char*)); if (0 != code) { printf("taos_insert_lines() lines_001_2 return code:%d (%s)\n", code, (char*)tstrerror(code)); return -1; } char* lines_002[] = { - "stb,id=\"stb_1\",t20=t,t21=T,t22=true,t23=True,t24=TRUE,t25=f,t26=F,t27=false,t28=False,t29=FALSE,t10=33.12345,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" c20=t,c21=T,c22=true,c23=True,c24=TRUE,c25=f,c26=F,c27=false,c28=False,c29=FALSE,c10=33.12345,c11=\"binaryValue\",c12=L\"ncharValue\" 1626006833639000000ns", - "stc,id=\"stc_1\",t20=t,t21=T,t22=true,t23=True,t24=TRUE,t25=f,t26=F,t27=false,t28=False,t29=FALSE,t10=33.12345,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" c20=t,c21=T,c22=true,c23=True,c24=TRUE,c25=f,c26=F,c27=false,c28=False,c29=FALSE,c10=33.12345,c11=\"binaryValue\",c12=L\"ncharValue\" 1626006833639019us", - "stc,id=\"stc_1\",t20=t,t21=T,t22=true,t23=True,t24=TRUE,t25=f,t26=F,t27=false,t28=False,t29=FALSE,t10=33.12345,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" c20=t,c21=T,c22=true,c23=True,c24=TRUE,c25=f,c26=F,c27=false,c28=False,c29=FALSE,c10=33.12345,c11=\"binaryValue\",c12=L\"ncharValue\" 1626006833640ms", - "stc,id=\"stc_1\",t20=t,t21=T,t22=true,t23=True,t24=TRUE,t25=f,t26=F,t27=false,t28=False,t29=FALSE,t10=33.12345,t11=\"binaryTagValue\",t12=L\"ncharTagValue\" c20=t,c21=T,c22=true,c23=True,c24=TRUE,c25=f,c26=F,c27=false,c28=False,c29=FALSE,c10=33.12345,c11=\"binaryValue\",c12=L\"ncharValue\" 1626006834s" - }; - - code = taos_insert_lines(taos, lines_002 , sizeof(lines_002)/sizeof(char*)); + "stb,id=\"stb_1\",t20=t,t21=T,t22=true,t23=True,t24=TRUE,t25=f,t26=F,t27=false,t28=False,t29=FALSE,t10=33.12345," + "t11=\"binaryTagValue\",t12=L\"ncharTagValue\" " + "c20=t,c21=T,c22=true,c23=True,c24=TRUE,c25=f,c26=F,c27=false,c28=False,c29=FALSE,c10=33.12345,c11=" + "\"binaryValue\",c12=L\"ncharValue\" 1626006833639000000ns", + "stc,id=\"stc_1\",t20=t,t21=T,t22=true,t23=True,t24=TRUE,t25=f,t26=F,t27=false,t28=False,t29=FALSE,t10=33.12345," + "t11=\"binaryTagValue\",t12=L\"ncharTagValue\" " + "c20=t,c21=T,c22=true,c23=True,c24=TRUE,c25=f,c26=F,c27=false,c28=False,c29=FALSE,c10=33.12345,c11=" + "\"binaryValue\",c12=L\"ncharValue\" 1626006833639019us", + "stc,id=\"stc_1\",t20=t,t21=T,t22=true,t23=True,t24=TRUE,t25=f,t26=F,t27=false,t28=False,t29=FALSE,t10=33.12345," + "t11=\"binaryTagValue\",t12=L\"ncharTagValue\" " + "c20=t,c21=T,c22=true,c23=True,c24=TRUE,c25=f,c26=F,c27=false,c28=False,c29=FALSE,c10=33.12345,c11=" + "\"binaryValue\",c12=L\"ncharValue\" 1626006833640ms", + "stc,id=\"stc_1\",t20=t,t21=T,t22=true,t23=True,t24=TRUE,t25=f,t26=F,t27=false,t28=False,t29=FALSE,t10=33.12345," + "t11=\"binaryTagValue\",t12=L\"ncharTagValue\" " + "c20=t,c21=T,c22=true,c23=True,c24=TRUE,c25=f,c26=F,c27=false,c28=False,c29=FALSE,c10=33.12345,c11=" + "\"binaryValue\",c12=L\"ncharValue\" 1626006834s"}; + + code = taos_insert_lines(taos, lines_002, sizeof(lines_002) / sizeof(char*)); if (0 != code) { printf("taos_insert_lines() lines_002 return code:%d (%s)\n", code, (char*)tstrerror(code)); return -1; } - //Duplicate key check; - char* lines_003_1[] = { - "std,id=\"std_3_1\",t1=4i64,Id=\"std\",t2=true c1=true 1626006834s" - }; + // Duplicate key check; + char* lines_003_1[] = {"std,id=\"std_3_1\",t1=4i64,Id=\"std\",t2=true c1=true 1626006834s"}; - code = taos_insert_lines(taos, lines_003_1 , sizeof(lines_003_1)/sizeof(char*)); + code = taos_insert_lines(taos, lines_003_1, sizeof(lines_003_1) / sizeof(char*)); if (0 == code) { printf("taos_insert_lines() lines_003_1 return code:%d (%s)\n", code, (char*)tstrerror(code)); return -1; } - char* lines_003_2[] = { - "std,id=\"std_3_2\",tag1=4i64,Tag2=true,tAg3=2,TaG2=\"dup!\" c1=true 1626006834s" - }; + char* lines_003_2[] = {"std,id=\"std_3_2\",tag1=4i64,Tag2=true,tAg3=2,TaG2=\"dup!\" c1=true 1626006834s"}; - code = taos_insert_lines(taos, lines_003_2 , sizeof(lines_003_2)/sizeof(char*)); + code = taos_insert_lines(taos, lines_003_2, sizeof(lines_003_2) / sizeof(char*)); if (0 == code) { printf("taos_insert_lines() lines_003_2 return code:%d (%s)\n", code, (char*)tstrerror(code)); return -1; } - char* lines_003_3[] = { - "std,id=\"std_3_3\",tag1=4i64 field1=true,Field2=2,FIElD1=\"dup!\",fIeLd4=true 1626006834s" - }; + char* lines_003_3[] = {"std,id=\"std_3_3\",tag1=4i64 field1=true,Field2=2,FIElD1=\"dup!\",fIeLd4=true 1626006834s"}; - code = taos_insert_lines(taos, lines_003_3 , sizeof(lines_003_3)/sizeof(char*)); + code = taos_insert_lines(taos, lines_003_3, sizeof(lines_003_3) / sizeof(char*)); if (0 == code) { printf("taos_insert_lines() lines_003_3 return code:%d (%s)\n", code, (char*)tstrerror(code)); return -1; } char* lines_003_4[] = { - "std,id=\"std_3_4\",tag1=4i64,dupkey=4i16,tag2=T field1=true,dUpkEy=1e3f32,field2=\"1234\" 1626006834s" - }; + "std,id=\"std_3_4\",tag1=4i64,dupkey=4i16,tag2=T field1=true,dUpkEy=1e3f32,field2=\"1234\" 1626006834s"}; - code = taos_insert_lines(taos, lines_003_4 , sizeof(lines_003_4)/sizeof(char*)); + code = taos_insert_lines(taos, lines_003_4, sizeof(lines_003_4) / sizeof(char*)); if (0 == code) { printf("taos_insert_lines() lines_003_4 return code:%d (%s)\n", code, (char*)tstrerror(code)); return -1; diff --git a/tests/examples/c/stream.c b/tests/examples/c/stream.c index 30a790f061cd8ef2b870a371c2cadfb0e2a413c1..e26d6588a1382a1f7133f84bd623aa05304060a9 100644 --- a/tests/examples/c/stream.c +++ b/tests/examples/c/stream.c @@ -13,24 +13,23 @@ * along with this program. If not, see . */ +#include #include #include #include -#include -#include #include // include TDengine header file +#include typedef struct { - char server_ip[64]; - char db_name[64]; - char tbl_name[64]; + char server_ip[64]; + char db_name[64]; + char tbl_name[64]; } param; -int g_thread_exit_flag = 0; -void* insert_rows(void *sarg); +int g_thread_exit_flag = 0; +void *insert_rows(void *sarg); -void streamCallBack(void *param, TAOS_RES *res, TAOS_ROW row) -{ +void streamCallBack(void *param, TAOS_RES *res, TAOS_ROW row) { // in this simple demo, it just print out the result char temp[128]; @@ -42,85 +41,81 @@ void streamCallBack(void *param, TAOS_RES *res, TAOS_ROW row) printf("\n%s\n", temp); } -int main(int argc, char *argv[]) -{ - TAOS *taos; - char db_name[64]; - char tbl_name[64]; - char sql[1024] = { 0 }; +int main(int argc, char *argv[]) { + TAOS *taos; + char db_name[64]; + char tbl_name[64]; + char sql[1024] = {0}; if (argc != 4) { printf("usage: %s server-ip dbname tblname\n", argv[0]); exit(0); - } + } strcpy(db_name, argv[2]); strcpy(tbl_name, argv[3]); - + // create pthread to insert into row per second for stream calc param *t_param = (param *)malloc(sizeof(param)); - if (NULL == t_param) - { + if (NULL == t_param) { printf("failed to malloc\n"); exit(1); } - memset(t_param, 0, sizeof(param)); + memset(t_param, 0, sizeof(param)); strcpy(t_param->server_ip, argv[1]); strcpy(t_param->db_name, db_name); strcpy(t_param->tbl_name, tbl_name); pthread_t pid; - pthread_create(&pid, NULL, (void * (*)(void *))insert_rows, t_param); + pthread_create(&pid, NULL, (void *(*)(void *))insert_rows, t_param); - sleep(3); // waiting for database is created. + sleep(3); // waiting for database is created. // open connection to database taos = taos_connect(argv[1], "root", "taosdata", db_name, 0); if (taos == NULL) { printf("failed to connet to server:%s\n", argv[1]); - free(t_param); + free(t_param); exit(1); } - // starting stream calc, + // starting stream calc, printf("please input stream SQL:[e.g., select count(*) from tblname interval(5s) sliding(2s);]\n"); fgets(sql, sizeof(sql), stdin); if (sql[0] == 0) { - printf("input NULL stream SQL, so exit!\n"); + printf("input NULL stream SQL, so exit!\n"); free(t_param); exit(1); } - // param is set to NULL in this demo, it shall be set to the pointer to app context + // param is set to NULL in this demo, it shall be set to the pointer to app context TAOS_STREAM *pStream = taos_open_stream(taos, sql, streamCallBack, 0, NULL, NULL); if (NULL == pStream) { - printf("failed to create stream\n"); + printf("failed to create stream\n"); free(t_param); exit(1); } - + printf("presss any key to exit\n"); getchar(); taos_close_stream(pStream); - - g_thread_exit_flag = 1; + + g_thread_exit_flag = 1; pthread_join(pid, NULL); taos_close(taos); - free(t_param); + free(t_param); return 0; } +void *insert_rows(void *sarg) { + TAOS * taos; + char command[1024] = {0}; + param *winfo = (param *)sarg; -void* insert_rows(void *sarg) -{ - TAOS *taos; - char command[1024] = { 0 }; - param *winfo = (param * )sarg; - - if (NULL == winfo){ - printf("para is null!\n"); + if (NULL == winfo) { + printf("para is null!\n"); exit(1); } @@ -129,7 +124,7 @@ void* insert_rows(void *sarg) printf("failed to connet to server:%s\n", winfo->server_ip); exit(1); } - + // drop database sprintf(command, "drop database %s;", winfo->db_name); if (taos_query(taos, command) != 0) { @@ -160,19 +155,18 @@ void* insert_rows(void *sarg) // insert data int64_t begin = (int64_t)time(NULL); - int index = 0; + int index = 0; while (1) { if (g_thread_exit_flag) break; - + index++; sprintf(command, "insert into %s values (%ld, %d)", winfo->tbl_name, (begin + index) * 1000, index); if (taos_query(taos, command)) { printf("failed to insert row [%s], reason:%s\n", command, taos_errstr(taos)); } sleep(1); - } + } taos_close(taos); return 0; } - diff --git a/tests/examples/c/subscribe.c b/tests/examples/c/subscribe.c index ad12f0e7a55b0f471f249f92f30cf659c94586a5..d8b76c008f24a4ff1e7827e5b1cb167f013c81c5 100644 --- a/tests/examples/c/subscribe.c +++ b/tests/examples/c/subscribe.c @@ -14,10 +14,10 @@ void print_result(TAOS_RES* res, int blockFetch) { int num_fields = taos_num_fields(res); TAOS_FIELD* fields = taos_fetch_fields(res); int nRows = 0; - + if (blockFetch) { nRows = taos_fetch_block(res, &row); - //for (int i = 0; i < nRows; i++) { + // for (int i = 0; i < nRows; i++) { // taos_print_row(buf, row + i, fields, num_fields); // puts(buf); //} @@ -34,15 +34,11 @@ void print_result(TAOS_RES* res, int blockFetch) { printf("%d rows consumed.\n", nRows); } - -void subscribe_callback(TAOS_SUB* tsub, TAOS_RES *res, void* param, int code) { - print_result(res, *(int*)param); -} - +void subscribe_callback(TAOS_SUB* tsub, TAOS_RES* res, void* param, int code) { print_result(res, *(int*)param); } void check_row_count(int line, TAOS_RES* res, int expected) { - int actual = 0; - TAOS_ROW row; + int actual = 0; + TAOS_ROW row; while ((row = taos_fetch_row(res))) { actual++; } @@ -53,16 +49,14 @@ void check_row_count(int line, TAOS_RES* res, int expected) { } } - void do_query(TAOS* taos, const char* sql) { TAOS_RES* res = taos_query(taos, sql); taos_free_result(res); } - void run_test(TAOS* taos) { do_query(taos, "drop database if exists test;"); - + usleep(100000); do_query(taos, "create database test;"); usleep(100000); @@ -161,14 +155,13 @@ void run_test(TAOS* taos) { taos_unsubscribe(tsub, 0); } - -int main(int argc, char *argv[]) { +int main(int argc, char* argv[]) { const char* host = "127.0.0.1"; const char* user = "root"; const char* passwd = "taosdata"; const char* sql = "select * from meters;"; const char* topic = "test-multiple"; - int async = 1, restart = 0, keep = 1, test = 0, blockFetch = 0; + int async = 1, restart = 0, keep = 1, test = 0, blockFetch = 0; for (int i = 1; i < argc; i++) { if (strncmp(argv[i], "-h=", 3) == 0) { @@ -240,20 +233,21 @@ int main(int argc, char *argv[]) { if (tsub == NULL) { printf("failed to create subscription.\n"); exit(0); - } + } if (async) { getchar(); - } else while(1) { - TAOS_RES* res = taos_consume(tsub); - if (res == NULL) { - printf("failed to consume data."); - break; - } else { - print_result(res, blockFetch); - getchar(); + } else + while (1) { + TAOS_RES* res = taos_consume(tsub); + if (res == NULL) { + printf("failed to consume data."); + break; + } else { + print_result(res, blockFetch); + getchar(); + } } - } printf("total rows consumed: %d\n", nTotalRows); taos_unsubscribe(tsub, keep); diff --git a/tests/nettest/FQDNnettest.sh b/tests/nettest/FQDNnettest.sh new file mode 100755 index 0000000000000000000000000000000000000000..f4ee5d56bce9842537ea9cea224c22dee28e2a7e --- /dev/null +++ b/tests/nettest/FQDNnettest.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +taos -n fqdn diff --git a/tests/nettest/TCPUDP.sh b/tests/nettest/TCPUDP.sh new file mode 100755 index 0000000000000000000000000000000000000000..3a4b5d77a4f26862b03194488380c8dad172bb42 --- /dev/null +++ b/tests/nettest/TCPUDP.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +for N in -1 0 1 10000 10001 +do + for l in 1023 1024 1073741824 1073741825 + do + for S in udp tcp + do + taos -n speed -h BCC-2 -P 6030 -N $N -l $l -S $S 2>&1 | tee -a result.txt + done + done +done diff --git a/tests/pytest/functions/queryTestCases.py b/tests/pytest/functions/queryTestCases.py index 8089bcccdc9866a7f7eab19a9d151e1e57df78a6..081669bbb9bc866da1ccd297ba1e762c698de341 100644 --- a/tests/pytest/functions/queryTestCases.py +++ b/tests/pytest/functions/queryTestCases.py @@ -975,6 +975,84 @@ class TDTestCase: tdSql.error("select ts as t, bottom(t1, 3) from t1 order by c3") + tdSql.error("select ts as t, top(c1, 0) from t1") + tdSql.query("select ts as t, top(c1, 5) from t1") + tdSql.checkRows(5) + tdSql.checkCols(3) + for i in range(5): + data=tdSql.getData(i, 0) + tdSql.checkData(i, 1, data) + tdSql.query("select ts as t, top(c1, 5) from stb1") + tdSql.checkRows(5) + tdSql.query("select ts as t, top(c1, 5) from stb1 group by tbname") + tdSql.checkRows(500) + tdSql.query("select ts as t, top(c1, 8) from t1") + tdSql.checkRows(6) + tdSql.query("select ts as t, top(c2, 8) from t1") + tdSql.checkRows(6) + tdSql.error("select ts as t, top(c3, 5) from t1") + tdSql.error("select ts as t, top(c4, 5) from t1") + tdSql.query("select ts as t, top(c5, 8) from t1") + tdSql.checkRows(6) + tdSql.error("select ts as t, top(c6, 5) from t1") + tdSql.error("select ts as t, top(c5, 8) as b from t1 order by b") + tdSql.error("select ts as t, top(t1, 1) from t1") + tdSql.error("select ts as t, top(t1, 1) from stb1") + tdSql.error("select ts as t, top(t1, 3) from stb1 order by c3") + tdSql.error("select ts as t, top(t1, 3) from t1 order by c3") + + tdDnodes.stop(1) + tdDnodes.start(1) + + tdSql.query("select ts as t, diff(c1) from t1") + tdSql.checkRows(5) + tdSql.checkCols(3) + for i in range(5): + data=tdSql.getData(i, 0) + tdSql.checkData(i, 1, data) + tdSql.query("select ts as t, diff(c1) from stb1 group by tbname") + tdSql.checkRows(500) + tdSql.checkCols(4) + tdSql.query("select ts as t, diff(c1) from t1") + tdSql.query("select ts as t, diff(c1) from t1") + tdSql.error("select ts as t, diff(c1) from stb1") + tdSql.query("select ts as t, diff(c2) from t1") + tdSql.checkRows(5) + tdSql.error("select ts as t, diff(c3) from t1") + tdSql.error("select ts as t, diff(c4) from t1") + tdSql.query("select ts as t, diff(c5) from t1") + tdSql.checkRows(5) + tdSql.error("select ts as t, diff(c6) from t1") + tdSql.error("select ts as t, diff(t1) from t1") + tdSql.error("select ts as t, diff(c1, c2) from t1") + + tdSql.error("select ts as t, bottom(c1, 0) from t1") + tdSql.query("select ts as t, bottom(c1, 5) from t1") + tdSql.checkRows(5) + tdSql.checkCols(3) + for i in range(5): + data=tdSql.getData(i, 0) + tdSql.checkData(i, 1, data) + tdSql.query("select ts as t, bottom(c1, 5) from stb1") + tdSql.checkRows(5) + tdSql.query("select ts as t, bottom(c1, 5) from stb1 group by tbname") + tdSql.checkRows(500) + tdSql.query("select ts as t, bottom(c1, 8) from t1") + tdSql.checkRows(6) + tdSql.query("select ts as t, bottom(c2, 8) from t1") + tdSql.checkRows(6) + tdSql.error("select ts as t, bottom(c3, 5) from t1") + tdSql.error("select ts as t, bottom(c4, 5) from t1") + tdSql.query("select ts as t, bottom(c5, 8) from t1") + tdSql.checkRows(6) + tdSql.error("select ts as t, bottom(c6, 5) from t1") + tdSql.error("select ts as t, bottom(c5, 8) as b from t1 order by b") + tdSql.error("select ts as t, bottom(t1, 1) from t1") + tdSql.error("select ts as t, bottom(t1, 1) from stb1") + tdSql.error("select ts as t, bottom(t1, 3) from stb1 order by c3") + tdSql.error("select ts as t, bottom(t1, 3) from t1 order by c3") + + tdSql.error("select ts as t, top(c1, 0) from t1") tdSql.query("select ts as t, top(c1, 5) from t1") tdSql.checkRows(5) diff --git a/tests/pytest/query/queryPerformance.py b/tests/pytest/query/queryPerformance.py index 81103252d8794032ee17b143f814276e171863f2..29e5cb19b75b0943c24382d268e81daebed01cdf 100644 --- a/tests/pytest/query/queryPerformance.py +++ b/tests/pytest/query/queryPerformance.py @@ -17,6 +17,7 @@ import os import taos import time import argparse +import json class taosdemoQueryPerformace: @@ -48,7 +49,7 @@ class taosdemoQueryPerformace: cursor2 = self.conn2.cursor() cursor2.execute("create database if not exists %s" % self.dbName) cursor2.execute("use %s" % self.dbName) - cursor2.execute("create table if not exists %s(ts timestamp, query_time float, commit_id binary(50), branch binary(50), type binary(20)) tags(query_id int, query_sql binary(300))" % self.stbName) + cursor2.execute("create table if not exists %s(ts timestamp, query_time_avg float, query_time_max float, query_time_min float, commit_id binary(50), branch binary(50), type binary(20)) tags(query_id int, query_sql binary(300))" % self.stbName) sql = "select count(*) from test.meters" tableid = 1 @@ -74,7 +75,7 @@ class taosdemoQueryPerformace: tableid = 6 cursor2.execute("create table if not exists %s%d using %s tags(%d, '%s')" % (self.tbPerfix, tableid, self.stbName, tableid, sql)) - sql = "select * from meters" + sql = "select * from meters limit 10000" tableid = 7 cursor2.execute("create table if not exists %s%d using %s tags(%d, '%s')" % (self.tbPerfix, tableid, self.stbName, tableid, sql)) @@ -87,37 +88,96 @@ class taosdemoQueryPerformace: cursor2.execute("create table if not exists %s%d using %s tags(%d, '%s')" % (self.tbPerfix, tableid, self.stbName, tableid, sql)) cursor2.close() + + def generateQueryJson(self): + + sqls = [] + cursor2 = self.conn2.cursor() + cursor2.execute("select query_id, query_sql from %s.%s" % (self.dbName, self.stbName)) + i = 0 + for data in cursor2: + sql = { + "sql": data[1], + "result_mode": "onlyformat", + "result_file": "./query_sql_res%d.txt" % i + } + sqls.append(sql) + i += 1 + + query_data = { + "filetype": "query", + "cfgdir": "/etc/perf", + "host": "127.0.0.1", + "port": 6030, + "user": "root", + "password": "taosdata", + "databases": "test", + "specified_table_query": { + "query_times": 100, + "concurrent": 1, + "sqls": sqls + } + } + + query_json_file = f"/tmp/query.json" + + with open(query_json_file, 'w') as f: + json.dump(query_data, f) + return query_json_file + + def getBuildPath(self): + selfPath = os.path.dirname(os.path.realpath(__file__)) + + if ("community" in selfPath): + projPath = selfPath[:selfPath.find("community")] + else: + projPath = selfPath[:selfPath.find("tests")] + + for root, dirs, files in os.walk(projPath): + if ("taosdemo" in files): + rootRealPath = os.path.dirname(os.path.realpath(root)) + if ("packaging" not in rootRealPath): + buildPath = root[:len(root) - len("/build/bin")] + break + return buildPath + + def getCMDOutput(self, cmd): + cmd = os.popen(cmd) + output = cmd.read() + cmd.close() + return output def query(self): - cursor = self.conn.cursor() - print("==================== query performance ====================") + buildPath = self.getBuildPath() + if (buildPath == ""): + print("taosdemo not found!") + sys.exit(1) + + binPath = buildPath + "/build/bin/" + os.system( + "%sperfMonitor -f %s > query_res.txt" % + (binPath, self.generateQueryJson())) + cursor = self.conn2.cursor() + print("==================== query performance ====================") cursor.execute("use %s" % self.dbName) - cursor.execute("select tbname, query_id, query_sql from %s" % self.stbName) + cursor.execute("select tbname, query_sql from %s" % self.stbName) + i = 0 for data in cursor: table_name = data[0] - query_id = data[1] - sql = data[2] + sql = data[1] + + self.avgDelay = self.getCMDOutput("grep 'avgDelay' query_res.txt | awk 'NR==%d{print $2}'" % (i + 1)) + self.maxDelay = self.getCMDOutput("grep 'avgDelay' query_res.txt | awk 'NR==%d{print $5}'" % (i + 1)) + self.minDelay = self.getCMDOutput("grep 'avgDelay' query_res.txt | awk 'NR==%d{print $8}'" % (i + 1)) + i += 1 + + print("query time for: %s %f seconds" % (sql, float(self.avgDelay))) + c = self.conn2.cursor() + c.execute("insert into %s.%s values(now, %f, %f, %f, '%s', '%s', '%s')" % (self.dbName, table_name, float(self.avgDelay), float(self.maxDelay), float(self.minDelay), self.commitID, self.branch, self.type)) - totalTime = 0 - cursor2 = self.conn.cursor() - cursor2.execute("use test") - for i in range(100): - if(self.clearCache == True): - # root permission is required - os.system("echo 3 > /proc/sys/vm/drop_caches") - - startTime = time.time() - cursor2.execute(sql) - totalTime += time.time() - startTime - cursor2.close() - print("query time for: %s %f seconds" % (sql, totalTime / 100)) - - cursor3 = self.conn2.cursor() - cursor3.execute("insert into %s.%s values(now, %f, '%s', '%s', '%s')" % (self.dbName, table_name, totalTime / 100, self.commitID, self.branch, self.type)) - - cursor3.close() + c.close() cursor.close() if __name__ == '__main__': @@ -174,4 +234,4 @@ if __name__ == '__main__': args = parser.parse_args() perftest = taosdemoQueryPerformace(args.remove_cache, args.commit_id, args.database_name, args.stable_name, args.table_perfix, args.git_branch, args.build_type) perftest.createPerfTables() - perftest.query() + perftest.query() \ No newline at end of file diff --git a/tests/pytest/tools/taosdemoPerformance.py b/tests/pytest/tools/taosdemoPerformance.py index 1d28a2708fa1911e22aca97afa246a124dd2d6fc..51b064a08e5cd55401f9cf803a8683653f722679 100644 --- a/tests/pytest/tools/taosdemoPerformance.py +++ b/tests/pytest/tools/taosdemoPerformance.py @@ -49,24 +49,18 @@ class taosdemoPerformace: def generateJson(self): db = { "name": "%s" % self.insertDB, - "drop": "yes", - "replica": 1 + "drop": "yes" } stb = { "name": "meters", - "child_table_exists": "no", "childtable_count": self.numOfTables, "childtable_prefix": "stb_", - "auto_create_table": "no", - "data_source": "rand", "batch_create_tbl_num": 10, - "insert_mode": "taosc", + "insert_mode": "rand", "insert_rows": self.numOfRows, - "interlace_rows": 0, - "max_sql_len": 1024000, - "disorder_ratio": 0, - "disorder_range": 1000, + "batch_rows": 1000000, + "max_sql_len": 1048576, "timestamp_step": 1, "start_timestamp": "2020-10-01 00:00:00.000", "sample_format": "csv", @@ -100,11 +94,8 @@ class taosdemoPerformace: "user": "root", "password": "taosdata", "thread_count": 10, - "thread_count_create_tbl": 10, + "thread_count_create_tbl": 4, "result_file": "./insert_res.txt", - "confirm_parameter_prompt": "no", - "insert_interval": 0, - "num_of_records_per_req": 30000, "databases": [db] } @@ -145,7 +136,7 @@ class taosdemoPerformace: binPath = buildPath + "/build/bin/" os.system( - "%staosdemo -f %s > /dev/null 2>&1" % + "%sperfMonitor -f %s > /dev/null 2>&1" % (binPath, self.generateJson())) self.createTableTime = self.getCMDOutput( "grep 'Spent' insert_res.txt | awk 'NR==1{print $2}'") diff --git a/tests/script/api/makefile b/tests/script/api/makefile index 97392f2e089275b423d9b616d0a029bf3ecf4b20..a3e3b5b95eb001d5dc805842f970332bc0deb3ac 100644 --- a/tests/script/api/makefile +++ b/tests/script/api/makefile @@ -15,8 +15,10 @@ exe: gcc $(CFLAGS) ./batchprepare.c -o $(ROOT)batchprepare $(LFLAGS) gcc $(CFLAGS) ./stmtBatchTest.c -o $(ROOT)stmtBatchTest $(LFLAGS) gcc $(CFLAGS) ./stmtTest.c -o $(ROOT)stmtTest $(LFLAGS) + gcc $(CFLAGS) ./stmt.c -o $(ROOT)stmt $(LFLAGS) clean: rm $(ROOT)batchprepare rm $(ROOT)stmtBatchTest rm $(ROOT)stmtTest + rm $(ROOT)stmt