tsBufTest.cpp 11.0 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
Haojun Liao 已提交
50 51

  free(list);
H
hjxilinx 已提交
52 53 54 55
}

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

  // 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 已提交
69
  EXPECT_EQ(pTSBuf->tsOrder, TSDB_ORDER_ASC);
H
hjxilinx 已提交
70 71 72 73 74

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

H
Haojun Liao 已提交
75
  tsBufDestroy(pTSBuf);
H
Haojun Liao 已提交
76
  free(list);
H
hjxilinx 已提交
77 78 79
}

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

  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 已提交
96
  EXPECT_EQ(pTSBuf->tsOrder, TSDB_ORDER_ASC);
H
hjxilinx 已提交
97 98 99 100 101 102 103 104 105
  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 已提交
106
  tsBufDestroy(pTSBuf);
H
hjxilinx 已提交
107 108 109
}

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

  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 已提交
132
  EXPECT_EQ(pTSBuf->tsOrder, TSDB_ORDER_ASC);
H
hjxilinx 已提交
133 134 135 136 137 138 139 140 141 142 143
  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 已提交
144
  tsBufDestroy(pTSBuf);
H
hjxilinx 已提交
145 146 147
}

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

  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 已提交
163
      printf("%d - %" PRIu64 "\n", i, list[0]);
H
hjxilinx 已提交
164 165 166 167 168 169 170 171

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

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

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

  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 已提交
200
  printf("start:%" PRIu64 "\n", s);
H
hjxilinx 已提交
201 202 203 204 205 206 207 208

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

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

  int64_t e = taosGetTimestampUs();
H
hjxilinx 已提交
213
  printf("end:%" PRIu64 ", elapsed:%" PRIu64 ", total obj:%d\n", e, e - s, x);
H
Haojun Liao 已提交
214 215
  tsBufDestroy(pTSBuf);
  tsBufDestroy(pNewBuf);
H
hjxilinx 已提交
216 217 218 219 220 221 222 223 224 225 226 227
}

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 已提交
228
  STSBuf* pTSBuf = tsBufCreate(true, TSDB_ORDER_ASC);
H
hjxilinx 已提交
229 230 231 232 233 234 235

  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 已提交
236
      printf("%d - %d - %" PRIu64 ", %" PRIu64 "\n", j, i, list[0], list[num - 1]);
H
hjxilinx 已提交
237 238 239 240 241 242

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

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

      start += step * num;
    }

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

  tsBufResetPos(pTSBuf);

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

H
hjxilinx 已提交
259
  pTSBuf->cur.order = TSDB_ORDER_DESC;
H
hjxilinx 已提交
260 261 262 263 264

  // complete reverse traverse
  int32_t x = 0;
  while (tsBufNextPos(pTSBuf)) {
    STSElem elem = tsBufGetElem(pTSBuf);
H
hjxilinx 已提交
265
    //    printf("%d-%" PRIu64 "-%" PRIu64 "\n", elem.vnode, elem.tag, elem.ts);
H
hjxilinx 已提交
266 267 268 269
  }

  // specify the data block with vnode and tags value
  tsBufResetPos(pTSBuf);
H
hjxilinx 已提交
270
  pTSBuf->cur.order = TSDB_ORDER_DESC;
H
hjxilinx 已提交
271 272 273 274 275 276 277 278 279

  int32_t startVnode = 1;
  int32_t startTag = 2;

  tsBufGetElemStartPos(pTSBuf, startVnode, startTag);

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

    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 已提交
304
  pTSBuf->cur.order = TSDB_ORDER_ASC;
H
hjxilinx 已提交
305 306 307 308 309
  tsBufResetPos(pTSBuf);

  // complete forwards traverse
  while (tsBufNextPos(pTSBuf)) {
    STSElem elem = tsBufGetElem(pTSBuf);
H
hjxilinx 已提交
310
    //    printf("%d-%" PRIu64 "-%" PRIu64 "\n", elem.vnode, elem.tag, elem.ts);
H
hjxilinx 已提交
311 312 313 314
  }

  // specify the data block with vnode and tags value
  tsBufResetPos(pTSBuf);
H
hjxilinx 已提交
315
  pTSBuf->cur.order = TSDB_ORDER_ASC;
H
hjxilinx 已提交
316 317 318 319 320 321 322 323 324

  startVnode = 1;
  startTag = 2;

  tsBufGetElemStartPos(pTSBuf, startVnode, startTag);

  totalOutput = 10;
  while (1) {
    STSElem elem = tsBufGetElem(pTSBuf);
H
hjxilinx 已提交
325
    printf("%d-%" PRIu64 "-%" PRIu64 "\n", elem.vnode, elem.tag, elem.ts);
H
hjxilinx 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345

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

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

      tsBufGetElemStartPos(pTSBuf, startVnode, --startTag);

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

      if (startVnode < 0) {
        break;
      }
    }
  }
H
Haojun Liao 已提交
346 347

  tsBufDestroy(pTSBuf);
H
hjxilinx 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361
}

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);
H
Haojun Liao 已提交
362
  tsBufDestroy(pNewBuf);
H
hjxilinx 已提交
363 364 365

  pNewBuf = tsBufCreateFromFile("/tmp/911", true);
  EXPECT_TRUE(pNewBuf == NULL);
H
Haojun Liao 已提交
366 367

  tsBufDestroy(pNewBuf);
H
hjxilinx 已提交
368 369 370
}

void mergeDiffVnodeBufferTest() {
H
Haojun Liao 已提交
371 372
  STSBuf* pTSBuf1 = tsBufCreate(true, TSDB_ORDER_ASC);
  STSBuf* pTSBuf2 = tsBufCreate(true, TSDB_ORDER_ASC);
H
hjxilinx 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397

  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 已提交
398 399
  tsBufDestroy(pTSBuf2);
  tsBufDestroy(pTSBuf1);
H
hjxilinx 已提交
400 401 402
}

void mergeIdenticalVnodeBufferTest() {
H
Haojun Liao 已提交
403 404
  STSBuf* pTSBuf1 = tsBufCreate(true, TSDB_ORDER_ASC);
  STSBuf* pTSBuf2 = tsBufCreate(true, TSDB_ORDER_ASC);
H
hjxilinx 已提交
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 431 432 433 434 435 436 437 438 439 440

  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 已提交
441
    printf("%d-%" PRIu64 "-%" PRIu64 "\n", elem.vnode, elem.tag, elem.ts);
H
hjxilinx 已提交
442 443
  }

H
Haojun Liao 已提交
444 445
  tsBufDestroy(pTSBuf1);
  tsBufDestroy(pTSBuf2);
H
hjxilinx 已提交
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
}
}  // namespace

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