TDengine/source/util/test/cacheTest.cpp

145 lines
4.6 KiB
C++
Raw Normal View History

2020-03-29 06:14:51 +00:00
#include <gtest/gtest.h>
2022-04-22 11:47:00 +00:00
#include <iostream>
2020-03-29 06:14:51 +00:00
#include "os.h"
2020-03-29 06:14:51 +00:00
#include "taos.h"
#include "tcache.h"
// test cache
2022-03-03 10:49:39 +00:00
TEST(cacheTest, client_cache_test) {
2020-03-29 06:14:51 +00:00
const int32_t REFRESH_TIME_IN_SEC = 2;
2022-04-22 11:47:00 +00:00
SCacheObj* tscMetaCache = taosCacheInit(TSDB_DATA_TYPE_BINARY, REFRESH_TIME_IN_SEC * 1000, 0, NULL, "test");
2020-03-29 06:14:51 +00:00
2020-04-05 08:40:08 +00:00
const char* key1 = "test1";
2022-04-22 11:47:00 +00:00
char data1[] = "test11";
2020-03-29 06:14:51 +00:00
2022-04-22 11:47:00 +00:00
char* cachedObj = (char*)taosCachePut(tscMetaCache, key1, strlen(key1), data1, strlen(data1) + 1, 1);
taosSsleep(REFRESH_TIME_IN_SEC + 1);
2020-03-29 06:14:51 +00:00
printf("obj is still valid: %s\n", cachedObj);
2020-04-05 08:40:08 +00:00
char data2[] = "test22";
2022-04-22 11:47:00 +00:00
taosCacheRelease(tscMetaCache, (void**)&cachedObj, false);
2020-03-29 06:14:51 +00:00
/* the object is cleared by cache clean operation */
2022-04-22 11:47:00 +00:00
cachedObj = (char*)taosCachePut(tscMetaCache, 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");
2022-04-22 11:47:00 +00:00
taosCacheRelease(tscMetaCache, (void**)&cachedObj, false);
2020-03-29 06:14:51 +00:00
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
2022-04-22 11:47:00 +00:00
char* cachedObj2 = (char*)taosCachePut(tscMetaCache, key3, strlen(key3), data3, strlen(data3) + 1, 1);
2020-03-29 06:14:51 +00:00
printf("%s\n", cachedObj2);
2022-04-22 11:47:00 +00:00
taosCacheRelease(tscMetaCache, (void**)&cachedObj2, false);
2020-03-29 06:14:51 +00:00
2022-03-10 03:56:11 +00:00
taosSsleep(3);
2022-04-22 11:47:00 +00:00
char* d = (char*)taosCacheAcquireByKey(tscMetaCache, key3, strlen(key3));
TD_ALWAYS_ASSERT(d == NULL);
2020-03-29 06:14:51 +00:00
2020-04-05 08:40:08 +00:00
char key5[] = "test5";
char data5[] = "data5kkkkk";
2022-04-22 11:47:00 +00:00
cachedObj2 = (char*)taosCachePut(tscMetaCache, key5, strlen(key5), data5, strlen(data5) + 1, 20);
2020-03-29 06:14:51 +00:00
2022-04-22 11:47:00 +00:00
const char* data6 = "new Data after updated";
taosCacheRelease(tscMetaCache, (void**)&cachedObj2, false);
2020-03-29 06:14:51 +00:00
2022-04-22 11:47:00 +00:00
cachedObj2 = (char*)taosCachePut(tscMetaCache, key5, strlen(key5), data6, strlen(data6) + 1, 20);
2020-03-29 06:14:51 +00:00
printf("%s\n", cachedObj2);
2022-04-22 11:47:00 +00:00
taosCacheRelease(tscMetaCache, (void**)&cachedObj2, true);
2020-03-29 06:14:51 +00:00
2020-04-05 08:40:08 +00:00
const char* data7 = "add call update procedure";
2022-04-22 11:47:00 +00:00
cachedObj2 = (char*)taosCachePut(tscMetaCache, key5, strlen(key5), data7, strlen(data7) + 1, 20);
2020-03-29 06:14:51 +00:00
printf("%s\n=======================================\n\n", cachedObj2);
2022-04-22 11:47:00 +00:00
char* cc = (char*)taosCacheAcquireByKey(tscMetaCache, key5, strlen(key5));
2020-03-29 06:14:51 +00:00
2022-04-22 11:47:00 +00:00
taosCacheRelease(tscMetaCache, (void**)&cachedObj2, true);
taosCacheRelease(tscMetaCache, (void**)&cc, false);
2020-03-29 06:14:51 +00:00
2020-04-05 08:40:08 +00:00
const char* data8 = "ttft";
const char* key6 = "key6";
2020-03-29 06:14:51 +00:00
2022-04-22 11:47:00 +00:00
char* ft = (char*)taosCachePut(tscMetaCache, key6, strlen(key6), data8, strlen(data8), 20);
taosCacheRelease(tscMetaCache, (void**)&ft, false);
2020-03-29 06:14:51 +00:00
/**
* 140ns
*/
uint64_t startTime = taosGetTimestampUs();
2020-04-05 08:40:08 +00:00
printf("Cache Performance Test\nstart time:%" PRIu64 "\n", startTime);
2022-04-22 11:47:00 +00:00
for (int32_t i = 0; i < 1000; ++i) {
char* dd = (char*)taosCacheAcquireByKey(tscMetaCache, key6, strlen(key6));
2020-03-29 06:14:51 +00:00
if (dd != NULL) {
2022-04-22 11:47:00 +00:00
// printf("get the data\n");
2020-03-29 06:14:51 +00:00
} else {
printf("data has been released\n");
}
2022-04-22 11:47:00 +00:00
taosCacheRelease(tscMetaCache, (void**)&dd, false);
2020-03-29 06:14:51 +00:00
}
uint64_t endTime = taosGetTimestampUs();
2022-04-22 11:47:00 +00:00
int64_t el = endTime - startTime;
2020-03-29 06:14:51 +00:00
2022-04-22 11:47:00 +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
2020-09-14 05:54:52 +00:00
taosCacheCleanup(tscMetaCache);
2020-03-29 06:14:51 +00:00
}
2022-03-03 10:49:39 +00:00
TEST(cacheTest, cache_iter_test) {
2020-03-29 06:14:51 +00:00
const int32_t REFRESH_TIME_IN_SEC = 2;
2022-04-22 11:47:00 +00:00
auto* pCache = taosCacheInit(TSDB_DATA_TYPE_BINARY, REFRESH_TIME_IN_SEC * 1000, false, NULL, "test");
2020-03-29 06:14:51 +00:00
char key[256] = {0};
char data[1024] = "abcdefghijk";
2022-04-22 11:47:00 +00:00
// int32_t len = strlen(data);
2020-03-29 06:14:51 +00:00
uint64_t startTime = taosGetTimestampUs();
2022-04-22 11:47:00 +00:00
int32_t num = 10000;
2020-03-29 06:14:51 +00:00
2022-04-22 11:47:00 +00:00
for (int32_t i = 0; i < num; ++i) {
2020-03-29 06:14:51 +00:00
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
}
2022-03-03 10:49:39 +00:00
2020-03-29 06:14:51 +00:00
uint64_t endTime = taosGetTimestampUs();
2022-04-22 11:47:00 +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();
2022-04-22 11:47:00 +00:00
for (int32_t i = 0; i < num; ++i) {
2020-03-29 06:14:51 +00:00
int32_t len = sprintf(key, "abc_%7d", i);
2022-04-22 11:47:00 +00:00
void* k = taosCacheAcquireByKey(pCache, key, len);
TD_ALWAYS_ASSERT(k != 0);
2020-03-29 06:14:51 +00:00
}
endTime = taosGetTimestampUs();
2022-04-22 11:47:00 +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
2022-04-22 11:47:00 +00:00
int32_t count = 0;
2022-03-03 10:49:39 +00:00
SCacheIter* pIter = taosCacheCreateIter(pCache);
2022-04-22 11:47:00 +00:00
while (taosCacheIterNext(pIter)) {
2022-03-03 10:49:39 +00:00
size_t keyLen = 0;
size_t dataLen = 0;
char* key1 = static_cast<char*>(taosCacheIterGetKey(pIter, &keyLen));
char* data1 = static_cast<char*>(taosCacheIterGetData(pIter, &dataLen));
2022-04-22 11:47:00 +00:00
// char d[256] = {0};
// memcpy(d, data1, dataLen);
// char k[256] = {0};
// memcpy(k, key1, keyLen);
2022-03-03 10:49:39 +00:00
}
ASSERT_EQ(count, num);
2020-03-29 06:14:51 +00:00
taosCacheCleanup(pCache);
2022-03-03 10:49:39 +00:00
}