系列服务器开发


文章目录

  • 系列服务器开发
  • 前言
  • 一、ab是什么?
  • 二、ab测试实例nginx
    • 1.nginx环境准备与安装
    • 2.ab测试nginx本身的性能
    • 3.ab测试基于brpc的http服务器性能
  • 三、ab实战之常见问题解决
  • 总结

前言


一、ab是什么?

ab全称为:apache bench,官方注释为:
Apache超文本传输协议(HTTP)的性能测试工具。其设计意图是描绘当前所安装的Apache的执行性能,主要是显示你安装的Apache每秒可以处理多少个请求。apache自带的压力测试工具。ab非常实用,它不仅可以对apache服务器进行网站访问压力测试,也可以对或其它类型的服务器进行压力测试。比如nginx、tomcat、IIS等。

其他几款轻量级的压测工具如siege http_load ab webbench.siege等都太吃内存(在相同的请求数与并发数下,ab相对而言耗资源较少)。

centos安装ab

安装:
centos安装:
yum install httpd-tools
linux安装:
apt-get install apache2-utils测试方法
./ab [options] [url]ab常用参数的介绍:
-n:总共的请求执行数,缺省是1;
-c:并发数,缺省是1;
-t:测试所进行的总时间,秒为单位,缺省50000s
-p:POST时的数据文件(本地存储json文件等)
-w: 以HTML表的格式输出结果
-T:payload数据格式'application/json'如:
ab -n 200 -c 10 "http://jd.com/"
如:
ab -n 200 -c 100 http://localhost/index.html

ab测试返回结果字段解析

Server Software:        web服务器软件及版本
Server Hostname:        请求的地址
Server Port:            请求的端口Document Path:          请求的页面路径
Document Length:        页面大小Concurrency Level:      并发数
Time taken for tests:   测试总共花费的时间
Complete requests:      完成的请求数
Failed requests:        失败的请求数
Write errors:           写入错误
Total transferred:      总共传输字节数,包含http的头信息等
HTML transferred:       html字节数,实际的页面传递字节数
Requests per second:    每秒处理的请求数,服务器的吞吐量(重要)
Time per request:       平均数,用户平均请求等待时间
Time per request:       服务器平均处理时间
Transfer rate:          平均传输速率(每秒收到的速率)

二、ab测试实例nginx

1.nginx环境准备与安装

1、安装nginx
2、登陆http://192.168.31.91或者http://localhost/index.html, 页面如下表示安装成功

3、nginx conf内容如下
默认:
1 worker_processes
1024 worker_connections

[root@localhost conf]# cat nginx.conf#user  nobody;
worker_processes  1;#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {worker_connections  1024;
}http {include       mime.types;default_type  application/octet-stream;#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '#                  '$status $body_bytes_sent "$http_referer" '#                  '"$http_user_agent" "$http_x_forwarded_for"';#access_log  logs/access.log  main;sendfile        on;#tcp_nopush     on;#keepalive_timeout  0;keepalive_timeout  65;#gzip  on;server {listen       80;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;location / {root   html;index  index.html index.htm;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}
}

2.ab测试nginx本身的性能

服务器上本机测试:连接数200000 ,请求数1000
ab -n 200000 -c 1000 http://localhost/index.html
测试结果如下(后面不在贴所有信息):

[root@localhost conf]# ab -n 200000 -c 1000 http://localhost/index.html
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking localhost (be patient)
Completed 20000 requests
Completed 40000 requests
Completed 60000 requests
Completed 80000 requests
Completed 100000 requests
Completed 120000 requests
Completed 140000 requests
Completed 160000 requests
Completed 180000 requests
Completed 200000 requests
Finished 200000 requestsServer Software:        nginx/1.20.0
Server Hostname:        localhost
Server Port:            80Document Path:          /index.html
Document Length:        612 bytesConcurrency Level:      1000
Time taken for tests:   8.144 seconds
Complete requests:      200000
Failed requests:        128(Connect: 0, Receive: 0, Length: 64, Exceptions: 64)
Write errors:           0
Total transferred:      168945920 bytes
HTML transferred:       122360832 bytes
Requests per second:    24557.34 [#/sec] (mean)
Time per request:       40.721 [ms] (mean)
Time per request:       0.041 [ms] (mean, across all concurrent requests)
Transfer rate:          20258.11 [Kbytes/sec] receivedConnection Times (ms)min  mean[+/-sd] median   max
Connect:        0   30 193.1      5    3013
Processing:     2    7   9.5      7     412
Waiting:        0    6   9.4      5     411
Total:          4   37 195.5     12    3415Percentage of the requests served within a certain time (ms)50%     1266%     1375%     1380%     1490%     1595%     1798%   101399%   1017100%   3415 (longest request)

nginx,默认worker_connections =1024,因此并发只能支持到1000左右
并发1000(Concurrency Level: 1000) 发送并完成200000请求(Complete requests: 200000) 失败的次数很少(Failed requests: 128),发送200000个请求总耗时20s(Time taken for tests: 8.144 seconds),那么qps大概为 24557

Concurrency Level:      1000
Time taken for tests:   8.144 seconds
Complete requests:      200000
Failed requests:        128(Connect: 0, Receive: 0, Length: 64, Exceptions: 64)
Write errors:           0
Requests per second:    24557.34 [#/sec] (mean)
Time per request:       40.721 [ms] (mean)
Time per request:       0.041 [ms] (mean, across all concurrent requests)

服务器上本机测试:连接数200000 ,请求数5000
ab -n 200000 -c 5000 http://localhost/index.html
测试结果如下:
nginx,默认worker_connections =1024,因此并发只能支持到1000左右
并发5000(Concurrency Level: 5000) 发送并完成200000请求(Complete requests: 200000) 失败的次数很少(Failed requests: 8128),发送200000个请求总耗时10s(Time taken for tests: 10.126 seconds),那么qps大概为 19751。
发现并发增大失败的请求增多(128->8128),且qps有所降低。

Concurrency Level:      5000
Time taken for tests:   10.126 seconds
Complete requests:      200000
Failed requests:        8128(Connect: 0, Receive: 0, Length: 4064, Exceptions: 4064)
Write errors:           0
Requests per second:    19751.38 [#/sec] (mean)
Time per request:       253.147 [ms] (mean)
Time per request:       0.051 [ms] (mean, across all concurrent requests)

通过ab对nginx的默认页面测试,tps可以支持到20000左右。并发目前由于并发设置的为1024.此时cpu已经满负荷,理论上nginx并发可以支持到10w个,设置更高.
关于Nginx的一些优化(突破十万并发)

3.ab测试基于brpc的http服务器性能

::vss::ResultCode StreamAdd(const ::vss::StreamAddReq* request,::vss::StreamAddRsp* response)
{::vss::ResultCode res = ::vss::ResultCode::success;//bthread_usleep(1000*1000);//sleep(1);++count;if(time(NULL) - timestart > 1){LOG(INFO)<< "every sec average: "<< count/1;count = 0;timestart = time(NULL);}    return res;
}

ab -n 1000000 -kc 1000 http://192.168.31.91:8001/app/vss/streamAdd
发现qps为10w左右,

Concurrency Level:      1000
Time taken for tests:   10.981 seconds
Complete requests:      1000000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    1000000
Total transferred:      87000000 bytes
HTML transferred:       24000000 bytes
Requests per second:    91068.25 [#/sec] (mean)
Time per request:       10.981 [ms] (mean)
Time per request:       0.011 [ms] (mean, across all concurrent requests)
Transfer rate:          7737.24 [Kbytes/sec] received

在接口中加入1s的延时,且必须使用bthread_usleep,不会阻塞bthread。tps为800+

::vss::ResultCode StreamAdd(const ::vss::StreamAddReq* request,::vss::StreamAddRsp* response)
{::vss::ResultCode res = ::vss::ResultCode::success;bthread_usleep(1000*1000);//sleep(1);return res;
}

ab -n 10000 -kc 1000 http://192.168.31.91:8001/app/vss/streamAdd

Concurrency Level:      1000
Time taken for tests:   12.160 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    10000
Requests per second:    822.36 [#/sec] (mean)
Time per request:       1216.009 [ms] (mean)
Time per request:       1.216 [ms] (mean, across all concurrent requests)

在接口中加入2s的延时,且必须使用bthread_usleep,不会阻塞bthread。tps为400+

::vss::ResultCode StreamAdd(const ::vss::StreamAddReq* request,::vss::StreamAddRsp* response)
{::vss::ResultCode res = ::vss::ResultCode::success;bthread_usleep(2*1000*1000);//sleep(1);return res;
}

ab -n 10000 -kc 1000 http://192.168.31.91:8001/app/vss/streamAdd

Concurrency Level:      1000
Time taken for tests:   24.106 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    10000
Requests per second:    414.83 [#/sec] (mean)
Time per request:       2410.631 [ms] (mean)
Time per request:       2.411 [ms] (mean, across all concurrent requests)
Transfer rate:          35.24 [Kbytes/sec] received

在接口中加入1s的延时,且必须使用sleep,会阻塞bthread。tps为8+

::vss::ResultCode StreamAdd(const ::vss::StreamAddReq* request,::vss::StreamAddRsp* response)
{::vss::ResultCode res = ::vss::ResultCode::success;sleep(1);return res;
}
ab -n 100 -kc 10 http://192.168.31.91:8001/app/vss/streamAdd
```cpp
Concurrency Level:      10
Time taken for tests:   12.032 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Keep-Alive requests:    100
Requests per second:    8.31 [#/sec] (mean)
Time per request:       1203.191 [ms] (mean)
Time per request:       120.319 [ms] (mean, across all concurrent requests)
Transfer rate:          0.71 [Kbytes/sec] received

三、ab实战之常见问题解决

测试中出现too many open files,提供一种临时的解决版本,如需设置生效的话,请自行查询资料。
ulimit -n 可以发现系统默认是1024,可以将文件句柄增大。
ulimit -n 65535 可以将系统最大打开文件数临时修改为65535,退出登录失效。

测试中出现apr_socket_recv: Connection reset by peer 提供一种临时方案
解释:查看应用服务器和数据库均未报错,连接被重置,apr_socket_recv是OS内核的一个参数,高并发情况下,内核会认为系统受到了SYN flood***,会发送cookies(possible SYN flooding on port 80. Sending cookies),这样会减慢影响请求的速度,所以在应用服务器上设置下该参数为0 禁用系统保护就可进行大并发测试了。

进入/etc/sysctl.conf
net.ipv4.tcp_syncookies = 0 ##禁用系统保护
sysctl -p ##查看是否成功


总结

通过本文对ab的实战练习,应该对ab有了基本的了解,希望能够对你的工作有所帮助。

c++ http服务器之Apache工具ab压力测试(nginx与brpc)相关推荐

  1. apache的ab压力测试介绍

    1.ab命令原理 Apache的ab命令模拟多线程并发请求,测试服务器负载压力,也可以测试nginx.lighthttp.IIS等其它Web服务器的压力. ab命令对发出负载的计算机要求很低,既不会占 ...

  2. CentOS7下ab压力测试Nginx和Tomcat

    Apache Benchmark(简称ab) 是Apache安装包中自带的压力测试工具 安装ab yum -y install httpd-tools Nginx压力测试  ab -n 5000 -c ...

  3. Apache自带压力测试工具—ab

    ab压力测试工具: ab全称为:apache bench 我们先来了解一下压力测试的概念: 吞吐率(Requests per second) 概念:服务器并发处理能力的量化描述,单位是reqs/s,指 ...

  4. Apache ab压力测试工具

    查看apache是否安装 # rpm -qa httpd httpd-2.2.3-63.el5.centos (在apache 版本2以后,apache全部改名为httpd) ab压力测试工具是apa ...

  5. ab压力测试(了解ab工具,实验对网页进行测试)

    文章目录 使用ab工具对Apache网页进行压力测试 应用场景 实验步骤 配置DNS服务 配置apaches 执行以下命令开始压力测试 使用ab工具对Apache网页进行压力测试 应用场景 做测试或者 ...

  6. Apache网页深入优化之ab压力测试、工作模式与目录属性优化

    ab压力测试工具 Apache自带压力测试工具ab,简单易用,且可以模拟各种条件对Web服务器发起测试请求.ab工具可以直接在Web服务器本地发起测试请求,这对于需要了解服务器的处理性能至关重要,因为 ...

  7. ab压力测试工具linux,【Linux】ApacheBench(ab)压力测试工具

    AB的简介 ab是apachebench命令的缩写. ab是apache自带的压力测试工具.ab非常实用,它不仅可以对apache服务器进行网站访问压力测试,也可以对或其它类型的服务器进行压力测试.比 ...

  8. apache ab压力测试报错apr_socket_recv

    apache ab压力测试报错(apr_socket_recv: Connection reset by peer (104)) apache 自带的ab工具测试,当并发量达到1000多的时候报错如下 ...

  9. Apache ab压力测试说明

    转自: http://leepiao.blog.163.com/blog/static/485031302010234352282/ 压力测试是一种基本的质量保证行为,它是每个重要软件测试工 作的一部 ...

最新文章

  1. 精美的电路图都是怎么画出来的?
  2. 一步步优化JVM五:优化延迟或者响应时间(1)
  3. 在ubuntu上安装apc扩展
  4. 【小题目】写JAVA程序时可以创建一个名为123.java的源文件吗
  5. 性能测试关注点整理总结!
  6. nssl1519-背包签到题【数论】
  7. 用JSONObject解析和处理json数据
  8. createPattern() 自定义宽高
  9. python未来人工智能主流_python深度学习,未来人工智能三大境界的划分
  10. Python 之图片对比
  11. 明日之后哪个服务器人最多_明日之后,末日寻宝活动上线,远星城宝箱位置攻略...
  12. 黑马程序员——C语言基础教程笔记
  13. c语言编写游戏冒险,C语言-趣味游戏编写
  14. android开发接口文档模板
  15. 十天学会单片机和c语言编程,十天学会单片机和C语言编程
  16. 浏览器安装Axure插件与配置
  17. 早期华为发售设备安装Play商店,安装服务框架谷歌Mate20,P30,Mate10,P20
  18. matlab收益率,债券价格与收益率的Matlab实现(10页)-原创力文档
  19. 人工智能:爬山法、随机重启爬山法、模拟退火算法、遗传算法、启发式搜索方法解决八数码和八皇后问题
  20. R中怎么做加权最小二乘_R方的理解与用法

热门文章

  1. IBM Cloud 2015 - Invoice - 06 账期 Credit term, payment days, Net 30 days terms
  2. JavaScript 的DOM对象
  3. python android开发月薪_同事逆袭面进阿里P7 年薪60W+,临别留下一张Android开发重点技术路线图…...
  4. 十大智能家居系统解决方案
  5. 使用 advanced installer 为 winform 做自动更新
  6. Python制作炫酷的个人足迹地图
  7. Ubuntu下安装打印机驱动(两种方法)
  8. python解决哲学家就餐问题(and型信号量)
  9. 此刻,若你不爱我,我也不会在意【茨维塔耶娃】
  10. 常用函数的拉氏变换表