tsdbTests.cpp 4.2 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
hzcheng 已提交
5
#include "tsdb.h"
H
hzcheng 已提交
6
#include "dataformat.h"
H
TD-34  
hzcheng 已提交
7
#include "tsdbFile.h"
H
TD-27  
hzcheng 已提交
8 9
#include "tsdbMeta.h"

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

H
TD-34  
hzcheng 已提交
16
TEST(TsdbTest, DISABLED_tableEncodeDecode) {
H
TD-27  
hzcheng 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  STable *pTable = (STable *)malloc(sizeof(STable));

  pTable->type = TSDB_NORMAL_TABLE;
  pTable->tableId.uid = 987607499877672L;
  pTable->tableId.tid = 0;
  pTable->superUid = -1;
  pTable->sversion = 0;
  pTable->tagSchema = NULL;
  pTable->tagVal = NULL;
  int nCols = 5;
  STSchema *schema = tdNewSchema(nCols);

  for (int i = 0; i < nCols; i++) {
    if (i == 0) {
      tdSchemaAppendCol(schema, TSDB_DATA_TYPE_TIMESTAMP, i, -1);
    } else {
      tdSchemaAppendCol(schema, TSDB_DATA_TYPE_INT, i, -1);
    }
  }

  pTable->schema = schema;

  int bufLen = 0;
  void *buf = tsdbEncodeTable(pTable, &bufLen);

  STable *tTable = tsdbDecodeTable(buf, bufLen);

  ASSERT_EQ(pTable->type, tTable->type);
  ASSERT_EQ(pTable->tableId.uid, tTable->tableId.uid);
  ASSERT_EQ(pTable->tableId.tid, tTable->tableId.tid);
  ASSERT_EQ(pTable->superUid, tTable->superUid);
  ASSERT_EQ(pTable->sversion, tTable->sversion);
  ASSERT_EQ(memcmp(pTable->schema, tTable->schema, sizeof(STSchema) + sizeof(STColumn) * nCols), 0);
}
H
hzcheng 已提交
51

H
TD-34  
hzcheng 已提交
52 53
// TEST(TsdbTest, DISABLED_createRepo) {
TEST(TsdbTest, createRepo) {
H
hzcheng 已提交
54
  STsdbCfg config;
H
hzcheng 已提交
55

H
hzcheng 已提交
56
  // 1. Create a tsdb repository
H
hzcheng 已提交
57 58
  tsdbSetDefaultCfg(&config);
  tsdb_repo_t *pRepo = tsdbCreateRepo("/home/ubuntu/work/ttest/vnode0", &config, NULL);
H
hzcheng 已提交
59 60
  ASSERT_NE(pRepo, nullptr);

H
hzcheng 已提交
61 62 63
  // 2. Create a normal table
  STableCfg tCfg;
  ASSERT_EQ(tsdbInitTableCfg(&tCfg, TSDB_SUPER_TABLE, 987607499877672L, 0), -1);
S
[TD-10]  
slguan 已提交
64
  ASSERT_EQ(tsdbInitTableCfg(&tCfg, TSDB_NORMAL_TABLE, 987607499877672L, 0), 0);
H
hzcheng 已提交
65

H
hzcheng 已提交
66
  int       nCols = 5;
H
hzcheng 已提交
67 68
  STSchema *schema = tdNewSchema(nCols);

H
hzcheng 已提交
69
  for (int i = 0; i < nCols; i++) {
H
hzcheng 已提交
70 71 72 73 74 75
    if (i == 0) {
      tdSchemaAppendCol(schema, TSDB_DATA_TYPE_TIMESTAMP, i, -1);
    } else {
      tdSchemaAppendCol(schema, TSDB_DATA_TYPE_INT, i, -1);
    }
  }
H
hzcheng 已提交
76

H
hzcheng 已提交
77
  tsdbTableSetSchema(&tCfg, schema, true);
H
hzcheng 已提交
78

H
hzcheng 已提交
79
  tsdbCreateTable(pRepo, &tCfg);
H
hzcheng 已提交
80

H
hzcheng 已提交
81
  // // 3. Loop to write some simple data
H
TD-34  
hzcheng 已提交
82
  int nRows = 10000000;
H
TD-34  
hzcheng 已提交
83
  int rowsPerSubmit = 10;
H
more  
hzcheng 已提交
84 85
  int64_t start_time = 1584081000000;

H
TD-34  
hzcheng 已提交
86 87
  SSubmitMsg *pMsg = (SSubmitMsg *)malloc(sizeof(SSubmitMsg) + sizeof(SSubmitBlk) + tdMaxRowBytesFromSchema(schema) * rowsPerSubmit);

H
TD-34  
hzcheng 已提交
88 89
  double stime = getCurTime();

H
TD-34  
hzcheng 已提交
90
  for (int k = 0; k < nRows/rowsPerSubmit; k++) {
H
TD-34  
hzcheng 已提交
91
    memset((void *)pMsg, 0, sizeof(SSubmitMsg));
H
TD-34  
hzcheng 已提交
92
    SSubmitBlk *pBlock = pMsg->blocks;
H
TD-34  
hzcheng 已提交
93 94
    pBlock->uid = 987607499877672L;
    pBlock->tid = 0;
H
TD-34  
hzcheng 已提交
95 96 97
    pBlock->sversion = 0;
    pBlock->len = 0;
    for (int i = 0; i < rowsPerSubmit; i++) {
H
TD-34  
hzcheng 已提交
98 99
      // start_time += 1000;
      start_time -= 1000;
H
TD-34  
hzcheng 已提交
100 101 102 103 104 105 106 107 108 109 110 111
      SDataRow row = (SDataRow)(pBlock->data + pBlock->len);
      tdInitDataRow(row, schema);

      for (int j = 0; j < schemaNCols(schema); j++) {
        if (j == 0) {  // Just for timestamp
          tdAppendColVal(row, (void *)(&start_time), schemaColAt(schema, j));
        } else {  // For int
          int val = 10;
          tdAppendColVal(row, (void *)(&val), schemaColAt(schema, j));
        }
      }
      pBlock->len += dataRowLen(row);
H
more  
hzcheng 已提交
112
    }
H
TD-34  
hzcheng 已提交
113 114 115
    pMsg->length = pMsg->length + sizeof(SSubmitBlk) + pBlock->len;
    pMsg->numOfBlocks = 1;

H
TD-34  
hzcheng 已提交
116 117 118 119 120 121 122 123 124 125 126
    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
more  
hzcheng 已提交
127

H
TD-34  
hzcheng 已提交
128
    tsdbInsertData(pRepo, pMsg);
H
more  
hzcheng 已提交
129
  }
H
hzcheng 已提交
130

H
TD-34  
hzcheng 已提交
131 132
  double etime = getCurTime();

H
TD-34  
hzcheng 已提交
133 134 135
  void *ptr = malloc(150000);
  free(ptr);

H
TD-34  
hzcheng 已提交
136 137
  printf("Spent %f seconds to write %d records\n", etime - stime, nRows);

H
TD-34  
hzcheng 已提交
138
  tsdbCloseRepo(pRepo);
H
TD-34  
hzcheng 已提交
139

H
hzcheng 已提交
140 141
}

H
TD-34  
hzcheng 已提交
142 143
// TEST(TsdbTest, DISABLED_openRepo) {
TEST(TsdbTest, openRepo) {
H
TD-27  
hzcheng 已提交
144 145
  tsdb_repo_t *pRepo = tsdbOpenRepo("/home/ubuntu/work/ttest/vnode0");
  ASSERT_NE(pRepo, nullptr);
H
TD-34  
hzcheng 已提交
146 147
}

H
TD-34  
hzcheng 已提交
148
TEST(TsdbTest, DISABLED_createFileGroup) {
H
TD-34  
hzcheng 已提交
149 150
  SFileGroup fGroup;

H
TD-34  
hzcheng 已提交
151
  // ASSERT_EQ(tsdbCreateFileGroup("/home/ubuntu/work/ttest/vnode0/data", 1820, &fGroup, 1000), 0);
H
TD-34  
hzcheng 已提交
152 153

  int k = 0;
H
TD-27  
hzcheng 已提交
154
}