简化协议,
/**
* Request
* Cmd: | OP(1) | argc(1) | repeat { arglen(4) | arg } |
*
* Response
* Rsp: | OP(1) | status(1) | datalen(4) | data |
*/
封装客户端进行批处理和单条命令测试。
This commit is contained in:
143
test/testcase.c
Normal file
143
test/testcase.c
Normal file
@@ -0,0 +1,143 @@
|
||||
|
||||
#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, const char* value, rsp_ret_status_e st, const char* rsp_value, 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, value, 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)) 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, "nage", "lian", KVS_STATUS_OK, NULL, "SET NAME");
|
||||
testcase(connfd, KVS_CMD_GET, "nage", NULL, KVS_STATUS_OK, "lian", "GET NAME");
|
||||
testcase(connfd, KVS_CMD_MOD, "nage", "liu", KVS_STATUS_OK, NULL, "MOD NAME");
|
||||
testcase(connfd, KVS_CMD_GET, "nage", NULL, KVS_STATUS_OK, "liu", "GET NAME");
|
||||
|
||||
testcase(connfd, KVS_CMD_EXIST, "nage", NULL, KVS_STATUS_EXIST, NULL, "EXIST NAME");
|
||||
testcase(connfd, KVS_CMD_DEL, "nage", NULL, KVS_STATUS_OK, NULL, "DEL NAME");
|
||||
testcase(connfd, KVS_CMD_EXIST, "nage", NULL, KVS_STATUS_NO_EXIST, NULL, "NOT EXIST NAME");
|
||||
}
|
||||
|
||||
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, 70000 * 1000 / time_used);
|
||||
|
||||
}
|
||||
|
||||
void do_batch_example(int fd)
|
||||
{
|
||||
kvs_batch_t batch;
|
||||
kvs_batch_init(&batch);
|
||||
|
||||
// 组 batch(最多 64 条)
|
||||
kvs_batch_add(&batch, KVS_CMD_SET, "k1", "v1");
|
||||
kvs_batch_add(&batch, KVS_CMD_SET, "k2", "v2");
|
||||
kvs_batch_add(&batch, KVS_CMD_GET, "k1", NULL);
|
||||
kvs_batch_add(&batch, KVS_CMD_GET, "k2", 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++) {
|
||||
PRESP("BATCH", &rsps[i]);
|
||||
}
|
||||
|
||||
printf("%d\n", nrsp);
|
||||
|
||||
testcase(fd, KVS_CMD_GET, "k1", NULL, KVS_STATUS_OK, "v1", "GET k1");
|
||||
testcase(fd, KVS_CMD_GET, "k2", NULL, KVS_STATUS_OK, "v2", "GET k2");
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc != 3) {
|
||||
printf("arg error\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *ip = argv[1];
|
||||
int port = atoi(argv[2]);
|
||||
|
||||
int connfd = connect_tcpserver(ip, port);
|
||||
|
||||
// array_testcase_1w(connfd);
|
||||
do_batch_example(connfd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user