[kvdb] add db size scale up testcase

This commit is contained in:
朱天龙 (Armink) 2023-05-19 20:50:05 +08:00
parent 3c80fa5679
commit dab9d2bba9
1 changed files with 172 additions and 27 deletions

View File

@ -16,14 +16,25 @@
#include <flashdb.h>
#include <stdio.h>
#include <stdlib.h>
#define RT_USING_UTEST
#if RT_VER_NUM <= 0x40003
#include <dfs_posix.h>
#else
#include <dfs_file.h>
#include <unistd.h>
#endif
#define TEST_TS_PART_NAME "fdb_kvdb1"
#define TEST_KV_BLOB_NAME "kv_blob_test"
#define TEST_KV_NAME "kv_test"
#define TEST_KV_VALUE_LEN 1200 /* only save 3 KVs in a 4096 sector */
#define TEST_KV_NUM 4
#define TEST_KV_MAX_NUM 8
#define TEST_KVDB_SECTOR_SIZE 4096
#define TEST_KVDB_SECTOR_NUM 4
#define FDB_ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
#if defined(RT_USING_UTEST) && defined(FDB_USING_KVDB)
@ -39,14 +50,76 @@ static struct fdb_kvdb test_kvdb;
static void test_fdb_kvdb_deinit(void);
static void test_fdb_kvdb_init(void)
static rt_err_t dir_delete(const char* path)
{
DIR* dir = NULL;
struct dirent* dirent = NULL;
char* full_path;
rt_err_t res = RT_EOK;
if (path == RT_NULL || path[0] == '\0')
return -ENOENT;
full_path = (char*)rt_malloc(DFS_PATH_MAX);
if (full_path == RT_NULL)
return -ENOMEM;
dir = opendir(path);
if (dir == RT_NULL)
{
rt_free(full_path);
return -ENOENT;
}
while (1)
{
dirent = readdir(dir);
if (dirent == RT_NULL)
break;
if (rt_strcmp(".", dirent->d_name) != 0 &&
rt_strcmp("..", dirent->d_name) != 0)
{
rt_snprintf(full_path, DFS_PATH_MAX, "%s/%s", path, dirent->d_name);
if (dirent->d_type == DT_REG)
{
if (unlink(full_path) != 0)
{
LOG_W("cannot remove '%s'", full_path);
res = -RT_ERROR;
}
}
else if (dirent->d_type == DT_DIR)
{
if (dir_delete(full_path) != RT_EOK)
{
res = -RT_ERROR;
}
}
}
}
closedir(dir);
rt_free(full_path);
if (path[rt_strlen(path) - 1] != '/')
{
if (unlink(path) != 0)
{
LOG_W("cannot remove '%s'", path);
res = -RT_ERROR;
}
}
return res;
}
static void test_fdb_kvdb_init_by_sector_num(size_t sector_num)
{
if (access(TEST_TS_PART_NAME, 0) < 0)
{
mkdir(TEST_TS_PART_NAME, 0);
}
uint32_t sec_size = TEST_KVDB_SECTOR_SIZE, db_size = sec_size * 4;
uint32_t sec_size = TEST_KVDB_SECTOR_SIZE, db_size = sec_size * sector_num;
rt_bool_t file_mode = true;
fdb_kvdb_control(&(test_kvdb), FDB_KVDB_CTRL_SET_SEC_SIZE, &sec_size);
@ -56,12 +129,27 @@ static void test_fdb_kvdb_init(void)
uassert_true(fdb_kvdb_init(&test_kvdb, "test_kv", TEST_TS_PART_NAME, NULL, NULL) == FDB_NO_ERR);
}
static void test_fdb_kvdb_init(void)
{
test_fdb_kvdb_init_by_sector_num(TEST_KVDB_SECTOR_NUM);
}
static void test_fdb_kvdb_init_by_8_sectors(void)
{
test_fdb_kvdb_init_by_sector_num(8);
}
static void test_fdb_kvdb_init_check(void)
{
/* check the oldest address */
uassert_true(RT_ALIGN_DOWN(test_kvdb.parent.oldest_addr, TEST_KVDB_SECTOR_SIZE) == TEST_KVDB_SECTOR_SIZE * 0);
}
static void test_fdb_kvdb_deinit(void)
{
uassert_true(fdb_kvdb_deinit(&test_kvdb) == FDB_NO_ERR);
}
static void test_fdb_create_kv_blob(void)
{
fdb_err_t result = FDB_NO_ERR;
@ -168,7 +256,6 @@ static void test_fdb_change_kv(void)
/* check the oldest address is already right when kvdb reinit */
static void fdb_reboot(void)
{
extern void test_fdb_kvdb_deinit(void);
test_fdb_kvdb_deinit();
test_fdb_kvdb_init();
}
@ -197,7 +284,7 @@ static void test_fdb_del_kv(void)
}
}
static int iter_all_kv(fdb_kvdb_t db, struct test_kv *kv_tbl)
static int iter_all_kv(fdb_kvdb_t db, struct test_kv *kv_tbl, size_t len)
{
struct fdb_kv_iterator iterator;
fdb_kv_t cur_kv;
@ -206,7 +293,7 @@ static int iter_all_kv(fdb_kvdb_t db, struct test_kv *kv_tbl)
struct fdb_blob fdb_blob;
fdb_kv_iterator_init(db, &iterator);
while (fdb_kv_iterate(db, &iterator) == RT_TRUE)
while (fdb_kv_iterate(db, &iterator) == RT_TRUE && index < len)
{
/* get data len */
cur_kv = &(iterator.curr_kv);
@ -222,15 +309,12 @@ static int iter_all_kv(fdb_kvdb_t db, struct test_kv *kv_tbl)
index++;
}
uassert_true(index == TEST_KV_NUM);
return index;
}
static void test_fdb_by_kvs(const struct test_kv *kv_tbl, size_t len)
static void test_save_fdb_by_kvs(const struct test_kv *kv_tbl, size_t len)
{
struct fdb_blob blob_obj, * blob = &blob_obj;
static struct test_kv saved_kv_tbl[TEST_KV_NUM] = { 0 };
for (size_t i = 0; i < len; i++)
{
@ -239,15 +323,39 @@ static void test_fdb_by_kvs(const struct test_kv *kv_tbl, size_t len)
fdb_kv_set_blob(&test_kvdb, kv_tbl[i].name, fdb_blob_make(blob, kv_tbl[i].value, TEST_KV_VALUE_LEN));
}
}
}
iter_all_kv(&test_kvdb, saved_kv_tbl);
static void test_check_fdb_by_kvs(const struct test_kv *kv_tbl, size_t len)
{
static struct test_kv saved_kv_tbl[TEST_KV_MAX_NUM] = { 0 };
for (size_t i = 0; i < len; i++)
iter_all_kv(&test_kvdb, saved_kv_tbl, FDB_ARRAY_SIZE(saved_kv_tbl));
for (size_t i = 0, j = 0; i < len; i++)
{
uassert_str_equal(saved_kv_tbl[i].name, kv_tbl[i].name);
uassert_str_equal(saved_kv_tbl[i].value, kv_tbl[i].value);
uassert_true(RT_ALIGN_DOWN(saved_kv_tbl[i].addr, TEST_KVDB_SECTOR_SIZE) == TEST_KVDB_SECTOR_SIZE * kv_tbl[i].addr);
for (j = 0; j < FDB_ARRAY_SIZE(saved_kv_tbl); j++)
{
if (rt_strcmp(saved_kv_tbl[j].name, kv_tbl[i].name) == 0)
break;
}
if (j < FDB_ARRAY_SIZE(saved_kv_tbl))
{
uassert_str_equal(saved_kv_tbl[j].name, kv_tbl[i].name);
uassert_str_equal(saved_kv_tbl[j].value, kv_tbl[i].value);
uassert_true(RT_ALIGN_DOWN(saved_kv_tbl[j].addr, TEST_KVDB_SECTOR_SIZE) == TEST_KVDB_SECTOR_SIZE * kv_tbl[i].addr);
}
else
{
/* kv not found */
uassert_true(0);
}
}
}
static void test_fdb_by_kvs(const struct test_kv *kv_tbl, size_t len)
{
test_save_fdb_by_kvs(kv_tbl, len);
test_check_fdb_by_kvs(kv_tbl, len);
}
static void test_fdb_gc(void)
@ -277,14 +385,14 @@ static void test_fdb_gc(void)
* | | | | |
* +---------------------------------------------------------+
*/
const struct test_kv kv_tbl[] = {
static const struct test_kv kv_tbl[] = {
{"kv0", "0", 0, 0, 1},
{"kv1", "1", 0, 0, 1},
{"kv2", "2", 0, 0, 1},
{"kv3", "3", 1, 0, 1},
};
test_fdb_by_kvs(kv_tbl, sizeof(kv_tbl) / sizeof(kv_tbl[0]));
test_fdb_by_kvs(kv_tbl, FDB_ARRAY_SIZE(kv_tbl));
uassert_true(RT_ALIGN_DOWN(test_kvdb.parent.oldest_addr, TEST_KVDB_SECTOR_SIZE) == TEST_KVDB_SECTOR_SIZE * 0);
fdb_reboot();
uassert_true(RT_ALIGN_DOWN(test_kvdb.parent.oldest_addr, TEST_KVDB_SECTOR_SIZE) == TEST_KVDB_SECTOR_SIZE * 0);
@ -313,14 +421,14 @@ static void test_fdb_gc(void)
* | | | | |
* +--------------+-------------+--------------+-------------+
*/
const struct test_kv kv_tbl[] = {
static const struct test_kv kv_tbl[] = {
{"kv1", "1", 0, 0, 0},
{"kv2", "2", 0, 0, 0},
{"kv0", "00", 1, 0, 1},
{"kv3", "33", 1, 0, 1},
};
test_fdb_by_kvs(kv_tbl, sizeof(kv_tbl) / sizeof(kv_tbl[0]));
test_fdb_by_kvs(kv_tbl, FDB_ARRAY_SIZE(kv_tbl));
uassert_true(RT_ALIGN_DOWN(test_kvdb.parent.oldest_addr, TEST_KVDB_SECTOR_SIZE) == TEST_KVDB_SECTOR_SIZE * 0);
fdb_reboot();
uassert_true(RT_ALIGN_DOWN(test_kvdb.parent.oldest_addr, TEST_KVDB_SECTOR_SIZE) == TEST_KVDB_SECTOR_SIZE * 0);
@ -393,14 +501,14 @@ static void test_fdb_gc(void)
* +--------------+-------------+--------------+-------------+
*
*/
const struct test_kv kv_tbl[] = {
static const struct test_kv kv_tbl[] = {
{"kv0", "000", 2, 0, 1},
{"kv1", "111", 2, 0, 1},
{"kv2", "222", 2, 0, 1},
{"kv3", "333", 3, 0, 1},
};
test_fdb_by_kvs(kv_tbl, sizeof(kv_tbl) / sizeof(kv_tbl[0]));
test_fdb_by_kvs(kv_tbl, FDB_ARRAY_SIZE(kv_tbl));
uassert_true(RT_ALIGN_DOWN(test_kvdb.parent.oldest_addr, TEST_KVDB_SECTOR_SIZE) == TEST_KVDB_SECTOR_SIZE * 1);
fdb_reboot();
uassert_true(RT_ALIGN_DOWN(test_kvdb.parent.oldest_addr, TEST_KVDB_SECTOR_SIZE) == TEST_KVDB_SECTOR_SIZE * 1);
@ -493,14 +601,14 @@ static void test_fdb_gc(void)
* | | | | |
* +--------------+-------------+--------------+-------------+
*/
const struct test_kv kv_tbl[] = {
static const struct test_kv kv_tbl[] = {
{"kv0", "0000", 3, 0, 1},
{"kv1", "1111", 3, 0, 1},
{"kv2", "2222", 0, 0, 1},
{"kv3", "3333", 0, 0, 1},
};
test_fdb_by_kvs(kv_tbl, sizeof(kv_tbl) / sizeof(kv_tbl[0]));
test_fdb_by_kvs(kv_tbl, FDB_ARRAY_SIZE(kv_tbl));
uassert_true(RT_ALIGN_DOWN(test_kvdb.parent.oldest_addr, TEST_KVDB_SECTOR_SIZE) == TEST_KVDB_SECTOR_SIZE * 2);
fdb_reboot();
uassert_true(RT_ALIGN_DOWN(test_kvdb.parent.oldest_addr, TEST_KVDB_SECTOR_SIZE) == TEST_KVDB_SECTOR_SIZE * 2);
@ -508,18 +616,54 @@ static void test_fdb_gc(void)
}
}
static void test_fdb_scale_up(void)
{
fdb_kv_set_default(&test_kvdb);
static const struct test_kv old_kv_tbl[] = {
{"kv0", "0", 0, 0, 1},
{"kv1", "1", 0, 0, 1},
{"kv2", "2", 0, 0, 1},
{"kv3", "3", 1, 0, 1},
};
/* save some data */
test_save_fdb_by_kvs(old_kv_tbl, FDB_ARRAY_SIZE(old_kv_tbl));
/* reboot, scale up from 4 sectors to 8 sectors */
test_fdb_kvdb_deinit();
test_fdb_kvdb_init_by_8_sectors();
/* check old data */
test_check_fdb_by_kvs(old_kv_tbl, FDB_ARRAY_SIZE(old_kv_tbl));
/* save some new data */
static const struct test_kv new_kv_tbl[] = {
{"kv4", "4", 4, 0, 1},
{"kv5", "5", 4, 0, 1},
{"kv6", "6", 4, 0, 1},
{"kv7", "7", 5, 0, 1},
};
/* kv4: sector1, kv5: sector1, kv6: sector2, kv7: sector2 */
test_save_fdb_by_kvs(new_kv_tbl, FDB_ARRAY_SIZE(new_kv_tbl));
/* kv4: sector2, kv5: sector3, kv6: sector3, kv7: sector3 */
test_save_fdb_by_kvs(new_kv_tbl, FDB_ARRAY_SIZE(new_kv_tbl));
/* kv4: sector4, kv5: sector4, kv6: sector4, kv7: sector5 */
test_save_fdb_by_kvs(new_kv_tbl, FDB_ARRAY_SIZE(new_kv_tbl));
/* check new data */
test_check_fdb_by_kvs(new_kv_tbl, FDB_ARRAY_SIZE(new_kv_tbl));
/* check old data */
test_check_fdb_by_kvs(old_kv_tbl, FDB_ARRAY_SIZE(old_kv_tbl));
}
static void test_fdb_kvdb_set_default(void)
{
uassert_true(fdb_kv_set_default(&test_kvdb) == FDB_NO_ERR);
}
static void test_fdb_kvdb_deinit(void)
{
uassert_true(fdb_kvdb_deinit(&test_kvdb) == FDB_NO_ERR);
}
static rt_err_t utest_tc_init(void)
{
dir_delete(TEST_TS_PART_NAME);
return RT_EOK;
}
@ -545,6 +689,7 @@ static void testcase(void)
UTEST_UNIT_RUN(test_fdb_change_kv);
UTEST_UNIT_RUN(test_fdb_del_kv);
UTEST_UNIT_RUN(test_fdb_gc);
UTEST_UNIT_RUN(test_fdb_scale_up);
UTEST_UNIT_RUN(test_fdb_kvdb_set_default);
UTEST_UNIT_RUN(test_fdb_kvdb_deinit);
}