lhashTests.cpp 2.3 KB
Newer Older
H
Haojun Liao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <gtest/gtest.h>
#include <iostream>
18
#include "executorInt.h"
H
Haojun Liao 已提交
19 20 21 22 23 24 25 26 27
#include "tlinearhash.h"

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wwrite-strings"
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wsign-compare"

TEST(testCase, linear_hash_Tests) {
wafwerar's avatar
wafwerar 已提交
28
  taosSeedRand(taosGetTimestampSec());
H
Haojun Liao 已提交
29
  strcpy(tsTempDir, "/tmp/");
H
Haojun Liao 已提交
30 31

  _hash_fn_t fn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT);
H
Haojun Liao 已提交
32 33 34

  int64_t st = taosGetTimestampUs();

H
Hongze Cheng 已提交
35 36
  SLHashObj* pHashObj = tHashInit(4098 * 4 * 2, 512, fn, 40);
  for (int32_t i = 0; i < 1000000; ++i) {
H
Haojun Liao 已提交
37 38
    int32_t code = tHashPut(pHashObj, &i, sizeof(i), &i, sizeof(i));
    assert(code == 0);
H
Haojun Liao 已提交
39 40
  }

H
Hongze Cheng 已提交
41
  //  tHashPrint(pHashObj, LINEAR_HASH_STATIS);
H
Haojun Liao 已提交
42
  int64_t et = taosGetTimestampUs();
H
Haojun Liao 已提交
43

H
Hongze Cheng 已提交
44
  for (int32_t i = 0; i < 1000000; ++i) {
H
Haojun Liao 已提交
45 46 47 48 49
    if (i == 950000) {
      printf("kf\n");
    }
    char* v = tHashGet(pHashObj, &i, sizeof(i));
    if (v != NULL) {
H
Hongze Cheng 已提交
50
      //      printf("find value: %d, key:%d\n", *(int32_t*) v, i);
H
Haojun Liao 已提交
51
    } else {
H
Hongze Cheng 已提交
52
      //      printf("failed to found key:%d in hash\n", i);
H
Haojun Liao 已提交
53 54
    }
  }
H
Haojun Liao 已提交
55

H
Hongze Cheng 已提交
56
  //  tHashPrint(pHashObj, LINEAR_HASH_STATIS);
H
Haojun Liao 已提交
57
  tHashCleanup(pHashObj);
H
Haojun Liao 已提交
58
  int64_t et1 = taosGetTimestampUs();
H
Haojun Liao 已提交
59

H
Haojun Liao 已提交
60
  SHashObj* pHashObj1 = taosHashInit(1000, fn, false, HASH_NO_LOCK);
H
Hongze Cheng 已提交
61
  for (int32_t i = 0; i < 1000000; ++i) {
H
Haojun Liao 已提交
62
    taosHashPut(pHashObj1, &i, sizeof(i), &i, sizeof(i));
H
Haojun Liao 已提交
63 64
  }

H
Hongze Cheng 已提交
65
  for (int32_t i = 0; i < 1000000; ++i) {
H
Haojun Liao 已提交
66
    void* v = taosHashGet(pHashObj1, &i, sizeof(i));
H
Haojun Liao 已提交
67
  }
H
Haojun Liao 已提交
68
  taosHashCleanup(pHashObj1);
H
Haojun Liao 已提交
69

H
Haojun Liao 已提交
70
  int64_t et2 = taosGetTimestampUs();
H
Hongze Cheng 已提交
71 72
  printf("linear hash time:%.2f ms, buildHash:%.2f ms, hash:%.2f\n", (et1 - st) / 1000.0, (et - st) / 1000.0,
         (et2 - et1) / 1000.0);
H
Haojun Liao 已提交
73
}