- 论坛徽章:
- 1
|
服务器是回射服务器,现在客户端只建一个socket连服务器,
想测试一下服务器的最大tps和平均时延,只测一个socket的情况,
我现在的客服端是这样做的:
开一个线程专门读数据,主线程专门发数据,自己指定发多少
条就退出。
请教一下,这样测对不对呢,怎样测才比较好呢,大家帮忙看下啊,感谢。
发送数据的结构体:
typedef struct
{
struct timeval begin;
struct timeval end;
char buf[2048];
}data_t;
main函数:
time_t begin = time(NULL);
pthread_t sender;
pthread_create(&sender, NULL, read_data, &con_fd);//同一个套接字,con_fd
send_data(con_fd);
pthread_join(sender, NULL);
time_t end = time(NULL);
unsigned int cost = end - begin;
线程读函数:
void* read_data(void *arg)
{
int fd = *(int *)arg;
int ret = 0;
data_t buf_read;
int cost = 0;
fd_set readset;
fd_set set;
FD_ZERO(&readset);
FD_SET(fd,&readset);
while(1)
{
set = readset;
ret = select(fd+1, &set, NULL, NULL, NULL);
if(ret <= 0) continue;
memset(&buf_read, 0, sizeof(buf_read));
ret = my_write(fd, (char *)&buf_read, sizeof(data_t)); //循环读完
if(ret < 0)
{
fails++;
continue;
}
gettimeofday(&buf_read.end, NULL);
cost = (buf_read.end.tv_sec - buf_read.begin.tv_sec)*1000000 + buf_read.end.tv_usec - buf_read.begin.tv_usec;
if(cost >= 1000000) time_outs++;
total_time += cost;
if(++reads == sent_nums) return NULL;
}
}
还有一个发送的函数,和读函数差不多了。
|
|