insertPerRow.c 12.6 KB
Newer Older
S
slguan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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
H
Haojun Liao 已提交
17
#include "../../../include/client/taos.h"
S
slguan 已提交
18
#include "os.h"
H
Haojun Liao 已提交
19
#include "tglobal.h"
S
slguan 已提交
20
#include "ttimer.h"
H
Haojun Liao 已提交
21
#include "tulog.h"
S
slguan 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35
#include "tutil.h"

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

typedef struct {
  int64_t   rowsPerTable;
  int64_t   pointsPerTable;
  int64_t   tableBeginIndex;
  int64_t   tableEndIndex;
  int       threadIndex;
  char      dbName[32];
  char      stableName[64];
36
  float     createTableSpeed;
S
slguan 已提交
37 38 39 40 41 42 43 44 45 46
  pthread_t thread;
} SInfo;

void *syncTest(void *param);
void  generateRandomPoints();
void  shellParseArgument(int argc, char *argv[]);
void  createDbAndTable();
void  insertData();

int32_t randomData[MAX_RANDOM_POINTS];
S
Shengliang Guan 已提交
47
int64_t rowsPerTable = 1000000000;
S
slguan 已提交
48
int64_t pointsPerTable = 1;
S
Shengliang Guan 已提交
49 50
int64_t numOfThreads = 10;
int64_t numOfTablesPerThread = 100;
S
slguan 已提交
51 52
char    dbName[32] = "db";
char    stableName[64] = "st";
S
Shengliang Guan 已提交
53
int32_t cache = 1;
S
scripts  
Shengliang Guan 已提交
54
int32_t replica = 3;
S
Shengliang Guan 已提交
55 56
int32_t days = 10;
int32_t interval = 1000;
S
slguan 已提交
57 58 59 60 61 62 63 64 65 66

int main(int argc, char *argv[]) {
  shellParseArgument(argc, argv);
  generateRandomPoints();
  taos_init();
  createDbAndTable();
  insertData();
}

void createDbAndTable() {
S
slguan 已提交
67
  pPrint("start to create table");
S
slguan 已提交
68

69
  TAOS_RES *     pSql;
S
slguan 已提交
70 71 72
  TAOS *         con;
  char           qstr[64000];

J
jtao1735 已提交
73 74
  char fqdn[TSDB_FQDN_LEN];
  uint16_t port;
S
slguan 已提交
75
  taosGetFqdnPortFromEp(tsFirst, fqdn, &port);
76
  con = taos_connect(fqdn, "root", "taosdata", NULL, port);
S
slguan 已提交
77
  if (con == NULL) {
S
slguan 已提交
78
    pError("failed to connect to DB, reason:%s", taos_errstr(con));
S
slguan 已提交
79 80 81
    exit(1);
  }

S
Shengliang Guan 已提交
82
  sprintf(qstr, "create database if not exists %s cache %d replica %d days %d", dbName, cache, replica, days);
83 84 85 86
  pSql = taos_query(con, qstr);
  int32_t code = taos_errno(pSql);
  if (code != 0) {
    pError("failed to create database:%s, sql:%s, code:%d reason:%s", dbName, qstr, taos_errno(con), taos_errstr(con));
S
slguan 已提交
87 88
    exit(0);
  }
89
  taos_free_result(pSql);
S
slguan 已提交
90 91

  sprintf(qstr, "use %s", dbName);
92 93 94
  pSql = taos_query(con, qstr);
  code = taos_errno(pSql);
  if (code != 0) {
S
slguan 已提交
95
    pError("failed to use db, code:%d reason:%s", taos_errno(con), taos_errstr(con));
S
slguan 已提交
96 97
    exit(0);
  }
98
  taos_free_result(pSql);
S
slguan 已提交
99 100 101 102 103 104 105 106

  if (strcmp(stableName, "no") != 0) {
    int len = sprintf(qstr, "create table if not exists %s(ts timestamp", stableName);
    for (int64_t f = 0; f < pointsPerTable; ++f) {
      len += sprintf(qstr + len, ", f%ld double", f);
    }
    sprintf(qstr + len, ") tags(t int)");

107 108 109
    pSql = taos_query(con, qstr);
    code = taos_errno(pSql);
    if (code != 0) {
S
slguan 已提交
110
      pError("failed to create stable, code:%d reason:%s", taos_errno(con), taos_errstr(con));
S
slguan 已提交
111 112
      exit(0);
    }
113
    taos_free_result(pSql);
S
slguan 已提交
114 115 116 117 118 119 120 121 122 123
  }
}

void insertData() {
  struct timeval systemTime;
  int64_t        st, et;

  gettimeofday(&systemTime, NULL);
  st = systemTime.tv_sec * 1000000 + systemTime.tv_usec;

B
Bomin Zhang 已提交
124
  pPrint("%" PRId64 " threads are spawned to insert data", numOfThreads);
S
slguan 已提交
125 126 127 128

  pthread_attr_t thattr;
  pthread_attr_init(&thattr);
  pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE);
129
  SInfo *pInfo = (SInfo *)calloc(numOfThreads, sizeof(SInfo));
S
slguan 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

  // Start threads to write
  for (int i = 0; i < numOfThreads; ++i) {
    pInfo[i].rowsPerTable = rowsPerTable;
    pInfo[i].pointsPerTable = pointsPerTable;
    pInfo[i].tableBeginIndex = i * numOfTablesPerThread;
    pInfo[i].tableEndIndex = (i + 1) * numOfTablesPerThread;
    pInfo[i].threadIndex = i;
    strcpy(pInfo[i].dbName, dbName);
    strcpy(pInfo[i].stableName, stableName);
    pthread_create(&(pInfo[i].thread), &thattr, syncTest, (void *)(pInfo + i));
  }

  taosMsleep(300);
  for (int i = 0; i < numOfThreads; i++) {
    pthread_join(pInfo[i].thread, NULL);
  }

  gettimeofday(&systemTime, NULL);
  et = systemTime.tv_sec * 1000000 + systemTime.tv_usec;
  double seconds = (et - st) / 1000.0 / 1000.0;

  int64_t totalTables = numOfTablesPerThread * numOfThreads;
  int64_t totalRows = totalTables * rowsPerTable;
  int64_t totalPoints = totalTables * rowsPerTable * pointsPerTable;
  double  speedOfRows = totalRows / seconds;
  double  speedOfPoints = totalPoints / seconds;

158 159 160 161 162
  float createTableSpeed = 0;
  for (int i = 0; i < numOfThreads; ++i) {
    createTableSpeed += pInfo[i].createTableSpeed;
  }

S
slguan 已提交
163
  pPrint(
S
slguan 已提交
164
      "%sall threads:%ld finished, use %.1lf seconds, tables:%.ld rows:%ld points:%ld, speed RowsPerSecond:%.1lf "
165 166
      "PointsPerSecond:%.1lf CreateTableSpeed:%.1f t/s %s",
      GREEN, numOfThreads, seconds, totalTables, totalRows, totalPoints, speedOfRows, speedOfPoints, createTableSpeed, NC);
S
slguan 已提交
167

S
slguan 已提交
168
  pPrint("threads exit");
S
slguan 已提交
169 170 171 172 173 174 175 176 177 178 179 180

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

void *syncTest(void *param) {
  TAOS *         con;
  SInfo *        pInfo = (SInfo *)param;
  struct timeval systemTime;
  int64_t        st, et;
  char           qstr[65000];
  int            maxBytes = 60000;
181
  int            code;
S
slguan 已提交
182

S
slguan 已提交
183
  pPrint("thread:%d, start to run", pInfo->threadIndex);
S
slguan 已提交
184

J
jtao1735 已提交
185 186 187
  char     fqdn[TSDB_FQDN_LEN];
  uint16_t port;

S
slguan 已提交
188
  taosGetFqdnPortFromEp(tsFirst, fqdn, &port);
189
  con = taos_connect(fqdn, "root", "taosdata", NULL, port);
S
slguan 已提交
190
  if (con == NULL) {
S
slguan 已提交
191
    pError("index:%d, failed to connect to DB, reason:%s", pInfo->threadIndex, taos_errstr(con));
S
slguan 已提交
192 193 194 195 196 197 198 199 200
    exit(1);
  }

  sprintf(qstr, "use %s", pInfo->dbName);
  taos_query(con, qstr);

  gettimeofday(&systemTime, NULL);
  st = systemTime.tv_sec * 1000000 + systemTime.tv_usec;

201 202 203 204 205 206
  if (strcmp(stableName, "no") != 0) {
    for (int64_t t = pInfo->tableBeginIndex; t < pInfo->tableEndIndex; ++t) {
      sprintf(qstr, "create table if not exists %s%ld using %s tags(%ld)", stableName, t, stableName, t);
      TAOS_RES *pSql = taos_query(con, qstr);
      code = taos_errno(pSql);
      if (code != 0) {
B
Bomin Zhang 已提交
207
        pError("failed to create table %s%" PRId64 ", reason:%s", stableName, t, taos_errstr(con));
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
        exit(0);
      }
      taos_free_result(pSql);
    }
  } else {
    for (int64_t t = pInfo->tableBeginIndex; t < pInfo->tableEndIndex; ++t) {
      int len = sprintf(qstr, "create table if not exists %s%ld(ts timestamp", stableName, t);
      for (int64_t f = 0; f < pointsPerTable; ++f) {
        len += sprintf(qstr + len, ", f%ld double", f);
      }
      sprintf(qstr + len, ")");

      TAOS_RES *pSql = taos_query(con, qstr);
      code = taos_errno(pSql);
      if (code != 0) {
        pError("failed to create table %s%ld, reason:%s", stableName, t, taos_errstr(con));
        exit(0);
      }
      taos_free_result(pSql);
    }
  }

  gettimeofday(&systemTime, NULL);
  et = systemTime.tv_sec * 1000000 + systemTime.tv_usec;
  float seconds = (et - st) / 1000.0 / 1000.0;
  int64_t tables = pInfo->tableEndIndex - pInfo->tableBeginIndex;
  pInfo->createTableSpeed = (float)tables / seconds;
  pPrint("thread:%d, %.1f seconds to create %ld tables, speed:%.1f", pInfo->threadIndex, seconds, tables,
         pInfo->createTableSpeed);

  if (pInfo->rowsPerTable == 0) return NULL;       

  gettimeofday(&systemTime, NULL);
  st = systemTime.tv_sec * 1000000 + systemTime.tv_usec;

S
slguan 已提交
243
  int64_t start = 1430000000000;
S
Shengliang Guan 已提交
244
  interval = 1000;  // 1000 ms
S
slguan 已提交
245 246 247 248 249

  char *sql = qstr;
  char  inserStr[] = "insert into";
  int   len = sprintf(sql, "%s", inserStr);

S
slguan 已提交
250 251 252
  for (int64_t row = 0; row < pInfo->rowsPerTable; row++) {    
    for (int64_t table = pInfo->tableBeginIndex; table < pInfo->tableEndIndex; ++table) {
      len += sprintf(sql + len, " %s%ld values", pInfo->stableName, table);
S
slguan 已提交
253 254 255 256 257 258 259
      len += sprintf(sql + len, "(%ld", start + row * interval);
      for (int64_t point = 0; point < pInfo->pointsPerTable; ++point) {
        len += sprintf(sql + len, ",%d", randomData[(123 * table + 456 * row + 789 * point) % MAX_RANDOM_POINTS]);
        // len += sprintf(sql + len, ",%ld", row);
      }
      len += sprintf(sql + len, ")");
      if (len > maxBytes) {
260 261 262
        TAOS_RES *pSql = taos_query(con, qstr);
        int32_t code = taos_errno(pSql);
        if (code != 0) {
S
slguan 已提交
263
          pError("thread:%d, failed to insert table:%s%ld row:%ld, reason:%s", pInfo->threadIndex, pInfo->stableName,
S
slguan 已提交
264 265
                 table, row, taos_errstr(con));
        }
266
        taos_free_result(pSql);
S
slguan 已提交
267

S
slguan 已提交
268 269 270 271 272 273 274
        // "insert into"
        len = sprintf(sql, "%s", inserStr);
      }
    }
  }

  if (len != strlen(inserStr)) {
275 276
    TAOS_RES *pSql = taos_query(con, qstr);
    taos_free_result(pSql);
S
slguan 已提交
277 278 279 280 281 282 283
  }

  gettimeofday(&systemTime, NULL);
  et = systemTime.tv_sec * 1000000 + systemTime.tv_usec;
  int64_t totalTables = pInfo->tableEndIndex - pInfo->tableBeginIndex;
  int64_t totalRows = totalTables * pInfo->rowsPerTable;
  int64_t totalPoints = totalRows * pInfo->pointsPerTable;
S
slguan 已提交
284
  pPrint("thread:%d, insert finished, use %.2f seconds, tables:%ld rows:%ld points:%ld", pInfo->threadIndex,
S
slguan 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297
         (et - st) / 1000.0 / 1000.0, totalTables, totalRows, totalPoints);

  return NULL;
}

void generateRandomPoints() {
  for (int r = 0; r < MAX_RANDOM_POINTS; ++r) {
    randomData[r] = rand() % 1000;
  }
}

void printHelp() {
  char indent[10] = "        ";
S
slguan 已提交
298
  printf("Used to test the performance of TDengine\n After writing one row of data to all tables, write the next row\n");
S
slguan 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313

  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");
  printf("%s%s%s%s%s\n", indent, indent, "The name of the super table to be created, default is ", stableName, ", if 'no' then create normal table");
  printf("%s%s\n", indent, "-c");
  printf("%s%s%s%s\n", indent, indent, "Configuration directory, default is ", configDir);
  printf("%s%s\n", indent, "-r");
  printf("%s%s%s%ld\n", indent, indent, "Number of records to write to each table, default is ", rowsPerTable);
  printf("%s%s\n", indent, "-p");
  printf("%s%s%s%" PRId64 "\n", indent, indent, "Number of columns per table, default is ", pointsPerTable);
  printf("%s%s\n", indent, "-t");
  printf("%s%s%s%" PRId64 "\n", indent, indent, "Number of threads to be used, default is ", numOfThreads);
  printf("%s%s\n", indent, "-n");
  printf("%s%s%s%" PRId64 "\n", indent, indent, "Number of tables per thread, default is ", numOfTablesPerThread);
S
Shengliang Guan 已提交
314 315
  printf("%s%s\n", indent, "-replica");
  printf("%s%s%s%d\n", indent, indent, "Database parameters replica, default is ", replica);
S
slguan 已提交
316
  printf("%s%s\n", indent, "-cache");
S
scripts  
Shengliang Guan 已提交
317
  printf("%s%s%s%d\n", indent, indent, "Database parameters replica, default is ", cache);
S
Shengliang Guan 已提交
318 319 320 321
  printf("%s%s\n", indent, "-days");
  printf("%s%s%s%d\n", indent, indent, "Database parameters days, default is ", days);
  printf("%s%s\n", indent, "-interval");
  printf("%s%s%s%d\n", indent, indent, "Interval of each rows in ms, default is ", interval);
S
slguan 已提交
322 323 324 325 326 327 328 329 330 331 332

  exit(EXIT_SUCCESS);
}

void shellParseArgument(int argc, char *argv[]) {
  for (int i = 1; i < argc; i++) {
    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]);
S
slguan 已提交
333 334
    } else if (strcmp(argv[i], "-c") == 0) {
      strcpy(configDir, argv[++i]);
S
slguan 已提交
335 336 337 338 339 340 341 342 343 344
    } else if (strcmp(argv[i], "-s") == 0) {
      strcpy(stableName, argv[++i]);
    } else if (strcmp(argv[i], "-r") == 0) {
      rowsPerTable = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-p") == 0) {
      pointsPerTable = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-t") == 0) {
      numOfThreads = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-n") == 0) {
      numOfTablesPerThread = atoi(argv[++i]);
S
Shengliang Guan 已提交
345 346
    } else if (strcmp(argv[i], "-replica") == 0) {
      replica = atoi(argv[++i]);
S
slguan 已提交
347 348
    } else if (strcmp(argv[i], "-cache") == 0) {
      cache = atoi(argv[++i]);
S
Shengliang Guan 已提交
349 350 351 352
    } else if (strcmp(argv[i], "-days") == 0) {
      days = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-interval") == 0) {
      interval = atoi(argv[++i]);
S
slguan 已提交
353 354 355 356
    } else {
    }
  }

S
slguan 已提交
357 358 359 360
  pPrint("%srowsPerTable:%" PRId64 "%s", GREEN, rowsPerTable, NC);
  pPrint("%spointsPerTable:%" PRId64 "%s", GREEN, pointsPerTable, NC);
  pPrint("%snumOfThreads:%" PRId64 "%s", GREEN, numOfThreads, NC);
  pPrint("%snumOfTablesPerThread:%" PRId64 "%s", GREEN, numOfTablesPerThread, NC);
B
Bomin Zhang 已提交
361
  pPrint("%scache:%" PRId32 "%s", GREEN, cache, NC);
S
Shengliang Guan 已提交
362
  pPrint("%stables:%" PRId32 "%s", GREEN, replica, NC);
S
slguan 已提交
363 364 365
  pPrint("%sdbName:%s%s", GREEN, dbName, NC);
  pPrint("%stableName:%s%s", GREEN, stableName, NC);
  pPrint("%sstart to run%s", GREEN, NC);
S
slguan 已提交
366
}