TDengine/src/util/tests/cacheTest.cpp

135 lines
4.3 KiB
C++
Raw Normal View History

2020-03-29 06:14:51 +00:00
#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;
2020-07-03 04:16:03 +00:00
SCacheObj* tscCacheHandle = taosCacheInit(TSDB_DATA_TYPE_BINARY, REFRESH_TIME_IN_SEC, 0, NULL, "test");
2020-03-29 06:14:51 +00:00
2020-04-05 08:40:08 +00:00
const char* key1 = "test1";
char data1[] = "test11";
2020-03-29 06:14:51 +00:00
2020-07-01 09:19:14 +00:00
char* cachedObj = (char*) taosCachePut(tscCacheHandle, key1, strlen(key1), data1, strlen(data1)+1, 1);
2020-03-29 06:14:51 +00:00
sleep(REFRESH_TIME_IN_SEC+1);
printf("obj is still valid: %s\n", cachedObj);
2020-04-05 08:40:08 +00:00
char data2[] = "test22";
2020-03-29 06:14:51 +00:00
taosCacheRelease(tscCacheHandle, (void**) &cachedObj, false);
/* the object is cleared by cache clean operation */
2020-07-01 09:19:14 +00:00
cachedObj = (char*) taosCachePut(tscCacheHandle, key1, strlen(key1), data2, strlen(data2)+1, 20);
2020-03-29 06:14:51 +00:00
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");
2020-04-05 08:40:08 +00:00
const char* key3 = "test2";
const char* data3 = "kkkkkkk";
2020-03-29 06:14:51 +00:00
2020-07-01 09:19:14 +00:00
char* cachedObj2 = (char*) taosCachePut(tscCacheHandle, key3, strlen(key3), data3, strlen(data3) + 1, 1);
2020-03-29 06:14:51 +00:00
printf("%s\n", cachedObj2);
taosCacheRelease(tscCacheHandle, (void**) &cachedObj2, false);
sleep(3);
2020-07-01 09:19:14 +00:00
char* d = (char*) taosCacheAcquireByKey(tscCacheHandle, key3, strlen(key3));
2020-03-29 06:14:51 +00:00
// assert(d == NULL);
2020-04-05 08:40:08 +00:00
char key5[] = "test5";
char data5[] = "data5kkkkk";
2020-07-01 09:19:14 +00:00
cachedObj2 = (char*) taosCachePut(tscCacheHandle, key5, strlen(key5), data5, strlen(data5) + 1, 20);
2020-03-29 06:14:51 +00:00
2020-04-05 08:40:08 +00:00
const char* data6= "new Data after updated";
2020-03-29 06:14:51 +00:00
taosCacheRelease(tscCacheHandle, (void**) &cachedObj2, false);
2020-07-01 09:19:14 +00:00
cachedObj2 = (char*) taosCachePut(tscCacheHandle, key5, strlen(key5), data6, strlen(data6) + 1, 20);
2020-03-29 06:14:51 +00:00
printf("%s\n", cachedObj2);
taosCacheRelease(tscCacheHandle, (void**) &cachedObj2, true);
2020-04-05 08:40:08 +00:00
const char* data7 = "add call update procedure";
2020-07-01 09:19:14 +00:00
cachedObj2 = (char*) taosCachePut(tscCacheHandle, key5, strlen(key5), data7, strlen(data7) + 1, 20);
2020-03-29 06:14:51 +00:00
printf("%s\n=======================================\n\n", cachedObj2);
2020-07-01 09:19:14 +00:00
char* cc = (char*) taosCacheAcquireByKey(tscCacheHandle, key5, strlen(key5));
2020-03-29 06:14:51 +00:00
taosCacheRelease(tscCacheHandle, (void**) &cachedObj2, true);
taosCacheRelease(tscCacheHandle, (void**) &cc, false);
2020-04-05 08:40:08 +00:00
const char* data8 = "ttft";
const char* key6 = "key6";
2020-03-29 06:14:51 +00:00
2020-07-01 09:19:14 +00:00
char* ft = (char*) taosCachePut(tscCacheHandle, key6, strlen(key6), data8, strlen(data8), 20);
2020-03-29 06:14:51 +00:00
taosCacheRelease(tscCacheHandle, (void**) &ft, false);
/**
* 140ns
*/
uint64_t startTime = taosGetTimestampUs();
2020-04-05 08:40:08 +00:00
printf("Cache Performance Test\nstart time:%" PRIu64 "\n", startTime);
2020-03-29 06:14:51 +00:00
for(int32_t i=0; i<1000; ++i) {
2020-07-01 09:19:14 +00:00
char* dd = (char*) taosCacheAcquireByKey(tscCacheHandle, key6, strlen(key6));
2020-03-29 06:14:51 +00:00
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;
2020-04-05 08:40:08 +00:00
printf("End of Test, %" PRIu64 "\nTotal Elapsed Time:%" PRIu64 " us.avg:%f us\n", endTime, el, el/1000.0);
2020-03-29 06:14:51 +00:00
taosCacheCleanup(tscCacheHandle);
}
TEST(testCase, cache_resize_test) {
const int32_t REFRESH_TIME_IN_SEC = 2;
2020-07-03 04:16:03 +00:00
auto* pCache = taosCacheInit(TSDB_DATA_TYPE_BINARY, REFRESH_TIME_IN_SEC, false, NULL, "test");
2020-03-29 06:14:51 +00:00
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);
2020-07-01 09:19:14 +00:00
taosCachePut(pCache, key, strlen(key), data, len, 3600);
2020-03-29 06:14:51 +00:00
}
uint64_t endTime = taosGetTimestampUs();
2020-04-05 08:40:08 +00:00
printf("add %d object cost:%" PRIu64 " us, avg:%f us\n", num, endTime - startTime, (endTime-startTime)/(double)num);
2020-03-29 06:14:51 +00:00
startTime = taosGetTimestampUs();
for(int32_t i = 0; i < num; ++i) {
int32_t len = sprintf(key, "abc_%7d", i);
2020-07-01 09:19:14 +00:00
void* k = taosCacheAcquireByKey(pCache, key, len);
2020-03-29 06:14:51 +00:00
assert(k != 0);
}
endTime = taosGetTimestampUs();
2020-04-05 08:40:08 +00:00
printf("retrieve %d object cost:%" PRIu64 " us,avg:%f\n", num, endTime - startTime, (endTime - startTime)/(double)num);
2020-03-29 06:14:51 +00:00
taosCacheCleanup(pCache);
}