libevent能做到每秒8万次send?我测试简单的socket程序每次send要0.1毫秒开销,8万次怎么做到?

testtcpsend 发布于 2016/08/30 10:45
阅读 1K+
收藏 0

相关帖子

http://bbs.chinaunix.net/thread-4177202-1-1.html

单服单进程,i7, 16G

libevent能做到每秒8万次send?我测试简单的socket程序每次send要0.1毫秒开销,8万次怎么做到?




加载中
0
渠富林
渠富林

你用的是阻塞的io,试试非阻塞的

0
t
testtcpsend

我就是非阻塞测试的, 附上代码


#include<sys/types.h>
 #include<sys/socket.h>
 #include<netinet/in.h>
 #include<arpa/inet.h>
 #include<unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <strings.h>
 #include<sys/wait.h>
 #include <string.h>
#include <time.h> 
#include <unistd.h>
 #include <fcntl.h>
#include <sys/time.h>
#include <stdio.h>
#include  <netinet/in.h>
 #include <netinet/tcp.h> // for TCP_NODELAY
#include <sys/time.h>
#include <sys/epoll.h>
#include <time.h>

typedef unsigned int            uint32_t; 
	void xsleep(uint32_t ms)
	{ 
	  struct timeval tval;
	  tval.tv_sec	= ms / 1000;
	  tval.tv_usec	= ( ms * 1000) % 1000000;
	  select(0, NULL, NULL, NULL, &tval);
	}	


 int main(int argc, char ** argv) 
 { 
     int sockfd,new_fd;
     struct sockaddr_in my_addr;
     struct sockaddr_in their_addr;
     unsigned int sin_size, myport, lisnum; 
  
     if(argv[1])  myport = atoi(argv[1]); 
     else myport = 50000; 
  
     if(argv[2])  lisnum = atoi(argv[2]); 
     else lisnum = 2; 
  
     if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) { 
         perror("socket"); 
         exit(1); 
     } 
  printf("socket %d ok \n",myport);
//fcntl(sockfd, F_SETFL, O_NONBLOCK);
    my_addr.sin_family=PF_INET; 
     my_addr.sin_port=htons(myport); 
     my_addr.sin_addr.s_addr = INADDR_ANY; 
     bzero(&(my_addr.sin_zero), 0); 
     if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { 
         perror("bind"); 
         exit(1); 
     } 
  printf("bind ok \n");
  
     if (listen(sockfd, lisnum) == -1) { 
         perror("listen"); 
         exit(1); 
     }
  printf("listen ok \n");
  


    sin_size = sizeof(struct sockaddr_in); 

xxx:
     if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) { 
         perror("accept"); 
   goto xxx;
     } 



     printf("server: got connection from %s\n",inet_ntoa(their_addr.sin_addr));
fcntl(new_fd, F_SETFL, O_NONBLOCK);
int arg = 1;
setsockopt(new_fd, IPPROTO_TCP, TCP_NODELAY, (char*)&arg, sizeof(int));
int nSendBuf=1024*1024*16;//¨¦??32K
setsockopt(new_fd,SOL_SOCKET,SO_SNDBUF,(const char*)&nSendBuf,sizeof(int)); 


int epfd;
struct epoll_event ev,events[20];
epfd=epoll_create(256);

ev.data.fd=sockfd;
ev.events=EPOLLIN|EPOLLOUT;
epoll_ctl(epfd,EPOLL_CTL_ADD,sockfd,&ev);

ev.data.fd=new_fd;
ev.events=EPOLLIN|EPOLLOUT;
epoll_ctl(epfd,EPOLL_CTL_ADD,new_fd,&ev);



 int   step = 0;
   char  szSnd[1024] = {0};
   memset(szSnd,97,1024);
szSnd[100] = 0;

//time_t lasttime = time(NULL);
struct timeval lasttime;
gettimeofday(&lasttime,0);
double alltimeuse = 0.0;


int iii = 0;
while(iii++<100)
{
struct timeval lasttime;
gettimeofday(&lasttime,0);

 if (send(new_fd, szSnd, strlen(szSnd), 0) == -1) {
     perror("send");
 }


struct timeval now_time;
gettimeofday(&now_time,0);
double timeuse = 1000000.0*(now_time.tv_sec - lasttime.tv_sec) + now_time.tv_usec - lasttime.tv_usec;
timeuse /=1000.0;
 printf("xxsend msg: %f\n",(timeuse));



{
struct timeval lasttime;
gettimeofday(&lasttime,0);

 if (send(new_fd, szSnd, strlen(szSnd), 0) == -1) {
     perror("send");
 }


struct timeval now_time;
gettimeofday(&now_time,0);
double timeuse = 1000000.0*(now_time.tv_sec - lasttime.tv_sec) + now_time.tv_usec - lasttime.tv_usec;
timeuse /=1000.0;
 printf("xxsend1 msg: %f\n",(timeuse));
}


//xsleep(100);
usleep(1000);
};

 exit(0); 
 }
 
结果是:
xxsend msg: 0.327000
xxsend1 msg: 0.126000
xxsend msg: 0.058000
xxsend1 msg: 0.041000
xxsend msg: 0.028000
xxsend1 msg: 0.002000
xxsend msg: 0.929000
xxsend1 msg: 0.002000
xxsend msg: 0.045000
xxsend1 msg: 0.002000
xxsend msg: 0.068000
xxsend1 msg: 0.045000
xxsend msg: 0.054000
xxsend1 msg: 0.001000
xxsend msg: 0.044000
xxsend1 msg: 0.040000
xxsend msg: 0.029000
xxsend1 msg: 0.023000
xxsend msg: 3.455000
xxsend1 msg: 0.002000
xxsend msg: 0.050000
xxsend1 msg: 0.028000
xxsend msg: 0.061000
xxsend1 msg: 0.002000
xxsend msg: 0.049000
xxsend1 msg: 0.036000
xxsend msg: 0.179000
xxsend1 msg: 0.035000
xxsend msg: 0.048000
xxsend1 msg: 0.001000
xxsend msg: 0.058000
xxsend1 msg: 0.012000
xxsend msg: 0.053000
xxsend1 msg: 0.001000
xxsend msg: 0.065000
xxsend1 msg: 0.001000
xxsend msg: 0.030000


0
t
testtcpsend
1测试结果:可见第一次send很慢, 第二次send1很快, 如果while循环一直连续send就超快, 几十万次的确小意思, 但是一旦期间有其他代码执行下一次send就超慢, 在现实业务中肯定send完之后要做其他事情, 为何会这样?
0
返回顶部
顶部