代码:

  1 /*
  2  * g++ -o stress_test ./stress_test.cpp
  3  */
  4
  5 #include <stdlib.h>
  6 #include <stdio.h>
  7 #include <string.h>
  8 #include <errno.h>
  9
 10 #include <unistd.h>
 11 #include <sys/types.h>
 12 #include <sys/epoll.h>
 13 #include <fcntl.h>
 14 #include <sys/socket.h>
 15 #include <netinet/in.h>
 16 #include <arpa/inet.h>
 17
 18 static const char *request = "GET / HTTP/1.1\r\nHost: 192.168.1.5\r\nConnection: keep-alive\r\n\r\n";
 19
 20 int setnonblocking(int fd)
 21 {
 22     int old_option = fcntl(fd, F_GETFL);
 23     int new_option = old_option | O_NONBLOCK;
 24     fcntl(fd, F_SETFL, new_option);
 25
 26     return old_option;
 27 }
 28
 29 int add_fd(int epoll_fd, int fd)
 30 {
 31     struct epoll_event event;
 32     event.events = EPOLLOUT | EPOLLET | EPOLLERR;
 33     event.data.fd = fd;
 34     epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &event);
 35     setnonblocking(fd);
 36 }
 37
 38 bool write_nbytes(int sockfd, const char *buffer, int len)
 39 {
 40     int bytes_write = 0;
 41     printf("write out %d bytes to socket %d\n", len, sockfd);
 42     while(1)
 43     {
 44         bytes_write = send(sockfd, buffer, len, 0);
 45         if (bytes_write == -1)
 46         {
 47             printf("send failed, errno[%d], error[%s]\n", errno, strerror(errno));
 48             return false;
 49         }
 50         else if (bytes_write == 0)
 51         {
 52             printf("send 0 bytes\n");
 53             return false;
 54         }
 55
 56         len -= bytes_write;
 57         buffer = buffer + bytes_write;
 58         if (len <= 0)
 59         {
 60             return true;
 61         }
 62     }
 63 }
 64
 65 bool read_once(int sockfd, char *buffer, int len)
 66 {
 67     int bytes_read = 0;
 68     memset(buffer, '\0', len);
 69     bytes_read = recv(sockfd, buffer, len, 0);
 70     if (bytes_read == -1)
 71     {
 72         printf("recv failed, errno[%d], error[%s]\n", errno, strerror(errno));
 73         return false;
 74     }
 75     else if (bytes_read == 0)
 76     {
 77         printf("recv 0 bytes\n");
 78         return false;
 79     }
 80     printf("read in %d bytes from socket %d\n", bytes_read, sockfd);
 81
 82     return true;
 83 }
 84
 85 int start_conn(int epoll_fd, const char *ip, int port, int num)
 86 {
 87     struct sockaddr_in addr;
 88     bzero(&addr, sizeof(addr));
 89     addr.sin_family = AF_INET;
 90     addr.sin_addr.s_addr = inet_addr(ip);
 91     addr.sin_port = htons(port);
 92
 93     socklen_t len = sizeof(addr);
 94     for (int i = 0; i < num; ++i)
 95     {
 96         sleep(1);
 97         int fd = socket(AF_INET, SOCK_STREAM, 0);
 98         if (fd == -1)
 99         {
100             printf("socket failed, errno[%d], error[%s]\n", errno, strerror(errno));
101             continue;
102         }
103
104         if (connect(fd, (struct sockaddr*)&addr, len) == 0)
105         {
106             printf("build connection %d\n", i);
107             add_fd(epoll_fd, fd);
108         }
109         else
110         {
111             printf("connect failed, errno[%d], error[%s]\n", errno, strerror(errno));
112             continue;
113         }
114     }
115 }
116
117 int close_conn(int epoll_fd, int fd)
118 {
119     epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, NULL);
120
121     printf("close socket[%d]\n", fd);
122     close(fd);
123 }
124
125 int main(int argc, char **argv)
126 {
127     if (argc != 4)
128     {
129         printf("usage:stress_test ip port num\n");
130         exit(-1);
131     }
132
133     int epoll_fd = epoll_create(10000);
134     start_conn(epoll_fd, argv[1], atoi(argv[2]), atoi(argv[3]));
135     epoll_event events[10000];
136     char buffer[2048];
137     while (1)
138     {
139         int fds = epoll_wait(epoll_fd, events, 10000, 2000);
140         for (int i = 0; i < fds; ++i)
141         {
142             int sockfd = events[i].data.fd;
143             if (events[i].events & EPOLLIN)
144             {
145                 if (!read_once(sockfd, buffer, 2048))
146                 {
147                     close_conn(epoll_fd, sockfd);
148                 }
149
150                 struct epoll_event event;
151                 event.events = EPOLLOUT | EPOLLET | EPOLLERR;
152                 event.data.fd = sockfd;
153                 epoll_ctl(epoll_fd, EPOLL_CTL_MOD, sockfd, &event);
154             }
155             else if (events[i].events & EPOLLOUT)
156             {
157                 if (!write_nbytes(sockfd, request, strlen(request)))
158                 {
159                     close_conn(epoll_fd, sockfd);
160                 }
161
162                 struct epoll_event event;
163                 event.events = EPOLLIN | EPOLLET | EPOLLERR;
164                 event.data.fd = sockfd;
165                 epoll_ctl(epoll_fd, EPOLL_CTL_MOD, sockfd, &event);
166             }
167             else if (events[i].events & EPOLLERR)
168             {
169                 close_conn(epoll_fd, sockfd);
170             }
171         }
172     }
173
174     return 0;
175 }

转载于:https://www.cnblogs.com/lit10050528/p/5808289.html

epoll实现压测工具相关推荐

  1. Http压测工具wrk使用指南【转】

    用过了很多压测工具,却一直没找到中意的那款.最近试了wrk感觉不错,写下这份使用指南给自己备忘用,如果能帮到你,那也很好. 安装 wrk支持大多数类UNIX系统,不支持windows.需要操作系统支持 ...

  2. Http压测工具wrk使用指南

    用过了很多压测工具,却一直没找到中意的那款.最近试了wrk感觉不错,写下这份使用指南给自己备忘用,如果能帮到你,那也很好. 安装 wrk支持大多数类UNIX系统,不支持windows.需要操作系统支持 ...

  3. Web 压测工具介绍

    Web 压测工具 web benchmark tools Apache Bench ApacheBench 是一个用来衡量http服务器性能的单线程命令行工具.原本针对Apache http服务器,但 ...

  4. 性能压测工具:wrk

    一般我们压测的时候,需要了解衡量系统性能的一些参数指标,比如. 1.系统性能的维度 1.1 延迟 简单易懂.green:一般指响应时间 95线:P95.平均100%的请求中95%已经响应的时间 99线 ...

  5. HTTP压测工具Wrk 介绍和使用

    介绍 wrk是一款简单的HTTP压测工具,托管在Github上,https://github.com/wg/wrk. wrk 的一个很好的特性就是能用很少的线程压出很大的并发量. 原因是它使用了一些操 ...

  6. 技术丨压测工具wrk和Artillery的比较

    本文作者:Linkflow首席架构师 – 王鼎,11年软件研发经验,6年SaaS(基于公有云或私有云),熟悉ERP, CDP, omin渠道销售解决方案.参与SaaS产品的大型开发,成员400余人.在 ...

  7. 压测工具wrk和Artillery的比较

    这两天抽空使用了一下两款压测工具 wrk Artillery 并且通过两款工具对产品的两个环境进行了测试 工具比较 wrk wrk自身性能就非常惊人,使用epoll这种多路复用技术,所以可以用少量的线 ...

  8. 基准测试工具(压测工具):wrk---高并发、损耗低,安装简单 (一)

    基准测试工具:Wrk初识   最近和同事聊起常用的一些压测工具,谈到了Apache ab.阿里云的PTS.Jmeter.Locust以及wrk各自的一些优缺点和适用的场景类型. 这篇博客,简单介绍下H ...

  9. http接口压测工具wrk

    wrk是一款简单的HTTP压测工具,当运行在单个多核CPU上时,它能够产生巨大的负载. github:https://github.com/wg/wrk 国内镜像: https://gitee.com ...

最新文章

  1. LeetCode26. Remove Duplicates from Sorted Array
  2. 设置Dialog全屏显示(转)
  3. ubuntu中获取文件名称并生成txt文件
  4. unittest-读取yaml文件
  5. 使用TensorFlow.js的AI聊天机器人二:训练Trivia Expert AI
  6. 记一次mongdb搭建复制集的小故障
  7. 分类损失函数多元分类_二元分类为什么不能用MSE做为损失函数?
  8. 捷联惯导系统学习4.2(捷联惯导误差方程)
  9. 毕设项目 - 基于SSM的教师工作考核绩效管理系统(含源码+论文)
  10. javascript弹窗和基本数据类型
  11. npm publish 报错 403
  12. FunCoolShell
  13. zotero配合坚果云和papership进行文献管理
  14. 笛卡尔坐标为什么叫Cartesian coordinate而不是Descartes coordinate?
  15. 史上最全推广小程序实操方法
  16. 52brain公众号目录【2020年3月】
  17. [乱搞 树状数组] BZOJ 4548 小奇的糖果 BZOJ 3658 Jabberwocky
  18. Windows系统各个版本的ISO镜像下载地址
  19. oracle导出表为excel文件路径,Oracle导出数据为excel或文本文件
  20. 两大数据库安全产品比拼:IBM Guardium VS Imperva SecureSpher

热门文章

  1. sqlplus操作--文件的输入与输出
  2. 对 /sbin/nologin 的理解
  3. java多线程之线程的同步与锁定(转)
  4. The Zen of Python, by Tim Peters
  5. Android获取存储卡路径的方式(转)
  6. Skype通讯协议分析
  7. ubuntu不启动图形界面
  8. vim E492: Not an editor command: ^M
  9. python3 for循环怎么用_Python3入门系列之-----循环语句(for/while)
  10. 单词九连猜python编程_python实现猜单词游戏