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

S
Shengliang Guan 已提交
6
#include "os.h"
H
hjxilinx 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include "taosmsg.h"
#include "tskiplist.h"
#include "tutil.h"

namespace {

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

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

  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;
    }

    int32_t level = 0;
    int32_t size = 0;

31
    tSkipListNewNodeInfo(pSkipList, &level, &size);
H
hjxilinx 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    auto d = (SSkipListNode*)calloc(1, size + sizeof(double) * 2);
    d->level = level;

    double* key = (double*)SL_GET_NODE_KEY(pSkipList, d);
    key[0] = i * 0.997;
    key[1] = i * 0.997;

    tSkipListPut(pSkipList, d);
  }

  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;
    int32_t idx = abs((i * rand()) % 1000);

    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
Shengliang Guan 已提交
62
      taosTFree(pNodes);
H
hjxilinx 已提交
63 64 65 66 67 68 69 70 71
    }
  }

#endif

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

72 73 74 75 76 77 78 79 80 81 82 83
void randKeyTest() {
  SSkipList* pSkipList = tSkipListCreate(10, TSDB_DATA_TYPE_INT, sizeof(int32_t), 0, false, true, getkey);

  int32_t size = 200000;
  srand(time(NULL));

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

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

84
    tSkipListNewNodeInfo(pSkipList, &level, &s);
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    auto d = (SSkipListNode*)calloc(1, s + sizeof(int32_t) * 2);
    d->level = level;

    int32_t* key = (int32_t*)SL_GET_NODE_KEY(pSkipList, d);
    key[0] = rand() % 1000000000;

    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);
}
107

H
hjxilinx 已提交
108 109 110 111 112 113 114
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;
115
  tSkipListNewNodeInfo(pSkipList, &level, &headsize);
H
hjxilinx 已提交
116 117 118 119 120 121 122 123 124 125 126

  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);

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

  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);

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

H
hjxilinx 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
#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);
167
    tSkipListNewNodeInfo(pSkipList, &level, &headsize);
H
hjxilinx 已提交
168 169 170 171 172 173 174 175 176 177 178

    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 已提交
179
  printf("elapsed time:%" PRIu64 " us to insert %d data, avg:%f us\n", (e - s), total, (double)(e - s) / total);
H
hjxilinx 已提交
180

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

H
hjxilinx 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
#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
Shengliang Guan 已提交
199
      taosTFree(pres);
H
hjxilinx 已提交
200 201 202 203 204 205 206 207 208 209 210 211
    }
  }

  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);

212
  int32_t size = 1000000;
H
hjxilinx 已提交
213 214 215 216 217 218 219 220 221 222 223
  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;

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

    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 已提交
241
      printf("add %d, elapsed time: %" PRIu64 " ms, avg elapsed:%f ms, total:%d\n", 100000, elapsed, elapsed / 100000.0, i);
H
hjxilinx 已提交
242 243 244 245 246
      prev = cur;
    }
  }

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

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

252 253 254 255 256 257 258 259 260 261 262 263 264
  //  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 已提交
265 266 267 268 269 270 271 272 273 274 275

  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 已提交
276
  assert(SL_GET_SIZE(pSkipList) == size);
H
hjxilinx 已提交
277 278

  tSkipListDestroy(pSkipList);
S
Shengliang Guan 已提交
279
  taosTFree(total);
H
hjxilinx 已提交
280 281 282 283
}

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

B
Bomin Zhang 已提交
286
  for (int32_t i = 0; i < 200; ++i) {
H
hjxilinx 已提交
287
    for (int32_t j = 0; j < 5; ++j) {
B
Bomin Zhang 已提交
288 289 290 291 292 293 294
      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 已提交
295 296 297 298
    }
  }

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

B
Bomin Zhang 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
  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 已提交
320
  }
B
Bomin Zhang 已提交
321 322 323 324 325 326 327 328 329 330
  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 已提交
331 332 333 334 335 336 337 338 339 340

  tSkipListDestroy(pSkipList);
}

}  // namespace

TEST(testCase, skiplist_test) {
  assert(sizeof(SSkipListKey) == 8);
  srand(time(NULL));

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

  //  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
Shengliang Guan 已提交
360
      taosTFree(pNodes);
H
hjxilinx 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373

      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
Shengliang Guan 已提交
374
      taosTFree(pNodes);
H
hjxilinx 已提交
375 376 377

      free(pKeys);*/
}