create_table.c 11.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#define _DEFAULT_SOURCE
#include "os.h"
S
Shengliang Guan 已提交
18
#include "taos.h"
19
#include "taoserror.h"
S
Shengliang Guan 已提交
20
#include "ulog.h"
21 22 23 24 25

#define GREEN "\033[1;32m"
#define NC "\033[0m"

char    dbName[32] = "db";
S
Shengliang Guan 已提交
26
char    stbName[64] = "st";
S
Shengliang Guan 已提交
27
int32_t numOfThreads = 1;
S
Shengliang Guan 已提交
28
int64_t numOfTables = 200000;
S
Shengliang Guan 已提交
29 30
int32_t createTable = 1;
int32_t insertData = 0;
S
Shengliang Guan 已提交
31
int32_t batchNum = 100;
S
Shengliang Guan 已提交
32
int32_t numOfVgroups = 2;
33 34

typedef struct {
S
Shengliang Guan 已提交
35 36
  int64_t   tableBeginIndex;
  int64_t   tableEndIndex;
37 38
  int32_t   threadIndex;
  char      dbName[32];
S
Shengliang Guan 已提交
39
  char      stbName[64];
40
  float     createTableSpeed;
S
Shengliang Guan 已提交
41
  float     insertDataSpeed;
S
Shengliang Guan 已提交
42
  int64_t   startMs;
L
lihui 已提交
43 44
  int64_t   maxDelay;
  int64_t   minDelay;
45 46 47
  pthread_t thread;
} SThreadInfo;

S
Shengliang Guan 已提交
48
void  parseArgument(int32_t argc, char *argv[]);
49
void *threadFunc(void *param);
S
Shengliang Guan 已提交
50
void  createDbAndStb();
51

S
Shengliang Guan 已提交
52
int32_t main(int32_t argc, char *argv[]) {
S
Shengliang Guan 已提交
53 54 55 56
  parseArgument(argc, argv);
  createDbAndStb();

  pPrint("%d threads are spawned to create %d tables", numOfThreads, numOfThreads);
57 58 59 60 61 62

  pthread_attr_t thattr;
  pthread_attr_init(&thattr);
  pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE);
  SThreadInfo *pInfo = (SThreadInfo *)calloc(numOfThreads, sizeof(SThreadInfo));

L
lihui 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  //int64_t numOfTablesPerThread = numOfTables / numOfThreads;
  //numOfTables = numOfTablesPerThread * numOfThreads;


  if (numOfThreads < 1) {
    numOfThreads = 1;
  }
  
  int64_t a = numOfTables / numOfThreads;
  if (a < 1) {
  	  numOfThreads = numOfTables;
  	  a = 1;
  }
  
  int64_t b = 0;
  b = numOfTables % numOfThreads;

  int64_t tableFrom = 0;
S
Shengliang Guan 已提交
81
  for (int32_t i = 0; i < numOfThreads; ++i) {
L
lihui 已提交
82 83 84
    pInfo[i].tableBeginIndex = tableFrom;
    pInfo[i].tableEndIndex = i < b ? tableFrom + a : tableFrom + a - 1;
    tableFrom = pInfo[i].tableEndIndex + 1;
85
    pInfo[i].threadIndex = i;
L
lihui 已提交
86
	pInfo[i].minDelay = INT64_MAX;
87
    strcpy(pInfo[i].dbName, dbName);
S
Shengliang Guan 已提交
88
    strcpy(pInfo[i].stbName, stbName);
89 90 91 92
    pthread_create(&(pInfo[i].thread), &thattr, threadFunc, (void *)(pInfo + i));
  }

  taosMsleep(300);
S
Shengliang Guan 已提交
93
  for (int32_t i = 0; i < numOfThreads; i++) {
94 95 96
    pthread_join(pInfo[i].thread, NULL);
  }

L
lihui 已提交
97 98 99
  int64_t maxDelay = 0;
  int64_t minDelay = INT64_MAX;

100
  float createTableSpeed = 0;
S
Shengliang Guan 已提交
101
  for (int32_t i = 0; i < numOfThreads; ++i) {
102
    createTableSpeed += pInfo[i].createTableSpeed;
L
lihui 已提交
103 104 105

    if (pInfo[i].maxDelay > maxDelay) maxDelay = pInfo[i].maxDelay;
    if (pInfo[i].minDelay < minDelay) minDelay = pInfo[i].minDelay;  
106 107
  }

S
Shengliang Guan 已提交
108
  float insertDataSpeed = 0;
S
Shengliang Guan 已提交
109
  for (int32_t i = 0; i < numOfThreads; ++i) {
S
Shengliang Guan 已提交
110 111 112
    insertDataSpeed += pInfo[i].insertDataSpeed;
  }

L
lihui 已提交
113 114 115 116 117 118 119 120 121 122 123
  pPrint("%s total %" PRId64 " tables, %.1f tables/second, threads:%d, maxDelay: %" PRId64 "us, minDelay: %" PRId64 "us %s", 
           GREEN, 
           numOfTables, 
           createTableSpeed, 
           numOfThreads, 
           maxDelay, 
           minDelay,
           NC);

  if (insertData) {
      pPrint("%s total %" PRId64 " tables, %.1f rows/second, threads:%d %s", GREEN, numOfTables, insertDataSpeed,
S
Shengliang Guan 已提交
124
         numOfThreads, NC);
L
lihui 已提交
125
  }
126 127 128 129 130

  pthread_attr_destroy(&thattr);
  free(pInfo);
}

S
Shengliang Guan 已提交
131
void createDbAndStb() {
132 133
  pPrint("start to create db and stable");
  char qstr[64000];
S
Shengliang Guan 已提交
134

135 136
  TAOS *con = taos_connect(NULL, "root", "taosdata", NULL, 0);
  if (con == NULL) {
L
lihui 已提交
137
    pError("failed to connect to DB, reason:%s", taos_errstr(NULL));
138 139 140
    exit(1);
  }

S
Shengliang Guan 已提交
141
  sprintf(qstr, "create database if not exists %s vgroups %d", dbName, numOfVgroups);
L
lihui 已提交
142 143
  TAOS_RES *pRes = taos_query(con, qstr);
  int32_t   code = taos_errno(pRes);
144
  if (code != 0) {
L
lihui 已提交
145
    pError("failed to create database:%s, sql:%s, code:%d reason:%s", dbName, qstr, taos_errno(pRes), taos_errstr(pRes));
146 147
    exit(0);
  }
L
lihui 已提交
148
  taos_free_result(pRes);
149 150

  sprintf(qstr, "use %s", dbName);
L
lihui 已提交
151 152
  pRes = taos_query(con, qstr);
  code = taos_errno(pRes);
153
  if (code != 0) {
L
lihui 已提交
154
    pError("failed to use db, code:%d reason:%s", taos_errno(pRes), taos_errstr(pRes));
155 156
    exit(0);
  }
L
lihui 已提交
157
  taos_free_result(pRes);
158

S
Shengliang Guan 已提交
159
  sprintf(qstr, "create table %s (ts timestamp, i int) tags (j int)", stbName);
L
lihui 已提交
160 161
  pRes = taos_query(con, qstr);
  code = taos_errno(pRes);
S
Shengliang Guan 已提交
162
  if (code != 0) {
L
lihui 已提交
163
    pError("failed to use db, code:%d reason:%s", taos_errno(pRes), taos_errstr(pRes));
S
Shengliang Guan 已提交
164 165
    exit(0);
  }
L
lihui 已提交
166
  taos_free_result(pRes);
S
Shengliang Guan 已提交
167

168 169 170
  taos_close(con);
}

S
Shengliang Guan 已提交
171
void printCreateProgress(SThreadInfo *pInfo, int64_t t) {
S
Shengliang Guan 已提交
172
  int64_t endMs = taosGetTimestampMs();
S
Shengliang Guan 已提交
173
  int64_t totalTables = t - pInfo->tableBeginIndex;
S
Shengliang Guan 已提交
174 175 176
  float   seconds = (endMs - pInfo->startMs) / 1000.0;
  float   speed = totalTables / seconds;
  pInfo->createTableSpeed = speed;
S
Shengliang Guan 已提交
177 178
  pPrint("thread:%d, %" PRId64 " tables created, time:%.2f sec, speed:%.1f tables/second, ", pInfo->threadIndex,
         totalTables, seconds, speed);
S
Shengliang Guan 已提交
179 180
}

S
Shengliang Guan 已提交
181
void printInsertProgress(SThreadInfo *pInfo, int64_t t) {
S
Shengliang Guan 已提交
182
  int64_t endMs = taosGetTimestampMs();
S
Shengliang Guan 已提交
183
  int64_t totalTables = t - pInfo->tableBeginIndex;
S
Shengliang Guan 已提交
184 185 186
  float   seconds = (endMs - pInfo->startMs) / 1000.0;
  float   speed = totalTables / seconds;
  pInfo->insertDataSpeed = speed;
S
Shengliang Guan 已提交
187 188
  pPrint("thread:%d, %" PRId64 " rows inserted, time:%.2f sec, speed:%.1f rows/second, ", pInfo->threadIndex,
         totalTables, seconds, speed);
S
Shengliang Guan 已提交
189 190
}

191 192
void *threadFunc(void *param) {
  SThreadInfo *pInfo = (SThreadInfo *)param;
S
Shengliang Guan 已提交
193 194
  char        *qstr = malloc(2000 * 1000);
  int32_t      code = 0;
195 196 197

  TAOS *con = taos_connect(NULL, "root", "taosdata", NULL, 0);
  if (con == NULL) {
L
lihui 已提交
198
    pError("index:%d, failed to connect to DB, reason:%s", pInfo->threadIndex, taos_errstr(NULL));
199 200 201
    exit(1);
  }

L
lihui 已提交
202
  //printf("thread:%d, table range: %"PRId64 " - %"PRId64 "\n", pInfo->threadIndex, pInfo->tableBeginIndex, pInfo->tableEndIndex);
203
  sprintf(qstr, "use %s", pInfo->dbName);
L
lihui 已提交
204 205 206
  TAOS_RES *pRes = taos_query(con, qstr);
  taos_free_result(pRes);

207

S
Shengliang Guan 已提交
208
  if (createTable) {
L
lihui 已提交
209 210 211
    int64_t curMs = 0;
    int64_t beginMs = taosGetTimestampMs();
    pInfo->startMs = beginMs;
S
Shengliang Guan 已提交
212 213
    for (int64_t t = pInfo->tableBeginIndex; t < pInfo->tableEndIndex; ++t) {
      int64_t batch = (pInfo->tableEndIndex - t);
S
Shengliang Guan 已提交
214 215 216 217
      batch = MIN(batch, batchNum);

      int32_t len = sprintf(qstr, "create table");
      for (int32_t i = 0; i < batch; ++i) {
S
Shengliang Guan 已提交
218
        len += sprintf(qstr + len, " t%" PRId64 " using %s tags(%" PRId64 ")", t + i, stbName, t + i);
S
Shengliang Guan 已提交
219
      }
S
Shengliang Guan 已提交
220

L
lihui 已提交
221 222 223
      int64_t startTs = taosGetTimestampUs();
      TAOS_RES *pRes = taos_query(con, qstr);
      code = taos_errno(pRes);
S
TD-1415  
Shengliang Guan 已提交
224
      if (code != 0) {
S
Shengliang Guan 已提交
225
        pError("failed to create table t%" PRId64 ", reason:%s", t, tstrerror(code));
S
TD-1415  
Shengliang Guan 已提交
226
      }
L
lihui 已提交
227 228 229 230 231 232 233 234 235 236
      taos_free_result(pRes);
	  int64_t endTs = taosGetTimestampUs();
	  int64_t delay = endTs - startTs;
	  //printf("==== %"PRId64" -  %"PRId64", %"PRId64"\n", startTs, endTs, delay);
	  if (delay > pInfo->maxDelay) pInfo->maxDelay = delay;
      if (delay < pInfo->minDelay) pInfo->minDelay = delay;

	  curMs = taosGetTimestampMs();
      if (curMs -  beginMs > 10000) {
	  	beginMs = curMs;
S
Shengliang Guan 已提交
237 238 239
        printCreateProgress(pInfo, t);
      }
      t += (batch - 1);
S
TD-1415  
Shengliang Guan 已提交
240
    }
S
Shengliang Guan 已提交
241
    printCreateProgress(pInfo, pInfo->tableEndIndex);
S
Shengliang Guan 已提交
242 243 244
  }

  if (insertData) {
L
lihui 已提交
245 246 247
    int64_t curMs = 0;
    int64_t beginMs = taosGetTimestampMs();;

S
Shengliang Guan 已提交
248
    pInfo->startMs = taosGetTimestampMs();
S
Shengliang Guan 已提交
249 250
    for (int64_t t = pInfo->tableBeginIndex; t < pInfo->tableEndIndex; ++t) {
      int64_t batch = (pInfo->tableEndIndex - t);
S
Shengliang Guan 已提交
251 252 253 254
      batch = MIN(batch, batchNum);

      int32_t len = sprintf(qstr, "insert into");
      for (int32_t i = 0; i < batch; ++i) {
S
Shengliang Guan 已提交
255
        len += sprintf(qstr + len, " t%" PRId64 " values(now, %" PRId64 ")", t + i, t + i);
S
Shengliang Guan 已提交
256 257
      }

L
lihui 已提交
258 259
      TAOS_RES *pRes = taos_query(con, qstr);
      code = taos_errno(pRes);
S
TD-1415  
Shengliang Guan 已提交
260
      if (code != 0) {
S
Shengliang Guan 已提交
261
        pError("failed to insert table t%" PRId64 ", reason:%s", t, tstrerror(code));
S
TD-1415  
Shengliang Guan 已提交
262
      }
L
lihui 已提交
263
      taos_free_result(pRes);
S
Shengliang Guan 已提交
264

L
lihui 已提交
265 266
      curMs = taosGetTimestampMs();
      if (curMs -  beginMs > 10000) {
S
Shengliang Guan 已提交
267 268 269
        printInsertProgress(pInfo, t);
      }
      t += (batch - 1);
270
    }
S
Shengliang Guan 已提交
271
    printInsertProgress(pInfo, pInfo->tableEndIndex);
272 273 274
  }

  taos_close(con);
S
Shengliang Guan 已提交
275
  free(qstr);
276 277 278 279 280 281 282 283 284 285 286 287
  return 0;
}

void printHelp() {
  char indent[10] = "        ";
  printf("Used to test the performance while create table\n");

  printf("%s%s\n", indent, "-c");
  printf("%s%s%s%s\n", indent, indent, "Configuration directory, default is ", configDir);
  printf("%s%s\n", indent, "-d");
  printf("%s%s%s%s\n", indent, indent, "The name of the database to be created, default is ", dbName);
  printf("%s%s\n", indent, "-s");
S
Shengliang Guan 已提交
288
  printf("%s%s%s%s\n", indent, indent, "The name of the super table to be created, default is ", stbName);
289 290 291
  printf("%s%s\n", indent, "-t");
  printf("%s%s%s%d\n", indent, indent, "numOfThreads, default is ", numOfThreads);
  printf("%s%s\n", indent, "-n");
S
Shengliang Guan 已提交
292
  printf("%s%s%s%" PRId64 "\n", indent, indent, "numOfTables, default is ", numOfTables);
S
Shengliang Guan 已提交
293 294 295 296 297 298 299 300 301
  printf("%s%s\n", indent, "-v");
  printf("%s%s%s%d\n", indent, indent, "numOfVgroups, default is ", numOfVgroups);
  printf("%s%s\n", indent, "-a");
  printf("%s%s%s%d\n", indent, indent, "createTable, default is ", createTable);
  printf("%s%s\n", indent, "-i");
  printf("%s%s%s%d\n", indent, indent, "insertData, default is ", insertData);
  printf("%s%s\n", indent, "-b");
  printf("%s%s%s%d\n", indent, indent, "batchNum, default is ", batchNum);

302 303 304
  exit(EXIT_SUCCESS);
}

S
Shengliang Guan 已提交
305 306
void parseArgument(int32_t argc, char *argv[]) {
  for (int32_t i = 1; i < argc; i++) {
307 308 309 310 311 312 313 314
    if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
      printHelp();
      exit(0);
    } else if (strcmp(argv[i], "-d") == 0) {
      strcpy(dbName, argv[++i]);
    } else if (strcmp(argv[i], "-c") == 0) {
      strcpy(configDir, argv[++i]);
    } else if (strcmp(argv[i], "-s") == 0) {
S
Shengliang Guan 已提交
315
      strcpy(stbName, argv[++i]);
316 317 318
    } else if (strcmp(argv[i], "-t") == 0) {
      numOfThreads = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-n") == 0) {
S
Shengliang Guan 已提交
319
      numOfTables = atoll(argv[++i]);
L
lihui 已提交
320
    } else if (strcmp(argv[i], "-v") == 0) {
S
Shengliang Guan 已提交
321 322 323 324 325 326 327
      numOfVgroups = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-a") == 0) {
      createTable = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-i") == 0) {
      insertData = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-b") == 0) {
      batchNum = atoi(argv[++i]);
328 329 330 331 332
    } else {
    }
  }

  pPrint("%s dbName:%s %s", GREEN, dbName, NC);
S
Shengliang Guan 已提交
333
  pPrint("%s stbName:%s %s", GREEN, stbName, NC);
334
  pPrint("%s configDir:%s %s", GREEN, configDir, NC);
S
Shengliang Guan 已提交
335
  pPrint("%s numOfTables:%" PRId64 " %s", GREEN, numOfTables, NC);
336
  pPrint("%s numOfThreads:%d %s", GREEN, numOfThreads, NC);
S
Shengliang Guan 已提交
337 338 339 340 341
  pPrint("%s numOfVgroups:%d %s", GREEN, numOfVgroups, NC);
  pPrint("%s createTable:%d %s", GREEN, createTable, NC);
  pPrint("%s insertData:%d %s", GREEN, insertData, NC);
  pPrint("%s batchNum:%d %s", GREEN, batchNum, NC);

342 343
  pPrint("%s start create table performace test %s", GREEN, NC);
}