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

发布于 2021-12-02 17:47:09 字数 210 浏览 771 评论 3

相关帖子

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

单服单进程,i7, 16G

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




如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜血缘 2021-12-03 22:23:29

高手何在

感情旳空白 2021-12-03 21:18:18

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

陌上芳菲 2021-12-03 19:54:27

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

#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 %sn",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: %fn",(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: %fn",(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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文