tsdbTests.cpp 4.7 KB
Newer Older
H
hzcheng 已提交
1 2
#include <gtest/gtest.h>
#include <stdlib.h>
H
TD-34  
hzcheng 已提交
3
#include <sys/time.h>
H
hzcheng 已提交
4

H
Hongze Cheng 已提交
5
#include "tsdb.h"
H
TD-34  
hzcheng 已提交
6
#include "tsdbMain.h"
H
TD-27  
hzcheng 已提交
7

H
TD-100  
hzcheng 已提交
8
static double getCurTime() {
H
TD-34  
hzcheng 已提交
9 10 11 12 13
  struct timeval tv;
  gettimeofday(&tv, NULL);
  return tv.tv_sec + tv.tv_usec * 1E-6;
}

H
TD-100  
hzcheng 已提交
14
typedef struct {
H
Hongze Cheng 已提交
15
  STsdbRepo *pRepo;
H
TD-100  
hzcheng 已提交
16 17
  bool       isAscend;
  int        tid;
18
  uint64_t   uid;
H
TD-100  
hzcheng 已提交
19 20 21 22 23 24
  int        sversion;
  TSKEY      startTime;
  TSKEY      interval;
  int        totalRows;
  int        rowsPerSubmit;
  STSchema * pSchema;
H
TD-100  
hzcheng 已提交
25 26 27 28
} SInsertInfo;

static int insertData(SInsertInfo *pInfo) {
  SSubmitMsg *pMsg =
H
TD-166  
hzcheng 已提交
29
      (SSubmitMsg *)malloc(sizeof(SSubmitMsg) + sizeof(SSubmitBlk) + dataRowMaxBytesFromSchema(pInfo->pSchema) * pInfo->rowsPerSubmit);
H
TD-100  
hzcheng 已提交
30 31 32 33 34 35 36 37
  if (pMsg == NULL) return -1;
  TSKEY start_time = pInfo->startTime;

  // Loop to write data
  double stime = getCurTime();

  for (int k = 0; k < pInfo->totalRows/pInfo->rowsPerSubmit; k++) {
    memset((void *)pMsg, 0, sizeof(SSubmitMsg));
H
Hongze Cheng 已提交
38
    SSubmitBlk *pBlock = (SSubmitBlk *)pMsg->blocks;
H
TD-100  
hzcheng 已提交
39 40 41
    pBlock->uid = pInfo->uid;
    pBlock->tid = pInfo->tid;
    pBlock->sversion = pInfo->sversion;
H
Hongze Cheng 已提交
42 43 44
    pBlock->dataLen = 0;
    pBlock->schemaLen = 0;
    pBlock->numOfRows = 0;
H
TD-100  
hzcheng 已提交
45 46
    for (int i = 0; i < pInfo->rowsPerSubmit; i++) {
      // start_time += 1000;
H
TD-100  
hzcheng 已提交
47 48 49 50 51
      if (pInfo->isAscend) {
        start_time += pInfo->interval;
      } else {
        start_time -= pInfo->interval;
      }
H
Hongze Cheng 已提交
52
      SDataRow row = (SDataRow)(pBlock->data + pBlock->dataLen);
H
TD-100  
hzcheng 已提交
53 54 55
      tdInitDataRow(row, pInfo->pSchema);

      for (int j = 0; j < schemaNCols(pInfo->pSchema); j++) {
H
TD-166  
hzcheng 已提交
56
        STColumn *pTCol = schemaColAt(pInfo->pSchema, j);
H
TD-100  
hzcheng 已提交
57
        if (j == 0) {  // Just for timestamp
C
Cary Xu 已提交
58
          tdAppendColVal(row, (void *)(&start_time), pTCol->type, pTCol->offset);
H
TD-100  
hzcheng 已提交
59 60
        } else {  // For int
          int val = 10;
C
Cary Xu 已提交
61
          tdAppendColVal(row, (void *)(&val), pTCol->type, pTCol->offset);
H
TD-100  
hzcheng 已提交
62 63
        }
      }
H
Hongze Cheng 已提交
64 65
      pBlock->dataLen += dataRowLen(row);
      pBlock->numOfRows++;
H
TD-100  
hzcheng 已提交
66
    }
H
Hongze Cheng 已提交
67
    pMsg->length = sizeof(SSubmitMsg) + sizeof(SSubmitBlk) + pBlock->dataLen;
H
TD-100  
hzcheng 已提交
68 69
    pMsg->numOfBlocks = 1;

H
Hongze Cheng 已提交
70
    pBlock->dataLen = htonl(pBlock->dataLen);
H
TD-100  
hzcheng 已提交
71
    pBlock->numOfRows = htonl(pBlock->numOfRows);
H
Hongze Cheng 已提交
72
    pBlock->schemaLen = htonl(pBlock->schemaLen);
H
TD-100  
hzcheng 已提交
73 74 75 76 77 78 79 80 81
    pBlock->uid = htobe64(pBlock->uid);
    pBlock->tid = htonl(pBlock->tid);

    pBlock->sversion = htonl(pBlock->sversion);
    pBlock->padding = htonl(pBlock->padding);

    pMsg->length = htonl(pMsg->length);
    pMsg->numOfBlocks = htonl(pMsg->numOfBlocks);

H
Hongze Cheng 已提交
82
    if (tsdbInsertData(pInfo->pRepo, pMsg, NULL) < 0) {
S
TD-1848  
Shengliang Guan 已提交
83
      tfree(pMsg);
H
TD-100  
hzcheng 已提交
84 85 86 87 88 89 90
      return -1;
    }
  }

  double etime = getCurTime();

  printf("Spent %f seconds to write %d records\n", etime - stime, pInfo->totalRows);
S
TD-1848  
Shengliang Guan 已提交
91
  tfree(pMsg);
H
TD-100  
hzcheng 已提交
92 93 94
  return 0;
}

H
Hongze Cheng 已提交
95 96 97 98 99 100
static void tsdbSetCfg(STsdbCfg *pCfg, int32_t tsdbId, int32_t cacheBlockSize, int32_t totalBlocks, int32_t maxTables,
                       int32_t daysPerFile, int32_t keep, int32_t minRows, int32_t maxRows, int8_t precision,
                       int8_t compression) {
  pCfg->tsdbId = tsdbId;
  pCfg->cacheBlockSize = cacheBlockSize;
  pCfg->totalBlocks = totalBlocks;
H
Hongze Cheng 已提交
101
  // pCfg->maxTables = maxTables;
H
Hongze Cheng 已提交
102 103 104 105 106 107
  pCfg->daysPerFile = daysPerFile;
  pCfg->keep = keep;
  pCfg->minRowsPerFileBlock = minRows;
  pCfg->maxRowsPerFileBlock = maxRows;
  pCfg->precision = precision;
  pCfg->compression = compression;
H
TD-27  
hzcheng 已提交
108
}
H
hzcheng 已提交
109

H
Hongze Cheng 已提交
110 111
static void tsdbSetTableCfg(STableCfg *pCfg) {
  STSchemaBuilder schemaBuilder = {0};
H
TD-100  
hzcheng 已提交
112

H
Hongze Cheng 已提交
113 114 115 116 117
  pCfg->type = TSDB_NORMAL_TABLE;
  pCfg->superUid = TSDB_INVALID_SUPER_TABLE_ID;
  pCfg->tableId.tid = 1;
  pCfg->tableId.uid = 5849583783847394;
  tdInitTSchemaBuilder(&schemaBuilder, 0);
H
TD-100  
hzcheng 已提交
118

H
Hongze Cheng 已提交
119 120 121 122
  int colId = 0;
  for (int i = 0; i < 5; i++) {
    tdAddColToSchema(&schemaBuilder, (colId == 0) ? TSDB_DATA_TYPE_TIMESTAMP : TSDB_DATA_TYPE_INT, colId, 0);
    colId++;
H
TD-100  
hzcheng 已提交
123 124
  }

H
Hongze Cheng 已提交
125 126
  pCfg->schema = tdGetSchemaFromBuilder(&schemaBuilder);
  pCfg->name = strdup("t1");
H
TD-100  
hzcheng 已提交
127

H
Hongze Cheng 已提交
128
  tdDestroyTSchemaBuilder(&schemaBuilder);
H
hzcheng 已提交
129 130
}

H
Hongze Cheng 已提交
131 132 133 134 135
TEST(TsdbTest, testInsertSpeed) {
  int         vnode = 1;
  int         ret = 0;
  STsdbCfg    tsdbCfg;
  STableCfg   tableCfg;
H
Hongze Cheng 已提交
136
  std::string testDir = "./test";
H
Hongze Cheng 已提交
137
  char *      rootDir = strdup((testDir + "/vnode" + std::to_string(vnode)).c_str());
H
TD-34  
hzcheng 已提交
138

H
Hongze Cheng 已提交
139
  tsdbDebugFlag = 131; //NOTE: you must set the flag
H
TD-34  
hzcheng 已提交
140

H
Hongze Cheng 已提交
141
  taosRemoveDir(rootDir);
H
TD-34  
hzcheng 已提交
142

H
Hongze Cheng 已提交
143 144 145
  // Create and open repository
  tsdbSetCfg(&tsdbCfg, 1, 16, 4, -1, -1, -1, -1, -1, -1, -1);
  tsdbCreateRepo(rootDir, &tsdbCfg);
H
Hongze Cheng 已提交
146
  STsdbRepo *repo = tsdbOpenRepo(rootDir, NULL);
H
Hongze Cheng 已提交
147
  ASSERT_NE(repo, nullptr);
H
TD-34  
hzcheng 已提交
148

H
Hongze Cheng 已提交
149 150 151
  // Create table
  tsdbSetTableCfg(&tableCfg);
  tsdbCreateTable(repo, &tableCfg);
H
TD-34  
hzcheng 已提交
152

H
Hongze Cheng 已提交
153
  // Insert data
H
Hongze Cheng 已提交
154
  SInsertInfo iInfo = {repo, true, 1, 5849583783847394, 0, 1590000000000, 10, 10000000, 100, tableCfg.schema};
H
TD-34  
hzcheng 已提交
155

H
Hongze Cheng 已提交
156
  insertData(&iInfo);
H
TD-34  
hzcheng 已提交
157

H
Hongze Cheng 已提交
158
  tsdbCloseRepo(repo, 1);
H
TD-100  
hzcheng 已提交
159 160 161 162
}

static char *getTKey(const void *data) {
  return (char *)data;
H
TD-27  
hzcheng 已提交
163
}