图片修改
This commit is contained in:
223
test/testcase.c
223
test/testcase.c
@@ -156,11 +156,11 @@ void hash_testcase_1w(int connfd) {
|
||||
|
||||
}
|
||||
|
||||
void do_batch_test(int fd, int op, const char *key, const char *value){
|
||||
void do_batch_test(int fd, int op, const char *key, const char *value, rsp_ret_status_e st, const char *rsp_value){
|
||||
kvs_batch_t batch;
|
||||
kvs_batch_init(&batch);
|
||||
|
||||
char bkey[15]={0}, bval[15]={0};
|
||||
char bkey[256]={0}, bval[256]={0};
|
||||
|
||||
// 组 batch(最多 64 条)
|
||||
for(int i = 0;i < 100; ++ i){
|
||||
@@ -184,18 +184,70 @@ void do_batch_test(int fd, int op, const char *key, const char *value){
|
||||
int nrsp = kvs_batch_recv_parse(fd, &batch, rsps, recvbuf, sizeof(recvbuf));
|
||||
|
||||
// 打印/处理
|
||||
for (int i = 0; i < nrsp; i++) {
|
||||
int len = sprintf(bkey, "PRT%d", i);
|
||||
PRESP(bkey, &rsps[i]);
|
||||
for (int i = 0; i < nrsp; i++) {
|
||||
print_response(bkey, &rsps[i]);
|
||||
int vlen;
|
||||
if(rsp_value != NULL) vlen = sprintf(bval, "%s%d", rsp_value, i);
|
||||
else vlen = 0;
|
||||
verify_response(&rsps[i], op, st, bval, vlen);
|
||||
}
|
||||
}
|
||||
|
||||
void array_testcase_1w_batch(int connfd) {
|
||||
kvs_batch_t batch;
|
||||
kvs_batch_init(&batch);
|
||||
|
||||
int count = 1000;
|
||||
int i = 0;
|
||||
|
||||
struct timeval tv_begin;
|
||||
gettimeofday(&tv_begin, NULL);
|
||||
|
||||
for (i = 0;i < count;i ++) {
|
||||
batch.cnt = 0;
|
||||
batch.len = 0;
|
||||
kvs_batch_add(&batch, KVS_CMD_SET, "name", 4, "l\r\0n", 4);
|
||||
kvs_batch_add(&batch, KVS_CMD_GET, "name", 4, NULL, 0);
|
||||
kvs_batch_add(&batch, KVS_CMD_MOD, "name", 4, "liu", 3);
|
||||
kvs_batch_add(&batch, KVS_CMD_GET, "name", 4, NULL, 0);
|
||||
kvs_batch_add(&batch, KVS_CMD_EXIST, "name", 4, NULL, 0);
|
||||
kvs_batch_add(&batch, KVS_CMD_DEL, "name", 4, NULL, 0);
|
||||
kvs_batch_add(&batch, KVS_CMD_EXIST, "name", 4, NULL, 0);
|
||||
|
||||
kvs_batch_add(&batch, KVS_CMD_MOD, "stu", 3, "liu", 3);
|
||||
kvs_batch_add(&batch, KVS_CMD_DEL, "stu", 3, NULL, 0);
|
||||
|
||||
kvs_batch_send(connfd, &batch);
|
||||
|
||||
uint8_t recvbuf[BATCH_SIZE];
|
||||
kvs_response_t rsps[KVS_BATCH_MAX];
|
||||
int nrsp = kvs_batch_recv_parse(connfd, &batch, rsps, recvbuf, sizeof(recvbuf));
|
||||
}
|
||||
|
||||
struct timeval tv_end;
|
||||
gettimeofday(&tv_end, NULL);
|
||||
|
||||
int time_used = TIME_SUB_MS(tv_end, tv_begin); // ms
|
||||
|
||||
printf("array testcase --> time_used: %d, qps: %d\n", time_used, 9000 * 1000 / time_used);
|
||||
|
||||
}
|
||||
|
||||
void batch_qps(int connfd) {
|
||||
const int N = 1000000;
|
||||
const int B = 100; // do_batch_test() 里写死 50
|
||||
const char *valA = "va";
|
||||
const char *valB = "vb";
|
||||
const char *valC = "vc";
|
||||
static char valA[256];
|
||||
static char valB[256];
|
||||
static char valC[256];
|
||||
|
||||
static int inited = 0;
|
||||
if (!inited) {
|
||||
// 填充 255 字节,最后补 '\0'
|
||||
memset(valA, 'A', 255); valA[255] = '\0';
|
||||
memset(valB, 'B', 255); valB[255] = '\0';
|
||||
memset(valC, 'C', 255); valC[255] = '\0';
|
||||
inited = 1;
|
||||
}
|
||||
|
||||
struct timeval tv_begin, tv_end;
|
||||
gettimeofday(&tv_begin, NULL);
|
||||
@@ -214,9 +266,9 @@ void batch_qps(int connfd) {
|
||||
snprintf(preA, sizeof(preA), "A%d_", base/100);
|
||||
snprintf(preB, sizeof(preB), "B%d_", base/100);
|
||||
|
||||
do_batch_test(connfd, KVS_CMD_RSET, preA, valA); // 50次 RSET A
|
||||
do_batch_test(connfd, KVS_CMD_RSET, preB, valB); // 50次 RSET B
|
||||
do_batch_test(connfd, KVS_CMD_RDEL, preA, NULL); // 50次 RDEL A
|
||||
do_batch_test(connfd, KVS_CMD_RSET, preA, valA, KVS_STATUS_OK, NULL); // 50次 RSET A
|
||||
do_batch_test(connfd, KVS_CMD_RSET, preB, valB, KVS_STATUS_OK, NULL); // 50次 RSET B
|
||||
do_batch_test(connfd, KVS_CMD_RDEL, preA, NULL, KVS_STATUS_OK, NULL); // 50次 RDEL A
|
||||
|
||||
if (base % 10000 == 0) printf("P1 base:%d\n", base);
|
||||
}
|
||||
@@ -229,9 +281,9 @@ void batch_qps(int connfd) {
|
||||
snprintf(preB, sizeof(preB), "B%d_", base/100);
|
||||
snprintf(preC, sizeof(preC), "C%d_", base/100);
|
||||
|
||||
do_batch_test(connfd, KVS_CMD_RSET, preC, valC); // 50次 RSET C
|
||||
do_batch_test(connfd, KVS_CMD_RDEL, preB, NULL); // 50次 RDEL B
|
||||
do_batch_test(connfd, KVS_CMD_RDEL, preC, NULL); // 50次 RDEL C
|
||||
do_batch_test(connfd, KVS_CMD_RSET, preC, valC, KVS_STATUS_OK, NULL); // 50次 RSET C
|
||||
do_batch_test(connfd, KVS_CMD_RDEL, preB, NULL, KVS_STATUS_OK, NULL); // 50次 RDEL B
|
||||
do_batch_test(connfd, KVS_CMD_RDEL, preC, NULL, KVS_STATUS_OK, NULL); // 50次 RDEL C
|
||||
|
||||
if (base % 10000 == 0) printf("P2 base:%d\n", base);
|
||||
}
|
||||
@@ -320,6 +372,118 @@ void testcase_add2_del1_then_add1_del2_100w(int connfd) {
|
||||
N, time_used, ops, qps);
|
||||
}
|
||||
|
||||
void send_spec_chars(int connfd){
|
||||
const char *v_ws = "li an\tok\nend"; /* 内容含空格、\t、\n */
|
||||
int v_ws_len = 12; /* 手算:l i ' ' a n \t o k \n e n d = 12 */
|
||||
|
||||
testcase(connfd, KVS_CMD_RSET,
|
||||
"ws", 2,
|
||||
v_ws, v_ws_len,
|
||||
KVS_STATUS_OK,
|
||||
NULL, 0,
|
||||
"RSET WHITESPACE");
|
||||
|
||||
testcase(connfd, KVS_CMD_RGET,
|
||||
"ws", 2,
|
||||
NULL, 0,
|
||||
KVS_STATUS_OK,
|
||||
v_ws, v_ws_len,
|
||||
"RGET WHITESPACE");
|
||||
|
||||
/* 2) 引号与反斜杠:测试是否被错误转义 */
|
||||
const char *v_quote = "he\"llo\\world'!";
|
||||
/* 字节数:h e " l l o \ w o r l d ' ! = 15 */
|
||||
int v_quote_len = 15;
|
||||
|
||||
testcase(connfd, KVS_CMD_RSET,
|
||||
"quote", 5,
|
||||
v_quote, v_quote_len,
|
||||
KVS_STATUS_OK,
|
||||
NULL, 0,
|
||||
"RSET QUOTE BACKSLASH");
|
||||
|
||||
testcase(connfd, KVS_CMD_RGET,
|
||||
"quote", 5,
|
||||
NULL, 0,
|
||||
KVS_STATUS_OK,
|
||||
v_quote, v_quote_len,
|
||||
"RGET QUOTE BACKSLASH");
|
||||
|
||||
|
||||
/* 3) 分隔符字符:冒号/逗号/分号/竖线 */
|
||||
const char *v_sep = "a:b,c;d|e";
|
||||
int v_sep_len = 9; /* a : b , c ; d | e = 9 */
|
||||
|
||||
testcase(connfd, KVS_CMD_RSET,
|
||||
"sep", 3,
|
||||
v_sep, v_sep_len,
|
||||
KVS_STATUS_OK,
|
||||
NULL, 0,
|
||||
"RSET SEPARATORS");
|
||||
|
||||
testcase(connfd, KVS_CMD_RGET,
|
||||
"sep", 3,
|
||||
NULL, 0,
|
||||
KVS_STATUS_OK,
|
||||
v_sep, v_sep_len,
|
||||
"RGET SEPARATORS");
|
||||
|
||||
/* 4) CRLF:\r\n 组合(最容易把一条请求拆错/响应拼错) */
|
||||
const char *v_crlf = "line1\r\nline2";
|
||||
int v_crlf_len = 12; /* line1(5) + \r(1) + \n(1) + line2(5) = 12 */
|
||||
|
||||
testcase(connfd, KVS_CMD_RSET,
|
||||
"crlf", 4,
|
||||
v_crlf, v_crlf_len,
|
||||
KVS_STATUS_OK,
|
||||
NULL, 0,
|
||||
"RSET CRLF");
|
||||
|
||||
testcase(connfd, KVS_CMD_RGET,
|
||||
"crlf", 4,
|
||||
NULL, 0,
|
||||
KVS_STATUS_OK,
|
||||
v_crlf, v_crlf_len,
|
||||
"RGET CRLF");
|
||||
|
||||
/* 5) 二进制数据:包含 \0,必须按长度处理,不能用 strlen */
|
||||
char v_bin[] = { 'A', 0x00, 'B', 'C', 0x00, 'D' };
|
||||
int v_bin_len = 6;
|
||||
|
||||
testcase(connfd, KVS_CMD_RSET,
|
||||
"bin", 3,
|
||||
v_bin, v_bin_len,
|
||||
KVS_STATUS_OK,
|
||||
NULL, 0,
|
||||
"RSET BINARY WITH NUL");
|
||||
|
||||
testcase(connfd, KVS_CMD_RGET,
|
||||
"bin", 3,
|
||||
NULL, 0,
|
||||
KVS_STATUS_OK,
|
||||
v_bin, v_bin_len,
|
||||
"RGET BINARY WITH NUL");
|
||||
|
||||
/* 6) UTF-8:中文 + emoji(测试多字节) */
|
||||
const char *v_utf8 = "中文🙂";
|
||||
/* "中"(3字节) + "文"(3字节) + "🙂"(4字节) = 10字节 */
|
||||
int v_utf8_len = 10;
|
||||
|
||||
testcase(connfd, KVS_CMD_RSET,
|
||||
"utf8", 4,
|
||||
v_utf8, v_utf8_len,
|
||||
KVS_STATUS_OK,
|
||||
NULL, 0,
|
||||
"RSET UTF8");
|
||||
|
||||
testcase(connfd, KVS_CMD_RGET,
|
||||
"utf8", 4,
|
||||
NULL, 0,
|
||||
KVS_STATUS_OK,
|
||||
v_utf8, v_utf8_len,
|
||||
"RGET UTF8");
|
||||
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc != 4) {
|
||||
@@ -336,33 +500,40 @@ int main(int argc, char *argv[]) {
|
||||
if(mode == 0){
|
||||
array_testcase_1w(connfd);
|
||||
}else if(mode == 1){
|
||||
rbtree_testcase_1w(connfd);
|
||||
array_testcase_1w_batch(connfd);
|
||||
}else if(mode == 2){
|
||||
hash_testcase_1w(connfd);
|
||||
rbtree_testcase_1w(connfd);
|
||||
}else if(mode == 3){
|
||||
hash_testcase_1w(connfd);
|
||||
}else if(mode == 4){
|
||||
testcase_add2_del1_then_add1_del2_100w(connfd);
|
||||
}else if(mode == 10){
|
||||
do_batch_test(connfd, KVS_CMD_SET, "array_set", "array_val");
|
||||
do_batch_test(connfd, KVS_CMD_SET, "array_set", "array_val", KVS_STATUS_OK, NULL);
|
||||
}else if(mode == 11){
|
||||
do_batch_test(connfd, KVS_CMD_GET, "array_set", NULL);
|
||||
do_batch_test(connfd, KVS_CMD_GET, "array_set", NULL, KVS_STATUS_OK, "array_val");
|
||||
}else if(mode == 12){
|
||||
do_batch_test(connfd, KVS_CMD_EXIST, "array_set", NULL);
|
||||
do_batch_test(connfd, KVS_CMD_EXIST, "array_set", NULL, KVS_STATUS_EXIST, NULL);
|
||||
}else if(mode == 13){
|
||||
do_batch_test(connfd, KVS_CMD_DEL, "array_set", NULL, KVS_STATUS_OK, NULL);
|
||||
}else if(mode == 20){
|
||||
do_batch_test(connfd, KVS_CMD_RSET, "rbtree_set", "rbtree_val");
|
||||
do_batch_test(connfd, KVS_CMD_RSET, "rbtree_set", "rbtree_val", KVS_STATUS_OK, NULL);
|
||||
}else if(mode == 21){
|
||||
do_batch_test(connfd, KVS_CMD_RGET, "rbtree_set", NULL);
|
||||
do_batch_test(connfd, KVS_CMD_RGET, "rbtree_set", NULL, KVS_STATUS_OK, "rbtree_val");
|
||||
}else if(mode == 22){
|
||||
do_batch_test(connfd, KVS_CMD_REXIST, "rbtree_set", NULL);
|
||||
do_batch_test(connfd, KVS_CMD_REXIST, "rbtree_set", NULL, KVS_STATUS_OK, NULL);
|
||||
}else if(mode == 30){
|
||||
do_batch_test(connfd, KVS_CMD_HSET, "hash_set", "hash_val");
|
||||
do_batch_test(connfd, KVS_CMD_HSET, "hash_set", "hash_val", KVS_STATUS_OK, NULL);
|
||||
}else if(mode == 31){
|
||||
do_batch_test(connfd, KVS_CMD_HGET, "hash_set", NULL);
|
||||
do_batch_test(connfd, KVS_CMD_HGET, "hash_set", NULL, KVS_STATUS_OK, "hash_val");
|
||||
}else if(mode == 32){
|
||||
do_batch_test(connfd, KVS_CMD_HEXIST, "hash_set", NULL);
|
||||
do_batch_test(connfd, KVS_CMD_HEXIST, "hash_set", NULL, KVS_STATUS_OK, NULL);
|
||||
}else if(mode == -1){
|
||||
save(connfd);
|
||||
}else if(mode == 4){
|
||||
}else if(mode == 5){
|
||||
batch_qps(connfd);
|
||||
}else if(mode == 6){
|
||||
send_spec_chars(connfd);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user