tsBufTest.cpp 10.8 KB
Newer Older
S
Shengliang Guan 已提交
1
#include "os.h"
H
hjxilinx 已提交
2 3 4 5 6 7
#include <gtest/gtest.h>
#include <cassert>
#include <iostream>

#include "taos.h"
#include "tsdb.h"
H
Haojun Liao 已提交
8
#include "qTsbuf.h"
H
hjxilinx 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include "tstoken.h"
#include "tutil.h"

namespace {
/**
 *
 * @param num  total number
 * @param step gap between two consecutive ts
 * @return
 */
int64_t* createTsList(int32_t num, int64_t start, int32_t step) {
  int64_t* pList = (int64_t*)malloc(num * sizeof(int64_t));

  for (int64_t i = 0; i < num; ++i) {
    pList[i] = start + i * step;
  }

  return pList;
}

// simple test
void simpleTest() {
H
Haojun Liao 已提交
31
  STSBuf* pTSBuf = tsBufCreate(true, TSDB_ORDER_ASC);
H
hjxilinx 已提交
32 33 34 35 36 37 38

  // write 10 ts points
  int32_t num = 10;
  int64_t tag = 1;

  int64_t* list = createTsList(10, 10000000, 30);
  tsBufAppend(pTSBuf, 0, tag, (const char*)list, num * sizeof(int64_t));
H
hjxilinx 已提交
39
  EXPECT_EQ(pTSBuf->tsOrder, TSDB_ORDER_ASC);
H
hjxilinx 已提交
40 41 42 43 44 45 46 47 48

  EXPECT_EQ(pTSBuf->tsData.len, sizeof(int64_t) * num);
  EXPECT_EQ(pTSBuf->block.tag, tag);
  EXPECT_EQ(pTSBuf->numOfVnodes, 1);

  tsBufFlush(pTSBuf);
  EXPECT_EQ(pTSBuf->tsData.len, 0);
  EXPECT_EQ(pTSBuf->block.numOfElem, num);

H
Haojun Liao 已提交
49
  tsBufDestroy(pTSBuf);
H
hjxilinx 已提交
50 51 52 53
}

// one large list of ts, the ts list need to be split into several small blocks
void largeTSTest() {
H
Haojun Liao 已提交
54
  STSBuf* pTSBuf = tsBufCreate(true, TSDB_ORDER_ASC);
H
hjxilinx 已提交
55 56 57 58 59 60 61 62 63 64 65 66

  // write 10 ts points
  int32_t num = 1000000;
  int64_t tag = 1;

  int64_t* list = createTsList(num, 10000000, 30);
  tsBufAppend(pTSBuf, 0, tag, (const char*)list, num * sizeof(int64_t));

  // the data has been flush to disk, no data in cache
  EXPECT_EQ(pTSBuf->tsData.len, 0);
  EXPECT_EQ(pTSBuf->block.tag, tag);
  EXPECT_EQ(pTSBuf->numOfVnodes, 1);
H
hjxilinx 已提交
67
  EXPECT_EQ(pTSBuf->tsOrder, TSDB_ORDER_ASC);
H
hjxilinx 已提交
68 69 70 71 72

  tsBufFlush(pTSBuf);
  EXPECT_EQ(pTSBuf->tsData.len, 0);
  EXPECT_EQ(pTSBuf->block.numOfElem, num);

H
Haojun Liao 已提交
73
  tsBufDestroy(pTSBuf);
H
hjxilinx 已提交
74 75 76
}

void multiTagsTest() {
H
Haojun Liao 已提交
77
  STSBuf* pTSBuf = tsBufCreate(true, TSDB_ORDER_ASC);
H
hjxilinx 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

  int32_t num = 10000;
  int64_t tag = 1;
  int64_t start = 10000000;
  int32_t numOfTags = 50;
  int32_t step = 30;

  for (int32_t i = 0; i < numOfTags; ++i) {
    int64_t* list = createTsList(num, start, step);
    tsBufAppend(pTSBuf, 0, i, (const char*)list, num * sizeof(int64_t));
    free(list);

    start += step * num;
  }

H
hjxilinx 已提交
93
  EXPECT_EQ(pTSBuf->tsOrder, TSDB_ORDER_ASC);
H
hjxilinx 已提交
94 95 96 97 98 99 100 101 102
  EXPECT_EQ(pTSBuf->tsData.len, num * sizeof(int64_t));

  EXPECT_EQ(pTSBuf->block.tag, numOfTags - 1);
  EXPECT_EQ(pTSBuf->numOfVnodes, 1);

  tsBufFlush(pTSBuf);
  EXPECT_EQ(pTSBuf->tsData.len, 0);
  EXPECT_EQ(pTSBuf->block.numOfElem, num);

H
Haojun Liao 已提交
103
  tsBufDestroy(pTSBuf);
H
hjxilinx 已提交
104 105 106
}

void multiVnodeTagsTest() {
H
Haojun Liao 已提交
107
  STSBuf* pTSBuf = tsBufCreate(true, TSDB_ORDER_ASC);
H
hjxilinx 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

  int32_t num = 10000;
  int64_t start = 10000000;
  int32_t numOfTags = 50;
  int32_t step = 30;

  // 2000 vnodes
  for (int32_t j = 0; j < 20; ++j) {
    // vnodeId:0
    start = 10000000;
    for (int32_t i = 0; i < numOfTags; ++i) {
      int64_t* list = createTsList(num, start, step);
      tsBufAppend(pTSBuf, j, i, (const char*)list, num * sizeof(int64_t));
      free(list);

      start += step * num;
    }

    EXPECT_EQ(pTSBuf->numOfVnodes, j + 1);
  }

H
hjxilinx 已提交
129
  EXPECT_EQ(pTSBuf->tsOrder, TSDB_ORDER_ASC);
H
hjxilinx 已提交
130 131 132 133 134 135 136 137 138 139 140
  EXPECT_EQ(pTSBuf->tsData.len, num * sizeof(int64_t));
  EXPECT_EQ(pTSBuf->block.tag, numOfTags - 1);

  EXPECT_EQ(pTSBuf->tsData.len, num * sizeof(int64_t));

  EXPECT_EQ(pTSBuf->block.tag, numOfTags - 1);

  tsBufFlush(pTSBuf);
  EXPECT_EQ(pTSBuf->tsData.len, 0);
  EXPECT_EQ(pTSBuf->block.numOfElem, num);

H
Haojun Liao 已提交
141
  tsBufDestroy(pTSBuf);
H
hjxilinx 已提交
142 143 144
}

void loadDataTest() {
H
Haojun Liao 已提交
145
  STSBuf* pTSBuf = tsBufCreate(true, TSDB_ORDER_ASC);
H
hjxilinx 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159

  int32_t num = 10000;
  int64_t oldStart = 10000000;
  int32_t numOfTags = 50;
  int32_t step = 30;
  int32_t numOfVnode = 200;

  // 10000 vnodes
  for (int32_t j = 0; j < numOfVnode; ++j) {
    // vnodeId:0
    int64_t start = 10000000;
    for (int32_t i = 0; i < numOfTags; ++i) {
      int64_t* list = createTsList(num, start, step);
      tsBufAppend(pTSBuf, j, i, (const char*)list, num * sizeof(int64_t));
H
hjxilinx 已提交
160
      printf("%d - %" PRIu64 "\n", i, list[0]);
H
hjxilinx 已提交
161 162 163 164 165 166 167 168

      free(list);
      start += step * num;
    }

    EXPECT_EQ(pTSBuf->numOfVnodes, j + 1);
  }

H
hjxilinx 已提交
169
  EXPECT_EQ(pTSBuf->tsOrder, TSDB_ORDER_ASC);
H
hjxilinx 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

  EXPECT_EQ(pTSBuf->tsData.len, num * sizeof(int64_t));
  EXPECT_EQ(pTSBuf->block.tag, numOfTags - 1);

  EXPECT_EQ(pTSBuf->tsData.len, num * sizeof(int64_t));

  EXPECT_EQ(pTSBuf->block.tag, numOfTags - 1);

  tsBufFlush(pTSBuf);
  EXPECT_EQ(pTSBuf->tsData.len, 0);
  EXPECT_EQ(pTSBuf->block.numOfElem, num);

  // create from exists file
  STSBuf* pNewBuf = tsBufCreateFromFile(pTSBuf->path, false);
  EXPECT_EQ(pNewBuf->tsOrder, pTSBuf->tsOrder);
  EXPECT_EQ(pNewBuf->numOfVnodes, numOfVnode);
  EXPECT_EQ(pNewBuf->fileSize, pTSBuf->fileSize);

  EXPECT_EQ(pNewBuf->pData[0].info.offset, pTSBuf->pData[0].info.offset);
  EXPECT_EQ(pNewBuf->pData[0].info.numOfBlocks, pTSBuf->pData[0].info.numOfBlocks);
  EXPECT_EQ(pNewBuf->pData[0].info.compLen, pTSBuf->pData[0].info.compLen);

  EXPECT_STREQ(pNewBuf->path, pTSBuf->path);

  tsBufResetPos(pNewBuf);

  int64_t s = taosGetTimestampUs();
H
hjxilinx 已提交
197
  printf("start:%" PRIu64 "\n", s);
H
hjxilinx 已提交
198 199 200 201 202 203 204 205

  int32_t x = 0;
  while (tsBufNextPos(pNewBuf)) {
    STSElem elem = tsBufGetElem(pNewBuf);
    if (++x == 100000000) {
      break;
    }

H
hjxilinx 已提交
206
    //    printf("%d-%" PRIu64 "-%" PRIu64 "\n", elem.vnode, elem.tag, elem.ts);
H
hjxilinx 已提交
207 208 209
  }

  int64_t e = taosGetTimestampUs();
H
hjxilinx 已提交
210
  printf("end:%" PRIu64 ", elapsed:%" PRIu64 ", total obj:%d\n", e, e - s, x);
H
hjxilinx 已提交
211 212 213 214 215 216 217 218 219 220 221 222
}

void randomIncTsTest() {}

void TSTraverse() {
  // 10000 vnodes
  int32_t num = 200000;
  int64_t oldStart = 10000000;
  int32_t numOfTags = 3;
  int32_t step = 30;
  int32_t numOfVnode = 2;

H
Haojun Liao 已提交
223
  STSBuf* pTSBuf = tsBufCreate(true, TSDB_ORDER_ASC);
H
hjxilinx 已提交
224 225 226 227 228 229 230

  for (int32_t j = 0; j < numOfVnode; ++j) {
    // vnodeId:0
    int64_t start = 10000000;
    for (int32_t i = 0; i < numOfTags; ++i) {
      int64_t* list = createTsList(num, start, step);
      tsBufAppend(pTSBuf, j, i, (const char*)list, num * sizeof(int64_t));
H
hjxilinx 已提交
231
      printf("%d - %d - %" PRIu64 ", %" PRIu64 "\n", j, i, list[0], list[num - 1]);
H
hjxilinx 已提交
232 233 234 235 236 237

      free(list);
      start += step * num;

      list = createTsList(num, start, step);
      tsBufAppend(pTSBuf, j, i, (const char*)list, num * sizeof(int64_t));
H
hjxilinx 已提交
238
      printf("%d - %d - %" PRIu64 ", %" PRIu64 "\n", j, i, list[0], list[num - 1]);
H
hjxilinx 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251
      free(list);

      start += step * num;
    }

    EXPECT_EQ(pTSBuf->numOfVnodes, j + 1);
  }

  tsBufResetPos(pTSBuf);

  ////////////////////////////////////////////////////////////////////////////////////////
  // reverse traverse
  int64_t s = taosGetTimestampUs();
H
hjxilinx 已提交
252
  printf("start:%" PRIu64 "\n", s);
H
hjxilinx 已提交
253

H
hjxilinx 已提交
254
  pTSBuf->cur.order = TSDB_ORDER_DESC;
H
hjxilinx 已提交
255 256 257 258 259

  // complete reverse traverse
  int32_t x = 0;
  while (tsBufNextPos(pTSBuf)) {
    STSElem elem = tsBufGetElem(pTSBuf);
H
hjxilinx 已提交
260
    //    printf("%d-%" PRIu64 "-%" PRIu64 "\n", elem.vnode, elem.tag, elem.ts);
H
hjxilinx 已提交
261 262 263 264
  }

  // specify the data block with vnode and tags value
  tsBufResetPos(pTSBuf);
H
hjxilinx 已提交
265
  pTSBuf->cur.order = TSDB_ORDER_DESC;
H
hjxilinx 已提交
266 267 268 269 270 271 272 273 274

  int32_t startVnode = 1;
  int32_t startTag = 2;

  tsBufGetElemStartPos(pTSBuf, startVnode, startTag);

  int32_t totalOutput = 10;
  while (1) {
    STSElem elem = tsBufGetElem(pTSBuf);
H
hjxilinx 已提交
275
    printf("%d-%" PRIu64 "-%" PRIu64 "\n", elem.vnode, elem.tag, elem.ts);
H
hjxilinx 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298

    if (!tsBufNextPos(pTSBuf)) {
      break;
    }

    if (--totalOutput <= 0) {
      totalOutput = 10;

      tsBufGetElemStartPos(pTSBuf, startVnode, --startTag);

      if (startTag == 0) {
        startVnode -= 1;
        startTag = 3;
      }

      if (startVnode < 0) {
        break;
      }
    }
  }

  /////////////////////////////////////////////////////////////////////////////////
  // traverse
H
hjxilinx 已提交
299
  pTSBuf->cur.order = TSDB_ORDER_ASC;
H
hjxilinx 已提交
300 301 302 303 304
  tsBufResetPos(pTSBuf);

  // complete forwards traverse
  while (tsBufNextPos(pTSBuf)) {
    STSElem elem = tsBufGetElem(pTSBuf);
H
hjxilinx 已提交
305
    //    printf("%d-%" PRIu64 "-%" PRIu64 "\n", elem.vnode, elem.tag, elem.ts);
H
hjxilinx 已提交
306 307 308 309
  }

  // specify the data block with vnode and tags value
  tsBufResetPos(pTSBuf);
H
hjxilinx 已提交
310
  pTSBuf->cur.order = TSDB_ORDER_ASC;
H
hjxilinx 已提交
311 312 313 314 315 316 317 318 319

  startVnode = 1;
  startTag = 2;

  tsBufGetElemStartPos(pTSBuf, startVnode, startTag);

  totalOutput = 10;
  while (1) {
    STSElem elem = tsBufGetElem(pTSBuf);
H
hjxilinx 已提交
320
    printf("%d-%" PRIu64 "-%" PRIu64 "\n", elem.vnode, elem.tag, elem.ts);
H
hjxilinx 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360

    if (!tsBufNextPos(pTSBuf)) {
      break;
    }

    if (--totalOutput <= 0) {
      totalOutput = 10;

      tsBufGetElemStartPos(pTSBuf, startVnode, --startTag);

      if (startTag < 0) {
        startVnode -= 1;
        startTag = 3;
      }

      if (startVnode < 0) {
        break;
      }
    }
  }
}

void performanceTest() {}

void emptyTagTest() {}

void invalidFileTest() {
  const char* cmd = "touch /tmp/test";

  // create empty file
  system(cmd);

  STSBuf* pNewBuf = tsBufCreateFromFile("/tmp/test", true);
  EXPECT_TRUE(pNewBuf == NULL);

  pNewBuf = tsBufCreateFromFile("/tmp/911", true);
  EXPECT_TRUE(pNewBuf == NULL);
}

void mergeDiffVnodeBufferTest() {
H
Haojun Liao 已提交
361 362
  STSBuf* pTSBuf1 = tsBufCreate(true, TSDB_ORDER_ASC);
  STSBuf* pTSBuf2 = tsBufCreate(true, TSDB_ORDER_ASC);
H
hjxilinx 已提交
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387

  int32_t step = 30;
  int32_t num = 1000;
  int32_t numOfTags = 10;

  // vnodeId:0
  int64_t start = 10000000;
  for (int32_t i = 0; i < numOfTags; ++i) {
    int64_t* list = createTsList(num, start, step);
    tsBufAppend(pTSBuf1, 0, i, (const char*)list, num * sizeof(int64_t));
    tsBufAppend(pTSBuf2, 0, i, (const char*)list, num * sizeof(int64_t));

    free(list);

    start += step * num;
  }

  tsBufFlush(pTSBuf2);

  tsBufMerge(pTSBuf1, pTSBuf2, 9);
  EXPECT_EQ(pTSBuf1->numOfVnodes, 2);
  EXPECT_EQ(pTSBuf1->numOfTotal, numOfTags * 2 * num);

  tsBufDisplay(pTSBuf1);

H
Haojun Liao 已提交
388 389
  tsBufDestroy(pTSBuf2);
  tsBufDestroy(pTSBuf1);
H
hjxilinx 已提交
390 391 392
}

void mergeIdenticalVnodeBufferTest() {
H
Haojun Liao 已提交
393 394
  STSBuf* pTSBuf1 = tsBufCreate(true, TSDB_ORDER_ASC);
  STSBuf* pTSBuf2 = tsBufCreate(true, TSDB_ORDER_ASC);
H
hjxilinx 已提交
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430

  int32_t step = 30;
  int32_t num = 1000;
  int32_t numOfTags = 10;

  // vnodeId:0
  int64_t start = 10000000;
  for (int32_t i = 0; i < numOfTags; ++i) {
    int64_t* list = createTsList(num, start, step);

    tsBufAppend(pTSBuf1, 12, i, (const char*)list, num * sizeof(int64_t));
    free(list);

    start += step * num;
  }

  for (int32_t i = numOfTags; i < numOfTags * 2; ++i) {
    int64_t* list = createTsList(num, start, step);

    tsBufAppend(pTSBuf2, 77, i, (const char*)list, num * sizeof(int64_t));
    free(list);

    start += step * num;
  }

  tsBufFlush(pTSBuf2);

  tsBufMerge(pTSBuf1, pTSBuf2, 12);
  EXPECT_EQ(pTSBuf1->numOfVnodes, 1);
  EXPECT_EQ(pTSBuf1->numOfTotal, numOfTags * 2 * num);

  tsBufResetPos(pTSBuf1);
  while (tsBufNextPos(pTSBuf1)) {
    STSElem elem = tsBufGetElem(pTSBuf1);
    EXPECT_EQ(elem.vnode, 12);

H
hjxilinx 已提交
431
    printf("%d-%" PRIu64 "-%" PRIu64 "\n", elem.vnode, elem.tag, elem.ts);
H
hjxilinx 已提交
432 433
  }

H
Haojun Liao 已提交
434 435
  tsBufDestroy(pTSBuf1);
  tsBufDestroy(pTSBuf2);
H
hjxilinx 已提交
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
}
}  // namespace

TEST(testCase, tsBufTest) {
  simpleTest();
  largeTSTest();
  multiTagsTest();
  multiVnodeTagsTest();
  loadDataTest();
  invalidFileTest();
  //  randomIncTsTest();
  TSTraverse();
  mergeDiffVnodeBufferTest();
  mergeIdenticalVnodeBufferTest();
}