cacheTest.cpp 4.2 KB
Newer Older
H
hjxilinx 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
#include <iostream>
#include <gtest/gtest.h>
#include <sys/time.h>

#include "taos.h"
//#include "tsdb.h"

//#include "testCommon.h"
#include "tstoken.h"
#include "tutil.h"
#include "tcache.h"
#include "ttimer.h"
#include "ttime.h"

namespace {
int32_t tsMaxMgmtConnections = 10000;
int32_t tsMaxMeterConnections = 200;
}
// test cache
TEST(testCase, client_cache_test) {
  const int32_t REFRESH_TIME_IN_SEC = 2;
  void* tscTmr = taosTmrInit (tsMaxMgmtConnections*2, 200, 6000, "TSC");
  SCacheObj* tscCacheHandle = taosCacheInit(tscTmr, REFRESH_TIME_IN_SEC);

  char* key1 = "test1";
  char* data1 = "test11";

  char* cachedObj = (char*) taosCachePut(tscCacheHandle, key1, data1, strlen(data1), 1);
  sleep(REFRESH_TIME_IN_SEC+1);

  printf("obj is still valid: %s\n", cachedObj);

  char* data2 = "test22";
  taosCacheRelease(tscCacheHandle, (void**) &cachedObj, false);

  /* the object is cleared by cache clean operation */
  cachedObj = (char*) taosCachePut(tscCacheHandle, key1, data2, strlen(data2), 20);
  printf("after updated: %s\n", cachedObj);

  printf("start to remove data from cache\n");
  taosCacheRelease(tscCacheHandle, (void**) &cachedObj, false);
  printf("end of removing data from cache\n");

  getchar();

  char* key3 = "test2";
  char* data3 = "kkkkkkk";

  char* cachedObj2 = (char*) taosCachePut(tscCacheHandle, key3, data3, strlen(data3), 1);
  printf("%s\n", cachedObj2);

  taosCacheRelease(tscCacheHandle, (void**) &cachedObj2, false);

  sleep(3);
  char* d = (char*) taosCacheAcquireByName(tscCacheHandle, key3);
//    assert(d == NULL);

  char* key5 = "test5";
  char* data5 = "data5kkkkk";
  cachedObj2 = (char*) taosCachePut(tscCacheHandle, key5, data5, strlen(data5), 20);

  char* data6= "new Data after updated";
  taosCacheRelease(tscCacheHandle, (void**) &cachedObj2, false);

  cachedObj2 = (char*) taosCachePut(tscCacheHandle, key5, data6, strlen(data6), 20);
  printf("%s\n", cachedObj2);

  taosCacheRelease(tscCacheHandle, (void**) &cachedObj2, true);

  char* data7 = "add call update procedure";
  cachedObj2 = (char*) taosCachePut(tscCacheHandle, key5, data7, strlen(data7), 20);
  printf("%s\n=======================================\n\n", cachedObj2);

  char* cc = (char*) taosCacheAcquireByName(tscCacheHandle, key5);

  taosCacheRelease(tscCacheHandle, (void**) &cachedObj2, true);
  taosCacheRelease(tscCacheHandle, (void**) &cc, false);

  char* data8 = "ttft";
  char* key6 = "key6";

  char* ft = (char*) taosCachePut(tscCacheHandle, key6, data8, strlen(data8), 20);
  taosCacheRelease(tscCacheHandle, (void**) &ft, false);

  /**
   * 140ns
   */
  uint64_t startTime = taosGetTimestampUs();
  printf("Cache Performance Test\nstart time:%lld\n", startTime);
  for(int32_t i=0; i<1000; ++i) {
    char* dd = (char*) taosCacheAcquireByName(tscCacheHandle, key6);
    if (dd != NULL) {
//      printf("get the data\n");
    } else {
      printf("data has been released\n");
    }

    taosCacheRelease(tscCacheHandle, (void**) &dd, false);
  }

  uint64_t endTime = taosGetTimestampUs();
  int64_t el = endTime - startTime;

  printf("End of Test, %lld\nTotal Elapsed Time:%lld us.avg:%f us\n", endTime, el, el/1000.0);

  taosCacheCleanup(tscCacheHandle);
}

TEST(testCase, cache_resize_test) {
  const int32_t REFRESH_TIME_IN_SEC = 2;
  void* tscTmr = taosTmrInit (1000*2, 200, 6000, "TSC");

  auto* pCache = taosCacheInit(tscTmr, REFRESH_TIME_IN_SEC);

  char key[256] = {0};
  char data[1024] = "abcdefghijk";
  int32_t len = strlen(data);

  uint64_t startTime = taosGetTimestampUs();
  int32_t num = 10000;

  for(int32_t i = 0; i < num; ++i) {
    int32_t len = sprintf(key, "abc_%7d", i);
    taosCachePut(pCache, key, data, len, 3600);
  }
  uint64_t endTime = taosGetTimestampUs();

  printf("add 10,000,000 object cost:%lld us, avg:%f us\n", endTime - startTime, (endTime-startTime)/(double)num);

  startTime = taosGetTimestampUs();
  for(int32_t i = 0; i < num; ++i) {
    int32_t len = sprintf(key, "abc_%7d", i);
    void* k = taosCacheAcquireByName(pCache, key);
    assert(k != 0);
  }
  endTime = taosGetTimestampUs();
  printf("retrieve 10,000,000 object cost:%lld us,avg:%f\n", endTime - startTime, (endTime - startTime)/(double)num);

  taosCacheCleanup(pCache);
  taosMsleep(20000);
  getchar();
}