importOneRow.c 4.3 KB
Newer Older
S
slguan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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"
#include "taos.h"
S
slguan 已提交
19
#include "tulog.h"
S
slguan 已提交
20 21
#include "ttimer.h"
#include "tutil.h"
S
slguan 已提交
22
#include "tglobal.h"
S
slguan 已提交
23

S
slguan 已提交
24 25 26 27
#define MAX_RANDOM_POINTS 20000
#define GREEN "\033[1;32m"
#define NC "\033[0m"

S
slguan 已提交
28 29 30 31 32 33 34 35 36 37 38 39
void taos_error(TAOS *taos);
void* taos_execute(void *param);

typedef struct {
  pthread_t pid;
  int       index;
} ThreadObj;

int threadNum = 1;
int rowNum = 1000;
int replica = 1;

S
slguan 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
void printHelp() {
  char indent[10] = "        ";
  printf("Used to test the performance of TDengine\n After writing one row of data to all tables, write the next row\n");

  printf("%s%s\n", indent, "-r");
  printf("%s%s%s%d\n", indent, indent, "Number of records to write table, default is ", rowNum);
  printf("%s%s\n", indent, "-t");
  printf("%s%s%s%d\n", indent, indent, "Number of threads to be used, default is ", threadNum);
  printf("%s%s\n", indent, "-replica");
  printf("%s%s%s%d\n", indent, indent, "Database parameters replica, default is ", replica);

  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], "-r") == 0) {
      rowNum = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-t") == 0) {
      threadNum = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-replica") == 0) {
      replica = atoi(argv[++i]);
    } else {
    }
S
slguan 已提交
67 68
  }

S
slguan 已提交
69 70 71
  uPrint("%s rowNum:%d %s", GREEN, rowNum, NC);
  uPrint("%s threadNum:%d %s", GREEN, threadNum, NC);
  uPrint("%s replica:%d %s", GREEN, replica, NC);
S
slguan 已提交
72
}
S
slguan 已提交
73

S
slguan 已提交
74 75
int main(int argc, char *argv[]) {
  shellParseArgument(argc, argv);
S
slguan 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

  taos_init();

  ThreadObj *threads = calloc(threadNum, sizeof(ThreadObj));
  for (int i = 0; i < threadNum; ++i) {
    ThreadObj *    pthread = threads + i;
    pthread_attr_t thattr;
    pthread->index = i;
    pthread_attr_init(&thattr);
    pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE);
    pthread_create(&pthread->pid, &thattr, taos_execute, pthread);
  }

  for (int i = 0; i < threadNum; i++) {
    pthread_join(threads[i].pid, NULL);
  }

  printf("all finished\n");

  return 0;
}

void taos_error(TAOS *con) {
  fprintf(stderr, "TDengine error: %s\n", taos_errstr(con));
  taos_close(con);
  exit(1);
}

void* taos_execute(void *param) {
  ThreadObj *pThread = (ThreadObj *)param;

  void *taos = taos_connect(tsMasterIp, tsDefaultUser, tsDefaultPass, NULL, 0);
  if (taos == NULL) taos_error(taos);

  char sql[1024] = {0};
  sprintf(sql, "create database if not exists db replica %d", replica);
  taos_query(taos, sql);

  sprintf(sql, "create table if not exists db.t%d (ts timestamp, i int, j float, k double)", pThread->index);
  taos_query(taos, sql);

  int64_t timestamp = 1530374400000L;

  sprintf(sql, "insert into db.t%d values(%ld, %d, %d, %d)", pThread->index, timestamp, 0, 0, 0);
  int code = taos_query(taos, sql);
  if (code != 0) printf("error code:%d, sql:%s\n", code, sql);
  int affectrows = taos_affected_rows(taos);
  if (affectrows != 1) printf("affect rows:%d, sql:%s\n", affectrows, sql);

  timestamp -= 1000;

  int total_affect_rows = affectrows;

  for (int i = 1; i < rowNum; ++i) {
    sprintf(sql, "import into db.t%d values(%ld, %d, %d, %d)", pThread->index, timestamp, i, i, i);
    code = taos_query(taos, sql);
    if (code != 0) printf("error code:%d, sql:%s\n", code, sql);
    int affectrows = taos_affected_rows(taos);
    if (affectrows != 1) printf("affect rows:%d, sql:%s\n", affectrows, sql);

    total_affect_rows += affectrows;

    timestamp -= 1000;
  }

  printf("thread:%d run finished total_affect_rows:%d\n", pThread->index, total_affect_rows);

  return NULL;
}