skiplistTest.cpp 10.0 KB
Newer Older
H
hjxilinx 已提交
1 2 3
#include <gtest/gtest.h>
#include <limits.h>
#include <taosdef.h>
4
#include <tcompare.h>
H
hjxilinx 已提交
5 6
#include <iostream>

S
Shengliang Guan 已提交
7
#include "os.h"
H
Hongze Cheng 已提交
8
#include "tmsg.h"
H
hjxilinx 已提交
9 10 11
#include "tskiplist.h"
#include "tutil.h"

12
#if 0
H
hjxilinx 已提交
13 14 15 16 17
namespace {

char* getkey(const void* data) { return (char*)(data); }

void doubleSkipListTest() {
18 19
  SSkipList* pSkipList = tSkipListCreate(10, TSDB_DATA_TYPE_DOUBLE, sizeof(double),
                                         getKeyComparFunc(TSDB_DATA_TYPE_DOUBLE), false, getkey);
H
hjxilinx 已提交
20 21 22 23 24 25 26 27 28 29 30

  double  doubleVal[1000] = {0};
  int32_t size = 20000;

  printf("generated %d keys is: \n", size);

  for (int32_t i = 0; i < size; ++i) {
    if (i < 1000) {
      doubleVal[i] = i * 0.997;
    }

31 32
//    int32_t level = 0;
    size = 0;
H
hjxilinx 已提交
33

34 35 36
    //    tSkipListNewNodeInfo(pSkipList, &level, &size);
    //    auto d = (SSkipListNode*)calloc(1, size + sizeof(double) * 2);
    //    d->level = level;
H
hjxilinx 已提交
37

38 39
    double key = 0.997;
    tSkipListPut(pSkipList, &key);
H
hjxilinx 已提交
40 41 42 43 44 45 46 47 48 49
  }

  printf("the first level of skip list is:\n");
  tSkipListPrint(pSkipList, 1);

#if 0
  SSkipListNode **pNodes = NULL;
  SSkipListKey    sk;
  for (int32_t i = 0; i < 100; ++i) {
    sk.nType = TSDB_DATA_TYPE_DOUBLE;
wafwerar's avatar
wafwerar 已提交
50
    int32_t idx = abs((i * taosRand()) % 1000);
H
hjxilinx 已提交
51 52 53 54 55 56 57 58 59 60 61

    sk.dKey = doubleVal[idx];

    int32_t size = tSkipListGets(pSkipList, &sk, &pNodes);

    printf("the query result size is: %d\n", size);
    for (int32_t j = 0; j < size; ++j) {
      printf("the result is: %lf\n", pNodes[j]->key.dKey);
    }

    if (size > 0) {
S
TD-1848  
Shengliang Guan 已提交
62
      tfree(pNodes);
H
hjxilinx 已提交
63 64 65 66 67 68 69 70 71
    }
  }

#endif

  printf("double test end...\n");
  tSkipListDestroy(pSkipList);
}

72
void randKeyTest() {
H
Haojun Liao 已提交
73
  SSkipList* pSkipList = tSkipListCreate(10, TSDB_DATA_TYPE_INT, sizeof(int32_t), getKeyComparFunc(TSDB_DATA_TYPE_INT, TSDB_ORDER_ASC),
74
      false, getkey);
75 76

  int32_t size = 200000;
wafwerar's avatar
wafwerar 已提交
77
  taosSeedRand(time(NULL));
78 79 80 81 82 83 84

  printf("generated %d keys is: \n", size);

  for (int32_t i = 0; i < size; ++i) {
    int32_t level = 0;
    int32_t s = 0;

85
    tSkipListNewNodeInfo(pSkipList, &level, &s);
86 87 88 89
    auto d = (SSkipListNode*)calloc(1, s + sizeof(int32_t) * 2);
    d->level = level;

    int32_t* key = (int32_t*)SL_GET_NODE_KEY(pSkipList, d);
wafwerar's avatar
wafwerar 已提交
90
    key[0] = taosRand() % 1000000000;
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

    key[1] = key[0];

    tSkipListPut(pSkipList, d);
  }

  printf("the first level of skip list is:\n");
  tSkipListPrint(pSkipList, 1);

  printf("the sec level of skip list is:\n");
  tSkipListPrint(pSkipList, 2);

  printf("the 5 level of skip list is:\n");
  tSkipListPrint(pSkipList, 5);

  tSkipListDestroy(pSkipList);
}
108

H
hjxilinx 已提交
109 110 111 112 113 114 115
void stringKeySkiplistTest() {
  const int32_t max_key_size = 12;

  SSkipList* pSkipList = tSkipListCreate(10, TSDB_DATA_TYPE_BINARY, max_key_size, 0, false, true, getkey);

  int32_t level = 0;
  int32_t headsize = 0;
116
  tSkipListNewNodeInfo(pSkipList, &level, &headsize);
H
hjxilinx 已提交
117 118 119 120 121 122 123 124 125 126 127

  auto pNode = (SSkipListNode*)calloc(1, headsize + max_key_size + sizeof(double));
  pNode->level = level;

  char* d = SL_GET_NODE_DATA(pNode);
  strncpy(d, "nyse", 5);

  *(double*)(d + max_key_size) = 12;

  tSkipListPut(pSkipList, pNode);

128
  tSkipListNewNodeInfo(pSkipList, &level, &headsize);
H
hjxilinx 已提交
129 130 131 132 133 134 135 136 137 138 139

  pNode = (SSkipListNode*)calloc(1, headsize + max_key_size + sizeof(double));
  pNode->level = level;

  d = SL_GET_NODE_DATA(pNode);
  strncpy(d, "beijing", 8);

  *(double*)(d + max_key_size) = 911;

  tSkipListPut(pSkipList, pNode);

140 141 142
  printf("level one------------------\n");
  tSkipListPrint(pSkipList, 1);

H
hjxilinx 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
#if 0
  SSkipListNode **pRes = NULL;
  int32_t ret = tSkipListGets(pSkipList, &key1, &pRes);

  assert(ret == 1);
  assert(strcmp(pRes[0]->key.pz, "beijing") == 0);
  assert(pRes[0]->key.nType == TSDB_DATA_TYPE_BINARY);

  tSkipListDestroyKey(&key1);
  tSkipListDestroyKey(&key);

  tSkipListDestroy(pSkipList);

  free(pRes);
#endif

  tSkipListDestroy(pSkipList);

  int64_t s = taosGetTimestampUs();
  pSkipList = tSkipListCreate(10, TSDB_DATA_TYPE_BINARY, 20, 0, false, true, getkey);
  char k[256] = {0};

  int32_t total = 10000;
  for (int32_t i = 0; i < total; ++i) {
    int32_t n = sprintf(k, "abc_%d_%d", i, i);
168
    tSkipListNewNodeInfo(pSkipList, &level, &headsize);
H
hjxilinx 已提交
169 170 171 172 173 174 175 176 177 178 179

    auto pNode = (SSkipListNode*)calloc(1, headsize + 20 + sizeof(double));
    pNode->level = level;

    char* d = SL_GET_NODE_DATA(pNode);
    strncpy(d, k, strlen(k));

    tSkipListPut(pSkipList, pNode);
  }

  int64_t e = taosGetTimestampUs();
H
hjxilinx 已提交
180
  printf("elapsed time:%" PRIu64 " us to insert %d data, avg:%f us\n", (e - s), total, (double)(e - s) / total);
H
hjxilinx 已提交
181

182 183 184
  printf("level two------------------\n");
  tSkipListPrint(pSkipList, 1);

H
hjxilinx 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
#if 0
  SSkipListNode **pres = NULL;

  s = taosGetTimestampMs();
  for (int32_t j = 0; j < total; ++j) {
    int32_t n = sprintf(k, "abc_%d_%d", j, j);
    key = tSkipListCreateKey(TSDB_DATA_TYPE_BINARY, k, n);

    int32_t num = tSkipListGets(pSkipList, &key, &pres);
    assert(num > 0);

    //    tSkipListRemove(pSkipList, &key);
    tSkipListRemoveNode(pSkipList, pres[0]);

    if (num > 0) {
S
TD-1848  
Shengliang Guan 已提交
200
      tfree(pres);
H
hjxilinx 已提交
201 202 203 204 205 206 207 208 209 210 211 212
    }
  }

  e = taosGetTimestampMs();
  printf("elapsed time:%lldms\n", e - s);
#endif
  tSkipListDestroy(pSkipList);
}

void skiplistPerformanceTest() {
  SSkipList* pSkipList = tSkipListCreate(10, TSDB_DATA_TYPE_DOUBLE, sizeof(double), 0, false, false, getkey);

213
  int32_t size = 1000000;
H
hjxilinx 已提交
214 215 216 217 218 219 220 221 222 223 224
  int64_t prev = taosGetTimestampMs();
  int64_t s = prev;

  int32_t level = 0;
  int32_t headsize = 0;

  int32_t unit = MAX_SKIP_LIST_LEVEL * POINTER_BYTES * 2 + sizeof(double) * 2 + sizeof(int16_t);

  char* total = (char*)calloc(1, unit * size);
  char* p = total;

225
  for (int32_t i = 0; i < size; ++i) {
226
    tSkipListNewNodeInfo(pSkipList, &level, &headsize);
H
hjxilinx 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241

    SSkipListNode* d = (SSkipListNode*)p;
    p += headsize + sizeof(double) * 2;

    d->level = level;
    double* v = (double*)SL_GET_NODE_DATA(d);
    v[0] = i * 0.997;
    v[1] = i * 0.997;

    tSkipListPut(pSkipList, d);

    if (i % 100000 == 0) {
      int64_t cur = taosGetTimestampMs();

      int64_t elapsed = cur - prev;
H
hjxilinx 已提交
242
      printf("add %d, elapsed time: %" PRIu64 " ms, avg elapsed:%f ms, total:%d\n", 100000, elapsed, elapsed / 100000.0, i);
H
hjxilinx 已提交
243 244 245 246 247
      prev = cur;
    }
  }

  int64_t e = taosGetTimestampMs();
H
hjxilinx 已提交
248
  printf("total:%" PRIu64 " ms, avg:%f\n", e - s, (e - s) / (double)size);
H
hjxilinx 已提交
249 250
  printf("max level of skiplist:%d, actually level:%d\n ", pSkipList->maxLevel, pSkipList->level);

H
TD-1437  
Hongze Cheng 已提交
251
  assert(SL_GET_SIZE(pSkipList) == size);
H
hjxilinx 已提交
252

253 254 255 256 257 258 259 260 261 262 263 264 265
  //  printf("the level of skiplist is:\n");
  //
  //  printf("level two------------------\n");
  //  tSkipListPrint(pSkipList, 2);
  //
  //  printf("level three------------------\n");
  //  tSkipListPrint(pSkipList, 3);
  //
  //  printf("level four------------------\n");
  //  tSkipListPrint(pSkipList, 4);
  //
  //  printf("level nine------------------\n");
  //  tSkipListPrint(pSkipList, 10);
H
hjxilinx 已提交
266 267 268 269 270 271 272 273 274 275 276

  int64_t st = taosGetTimestampMs();
#if 0
  for (int32_t i = 0; i < 100000; i += 1) {
    key.dKey = i * 0.997;
    tSkipListRemove(pSkipList, &key);
  }
#endif

  int64_t et = taosGetTimestampMs();
  printf("delete %d data from skiplist, elapased time:%" PRIu64 "ms\n", 10000, et - st);
H
TD-1437  
Hongze Cheng 已提交
277
  assert(SL_GET_SIZE(pSkipList) == size);
H
hjxilinx 已提交
278 279

  tSkipListDestroy(pSkipList);
S
TD-1848  
Shengliang Guan 已提交
280
  tfree(total);
H
hjxilinx 已提交
281 282 283 284
}

// todo not support duplicated key yet
void duplicatedKeyTest() {
B
Bomin Zhang 已提交
285
  SSkipList *pSkipList = tSkipListCreate(MAX_SKIP_LIST_LEVEL, TSDB_DATA_TYPE_INT, sizeof(int), true, false, true, getkey);
H
hjxilinx 已提交
286

B
Bomin Zhang 已提交
287
  for (int32_t i = 0; i < 200; ++i) {
H
hjxilinx 已提交
288
    for (int32_t j = 0; j < 5; ++j) {
B
Bomin Zhang 已提交
289 290 291 292 293 294 295
      int32_t level, size;
      tSkipListNewNodeInfo(pSkipList, &level, &size);
      SSkipListNode* d = (SSkipListNode*)calloc(1, size + sizeof(int32_t));
      d->level = level;
      int32_t* key = (int32_t*)SL_GET_NODE_KEY(pSkipList, d);
      key[0] = i;
      tSkipListPut(pSkipList, d);
H
hjxilinx 已提交
296 297 298 299
    }
  }

  for (int32_t i = 0; i < 100; ++i) {
B
Bomin Zhang 已提交
300 301 302 303 304
    SSkipListKey key;
    SArray*  nodes = tSkipListGet(pSkipList, (char*)(&i));
    assert( taosArrayGetSize(nodes) == 5 );
    taosArrayDestroy(nodes);
  }
H
hjxilinx 已提交
305

B
Bomin Zhang 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
  int32_t key = 101;
  uint32_t num = tSkipListRemove(pSkipList, (char*)(&key));
  assert(num == 5);

  SArray*  nodes = tSkipListGet(pSkipList, (char*)(&key));
  assert( taosArrayGetSize(nodes) == 0 );
  taosArrayDestroy(nodes);

  key = 102;
  SSkipListIterator* iter = tSkipListCreateIterFromVal(pSkipList, (char*)(&key), TSDB_DATA_TYPE_INT, TSDB_ORDER_ASC);
  for(int i = 0; i < 6; i++) {
    assert(tSkipListIterNext(iter) == true);
    SSkipListNode* node = tSkipListIterGet(iter);
    int32_t* val = (int32_t*)SL_GET_NODE_KEY(pSkipList, node);
    assert((i < 5) == ((*val) == key));
H
hjxilinx 已提交
321
  }
B
Bomin Zhang 已提交
322 323 324 325 326 327 328 329 330 331
  tSkipListDestroyIter(iter);

  iter = tSkipListCreateIterFromVal(pSkipList, (char*)(&key), TSDB_DATA_TYPE_INT, TSDB_ORDER_DESC);
  for(int i = 0; i < 6; i++) {
    assert(tSkipListIterNext(iter) == true);
    SSkipListNode* node = tSkipListIterGet(iter);
    int32_t* val = (int32_t*)SL_GET_NODE_KEY(pSkipList, node);
    assert((i < 5) == ((*val) == key));
  }
  tSkipListDestroyIter(iter);
H
hjxilinx 已提交
332 333 334 335 336 337 338 339

  tSkipListDestroy(pSkipList);
}

}  // namespace

TEST(testCase, skiplist_test) {
  assert(sizeof(SSkipListKey) == 8);
wafwerar's avatar
wafwerar 已提交
340
  taosSeedRand(time(NULL));
H
hjxilinx 已提交
341

342 343
  stringKeySkiplistTest();
  doubleSkipListTest();
H
hjxilinx 已提交
344
  skiplistPerformanceTest();
345 346
  duplicatedKeyTest();
  randKeyTest();
H
hjxilinx 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359 360

  //  tSKipListQueryCond q;
  //  q.upperBndRelOptr = true;
  //  q.lowerBndRelOptr = true;
  //  q.upperBnd.nType = TSDB_DATA_TYPE_DOUBLE;
  //  q.lowerBnd.nType = TSDB_DATA_TYPE_DOUBLE;
  //  q.lowerBnd.dKey = 120;
  //  q.upperBnd.dKey = 171.989;
  /*
      int32_t size = tSkipListQuery(pSkipList, &q, &pNodes);
      for (int32_t i = 0; i < size; ++i) {
          printf("-----%lf\n", pNodes[i]->key.dKey);
      }
      printf("the range query result size is: %d\n", size);
S
TD-1848  
Shengliang Guan 已提交
361
      tfree(pNodes);
H
hjxilinx 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374

      SSkipListKey *pKeys = malloc(sizeof(SSkipListKey) * 20);
      for (int32_t i = 0; i < 8; i += 2) {
          pKeys[i].dKey = i * 0.997;
          pKeys[i].nType = TSDB_DATA_TYPE_DOUBLE;
          printf("%lf ", pKeys[i].dKey);
      }

      int32_t r = tSkipListPointQuery(pSkipList, pKeys, 8, EXCLUDE_POINT_QUERY, &pNodes);
      printf("\nthe exclude query result is: %d\n", r);
      for (int32_t i = 0; i < r; ++i) {
  //        printf("%lf ", pNodes[i]->key.dKey);
      }
S
TD-1848  
Shengliang Guan 已提交
375
      tfree(pNodes);
H
hjxilinx 已提交
376 377 378

      free(pKeys);*/
}
379 380

#endif