326 lines
9.3 KiB
C
326 lines
9.3 KiB
C
|
||
#include "test_client.h"
|
||
#include <arpa/inet.h>
|
||
|
||
|
||
int connect_tcpserver(const char *ip, unsigned short port) {
|
||
|
||
int connfd = socket(AF_INET, SOCK_STREAM, 0);
|
||
|
||
struct sockaddr_in server_addr;
|
||
memset(&server_addr, 0, sizeof(struct sockaddr_in));
|
||
|
||
server_addr.sin_family = AF_INET;
|
||
server_addr.sin_addr.s_addr = inet_addr(ip);
|
||
server_addr.sin_port = htons(port);
|
||
|
||
if (0 != connect(connfd, (struct sockaddr*)&server_addr, sizeof(struct sockaddr_in))) {
|
||
perror("connect");
|
||
return -1;
|
||
}
|
||
|
||
return connfd;
|
||
|
||
}
|
||
|
||
|
||
int send_msg(int connfd, char *msg, int length) {
|
||
|
||
int res = send(connfd, msg, length, 0);
|
||
if (res < 0) {
|
||
perror("send");
|
||
exit(1);
|
||
}
|
||
return res;
|
||
}
|
||
|
||
int recv_msg(int connfd, char *msg, int length) {
|
||
|
||
int res = recv(connfd, msg, length, 0);
|
||
if (res < 0) {
|
||
perror("recv");
|
||
exit(1);
|
||
}
|
||
return res;
|
||
|
||
}
|
||
|
||
void testcase(int connfd, uint8_t op, const char* key, uint32_t key_len, const char* value,
|
||
uint32_t value_len, rsp_ret_status_e st, const char* rsp_value, uint32_t expect_len, const char* command_name){
|
||
uint8_t buf[CMD_SIZE];
|
||
uint8_t result[CMD_SIZE];
|
||
kvs_response_t rsp;
|
||
int len, recv_len;
|
||
|
||
len = getcmd(op, key, key_len, value, value_len, buf);
|
||
send_msg(connfd, buf, len);
|
||
recv_len = recv_msg(connfd, result, CMD_SIZE);
|
||
if (parse_response(result, recv_len, &rsp) > 0) {
|
||
PRESP(command_name, &rsp);
|
||
if(!verify_response(&rsp, op, st, rsp_value, expect_len)) printf("%s\n", command_name);
|
||
}else{
|
||
printf("parser error\n");
|
||
}
|
||
|
||
return ;
|
||
}
|
||
|
||
|
||
void array_testcase_1w(int connfd) {
|
||
|
||
int count = 1;
|
||
int i = 0;
|
||
|
||
struct timeval tv_begin;
|
||
gettimeofday(&tv_begin, NULL);
|
||
|
||
for (i = 0;i < count;i ++) {
|
||
testcase(connfd, KVS_CMD_SET, "name", 4, "l\r\0n", 4, KVS_STATUS_OK, NULL, 0, "SET NAME");
|
||
testcase(connfd, KVS_CMD_GET, "name", 4, NULL, 0, KVS_STATUS_OK, "l\r\0n", 4, "GET NAME");
|
||
testcase(connfd, KVS_CMD_MOD, "name", 4, "liu", 3, KVS_STATUS_OK, NULL, 0, "MOD NAME");
|
||
testcase(connfd, KVS_CMD_GET, "name", 4, NULL, 0, KVS_STATUS_OK, "liu", 3, "GET NAME");
|
||
testcase(connfd, KVS_CMD_EXIST, "name", 4, NULL, 0, KVS_STATUS_EXIST, NULL, 0, "EXIST NAME");
|
||
testcase(connfd, KVS_CMD_DEL, "name", 4, NULL, 0, KVS_STATUS_OK, NULL, 0, "DEL NAME");
|
||
testcase(connfd, KVS_CMD_EXIST, "name", 4, NULL, 0, KVS_STATUS_NO_EXIST, NULL, 0, "EXIST NAME");
|
||
|
||
testcase(connfd, KVS_CMD_MOD, "stu", 3, "liu", 3, KVS_STATUS_NO_EXIST, NULL, 0, "MOD NAME");
|
||
testcase(connfd, KVS_CMD_DEL, "stu", 3, NULL, 0, KVS_STATUS_NO_EXIST, NULL, 0, "DEL SUT");
|
||
}
|
||
|
||
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 rbtree_testcase_1w(int connfd) {
|
||
|
||
int count = 1;
|
||
int i = 0;
|
||
|
||
struct timeval tv_begin;
|
||
gettimeofday(&tv_begin, NULL);
|
||
|
||
for (i = 0;i < count;i ++) {
|
||
testcase(connfd, KVS_CMD_RSET, "name", 4, "l\r\0n", 4, KVS_STATUS_OK, NULL, 0, "RSET NAME");
|
||
testcase(connfd, KVS_CMD_RGET, "name", 4, NULL, 0, KVS_STATUS_OK, "l\r\0n", 4, "RGET NAME");
|
||
testcase(connfd, KVS_CMD_RMOD, "name", 4, "liu", 3, KVS_STATUS_OK, NULL, 0, "RMOD NAME");
|
||
testcase(connfd, KVS_CMD_RGET, "name", 4, NULL, 0, KVS_STATUS_OK, "liu", 3, "RGET NAME");
|
||
testcase(connfd, KVS_CMD_REXIST, "name", 4, NULL, 0, KVS_STATUS_EXIST, NULL, 0, "REXIST NAME");
|
||
testcase(connfd, KVS_CMD_RDEL, "name", 4, NULL, 0, KVS_STATUS_OK, NULL, 0, "RDEL NAME");
|
||
testcase(connfd, KVS_CMD_REXIST, "name", 4, NULL, 0, KVS_STATUS_NO_EXIST, NULL, 0, "REXIST NAME");
|
||
|
||
testcase(connfd, KVS_CMD_RMOD, "stu", 3, "liu", 3, KVS_STATUS_NO_EXIST, NULL, 0, "RMOD NAME");
|
||
testcase(connfd, KVS_CMD_RDEL, "stu", 3, NULL, 0, KVS_STATUS_NO_EXIST, NULL, 0, "RDEL SUT");
|
||
}
|
||
|
||
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 hash_testcase_1w(int connfd) {
|
||
|
||
int count = 1;
|
||
int i = 0;
|
||
|
||
struct timeval tv_begin;
|
||
gettimeofday(&tv_begin, NULL);
|
||
|
||
for (i = 0;i < count;i ++) {
|
||
testcase(connfd, KVS_CMD_HSET, "name", 4, "l\r\0n", 4, KVS_STATUS_OK, NULL, 0, "HSET NAME");
|
||
testcase(connfd, KVS_CMD_HGET, "name", 4, NULL, 0, KVS_STATUS_OK, "l\r\0n", 4, "HGET NAME");
|
||
testcase(connfd, KVS_CMD_HMOD, "name", 4, "liu", 3, KVS_STATUS_OK, NULL, 0, "HMOD NAME");
|
||
testcase(connfd, KVS_CMD_HGET, "name", 4, NULL, 0, KVS_STATUS_OK, "liu", 3, "HGET NAME");
|
||
testcase(connfd, KVS_CMD_HEXIST, "name", 4, NULL, 0, KVS_STATUS_EXIST, NULL, 0, "HEXIST NAME");
|
||
testcase(connfd, KVS_CMD_HDEL, "name", 4, NULL, 0, KVS_STATUS_OK, NULL, 0, "HDEL NAME");
|
||
testcase(connfd, KVS_CMD_HEXIST, "name", 4, NULL, 0, KVS_STATUS_NO_EXIST, NULL, 0, "HEXIST NAME");
|
||
|
||
testcase(connfd, KVS_CMD_HMOD, "stu", 3, "liu", 3, KVS_STATUS_NO_EXIST, NULL, 0, "HMOD NAME");
|
||
testcase(connfd, KVS_CMD_HDEL, "stu", 3, NULL, 0, KVS_STATUS_NO_EXIST, NULL, 0, "HDEL SUT");
|
||
}
|
||
|
||
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 do_batch_SET_example(int fd)
|
||
// {
|
||
// kvs_batch_t batch;
|
||
// kvs_batch_init(&batch);
|
||
|
||
// char key[10]={0}, val[10]={0};
|
||
|
||
// // 组 batch(最多 64 条)
|
||
// for(int i = 0;i < 48; ++ i){
|
||
// int len = sprintf(key, "k%d", i);
|
||
// len = sprintf(val, "v%d", i);
|
||
// kvs_batch_add(&batch, KVS_CMD_SET, key, val);
|
||
// }
|
||
|
||
// // 一次性发送
|
||
// kvs_batch_send(fd, &batch);
|
||
|
||
// // 一次性 recv + parse
|
||
// uint8_t recvbuf[BATCH_SIZE];
|
||
// kvs_response_t rsps[KVS_BATCH_MAX];
|
||
|
||
// int nrsp = kvs_batch_recv_parse(fd, &batch, rsps, recvbuf, sizeof(recvbuf));
|
||
|
||
// // 打印/处理
|
||
// for (int i = 0; i < nrsp; i++) {
|
||
// int len = sprintf(key, "SET%d", i);
|
||
// PRESP(key, &rsps[i]);
|
||
// }
|
||
// }
|
||
|
||
// void do_batch_GET_example(int fd)
|
||
// {
|
||
// kvs_batch_t batch;
|
||
// kvs_batch_init(&batch);
|
||
|
||
// char key[10]={0}, val[10]={0};
|
||
|
||
// // 组 batch(最多 64 条)
|
||
// for(int i = 0;i < 48; ++ i){
|
||
// int len = sprintf(key, "k%d", i);
|
||
// kvs_batch_add(&batch, KVS_CMD_GET, key, NULL);
|
||
// }
|
||
|
||
// // 一次性发送
|
||
// kvs_batch_send(fd, &batch);
|
||
|
||
// // 一次性 recv + parse
|
||
// uint8_t recvbuf[BATCH_SIZE];
|
||
// kvs_response_t rsps[KVS_BATCH_MAX];
|
||
|
||
// int nrsp = kvs_batch_recv_parse(fd, &batch, rsps, recvbuf, sizeof(recvbuf));
|
||
|
||
// // 打印/处理
|
||
// for (int i = 0; i < nrsp; i++) {
|
||
// int len = sprintf(key, "GET%d", i);
|
||
// PRESP(key, &rsps[i]);
|
||
// }
|
||
// }
|
||
|
||
// void do_batch_DEL_example(int fd)
|
||
// {
|
||
// kvs_batch_t batch;
|
||
// kvs_batch_init(&batch);
|
||
|
||
// char key[10]={0}, val[10]={0};
|
||
|
||
// // 组 batch(最多 64 条)
|
||
// for(int i = 0;i < 48; ++ i){
|
||
// int len = sprintf(key, "k%d", i);
|
||
// kvs_batch_add(&batch, KVS_CMD_DEL, key, NULL);
|
||
// }
|
||
|
||
// // 一次性发送
|
||
// kvs_batch_send(fd, &batch);
|
||
|
||
// // 一次性 recv + parse
|
||
// uint8_t recvbuf[BATCH_SIZE];
|
||
// kvs_response_t rsps[KVS_BATCH_MAX];
|
||
|
||
// int nrsp = kvs_batch_recv_parse(fd, &batch, rsps, recvbuf, sizeof(recvbuf));
|
||
|
||
// // 打印/处理
|
||
// for (int i = 0; i < nrsp; i++) {
|
||
// int len = sprintf(key, "DEL%d", i);
|
||
// PRESP(key, &rsps[i]);
|
||
// }
|
||
// }
|
||
|
||
void do_batch_test(int fd, int op, const char *key, const char *value){
|
||
kvs_batch_t batch;
|
||
kvs_batch_init(&batch);
|
||
|
||
char bkey[15]={0}, bval[15]={0};
|
||
|
||
// 组 batch(最多 64 条)
|
||
for(int i = 0;i < 48; ++ i){
|
||
if(value == NULL){
|
||
int klen = sprintf(bkey, "%s%d", key, i);
|
||
kvs_batch_add(&batch, op, bkey, klen, NULL, 0);
|
||
}else{
|
||
int klen = sprintf(bkey, "%s%d", key, i);
|
||
int vlen = sprintf(bval, "%s%d", value, i);
|
||
kvs_batch_add(&batch, op, bkey, klen, bval, vlen);
|
||
}
|
||
}
|
||
|
||
// 一次性发送
|
||
kvs_batch_send(fd, &batch);
|
||
|
||
// 一次性 recv + parse
|
||
uint8_t recvbuf[BATCH_SIZE];
|
||
kvs_response_t rsps[KVS_BATCH_MAX];
|
||
|
||
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]);
|
||
}
|
||
}
|
||
|
||
void save(int connfd){
|
||
testcase(connfd, KVS_CMD_SAVE, NULL, 0, NULL, 0, KVS_STATUS_OK, NULL, 0, "SAVE");
|
||
}
|
||
|
||
|
||
int main(int argc, char *argv[]) {
|
||
if (argc != 4) {
|
||
printf("arg error\n");
|
||
return -1;
|
||
}
|
||
|
||
char *ip = argv[1];
|
||
int port = atoi(argv[2]);
|
||
int mode = atoi(argv[3]);
|
||
|
||
int connfd = connect_tcpserver(ip, port);
|
||
|
||
if(mode == 0){
|
||
array_testcase_1w(connfd);
|
||
}else if(mode == 1){
|
||
rbtree_testcase_1w(connfd);
|
||
}else if(mode == 2){
|
||
hash_testcase_1w(connfd);
|
||
}else if(mode == 10){
|
||
do_batch_test(connfd, KVS_CMD_SET, "array_set", "array_val");
|
||
}else if(mode == 11){
|
||
do_batch_test(connfd, KVS_CMD_GET, "array_set", NULL);
|
||
}else if(mode == 12){
|
||
do_batch_test(connfd, KVS_CMD_EXIST, "array_set", NULL);
|
||
}else if(mode == 20){
|
||
do_batch_test(connfd, KVS_CMD_RSET, "rbtree_set", "rbtree_val");
|
||
}else if(mode == 21){
|
||
do_batch_test(connfd, KVS_CMD_RGET, "rbtree_set", NULL);
|
||
}else if(mode == 22){
|
||
do_batch_test(connfd, KVS_CMD_REXIST, "rbtree_set", NULL);
|
||
}else if(mode == 30){
|
||
do_batch_test(connfd, KVS_CMD_HSET, "hash_set", "hash_val");
|
||
}else if(mode == 31){
|
||
do_batch_test(connfd, KVS_CMD_HGET, "hash_set", NULL);
|
||
}else if(mode == 32){
|
||
do_batch_test(connfd, KVS_CMD_HEXIST, "hash_set", NULL);
|
||
}else if(mode == -1){
|
||
save(connfd);
|
||
}
|
||
return 0;
|
||
} |