tsdbTests.cpp 4.6 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
TD-353  
Hongze Cheng 已提交
15
  TSDB_REPO_T *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 38 39 40 41 42 43 44
  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));
    SSubmitBlk *pBlock = pMsg->blocks;
    pBlock->uid = pInfo->uid;
    pBlock->tid = pInfo->tid;
    pBlock->sversion = pInfo->sversion;
    pBlock->len = 0;
    for (int i = 0; i < pInfo->rowsPerSubmit; i++) {
      // start_time += 1000;
H
TD-100  
hzcheng 已提交
45 46 47 48 49
      if (pInfo->isAscend) {
        start_time += pInfo->interval;
      } else {
        start_time -= pInfo->interval;
      }
H
TD-100  
hzcheng 已提交
50 51 52 53
      SDataRow row = (SDataRow)(pBlock->data + pBlock->len);
      tdInitDataRow(row, pInfo->pSchema);

      for (int j = 0; j < schemaNCols(pInfo->pSchema); j++) {
H
TD-166  
hzcheng 已提交
54
        STColumn *pTCol = schemaColAt(pInfo->pSchema, j);
H
TD-100  
hzcheng 已提交
55
        if (j == 0) {  // Just for timestamp
H
TD-166  
hzcheng 已提交
56
          tdAppendColVal(row, (void *)(&start_time), pTCol->type, pTCol->bytes, pTCol->offset);
H
TD-100  
hzcheng 已提交
57 58
        } else {  // For int
          int val = 10;
H
TD-166  
hzcheng 已提交
59
          tdAppendColVal(row, (void *)(&val), pTCol->type, pTCol->bytes, pTCol->offset);
H
TD-100  
hzcheng 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
        }
      }
      pBlock->len += dataRowLen(row);
    }
    pMsg->length = pMsg->length + sizeof(SSubmitBlk) + pBlock->len;
    pMsg->numOfBlocks = 1;

    pBlock->len = htonl(pBlock->len);
    pBlock->numOfRows = htonl(pBlock->numOfRows);
    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);
    pMsg->compressed = htonl(pMsg->numOfBlocks);

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

  double etime = getCurTime();

  printf("Spent %f seconds to write %d records\n", etime - stime, pInfo->totalRows);
  tfree(pMsg);
  return 0;
}

H
Hongze Cheng 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104
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;
  pCfg->maxTables = maxTables;
  pCfg->daysPerFile = daysPerFile;
  pCfg->keep = keep;
  pCfg->minRowsPerFileBlock = minRows;
  pCfg->maxRowsPerFileBlock = maxRows;
  pCfg->precision = precision;
  pCfg->compression = compression;
H
TD-27  
hzcheng 已提交
105
}
H
hzcheng 已提交
106

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

H
Hongze Cheng 已提交
110 111 112 113 114
  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 已提交
115

H
Hongze Cheng 已提交
116 117 118 119
  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 已提交
120 121
  }

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

H
Hongze Cheng 已提交
125
  tdDestroyTSchemaBuilder(&schemaBuilder);
H
hzcheng 已提交
126 127
}

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

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

H
Hongze Cheng 已提交
138
  taosRemoveDir(rootDir);
H
TD-34  
hzcheng 已提交
139

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

H
Hongze Cheng 已提交
146 147 148
  // Create table
  tsdbSetTableCfg(&tableCfg);
  tsdbCreateTable(repo, &tableCfg);
H
TD-34  
hzcheng 已提交
149

H
Hongze Cheng 已提交
150 151
  // Insert data
  SInsertInfo iInfo = {repo, true, 1, 5849583783847394, 0, 1590000000000, 10, 10000000, 1000, tableCfg.schema};
H
TD-34  
hzcheng 已提交
152

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

H
Hongze Cheng 已提交
155
  tsdbCloseRepo(repo, 1);
H
TD-100  
hzcheng 已提交
156 157 158 159
}

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