系列服务器开发
前言
一、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 requests
Server Software: nginx/1.20.0
Server Hostname: localhost
Server Port: 80
Document Path: /index.html
Document Length: 612 bytes
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
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] received
Connection 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 3415
Percentage of the requests served within a certain time (ms)
50% 12
66% 13
75% 13
80% 14
90% 15
95% 17
98% 1013
99% 1017
100% 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 ##查看是否成功
文章来源:https://www.toymoban.com/news/detail-466468.html
总结
通过本文对ab的实战练习,应该对ab有了基本的了解,希望能够对你的工作有所帮助。文章来源地址https://www.toymoban.com/news/detail-466468.html
到了这里,关于c++ http服务器之Apache工具ab压力测试(nginx与brpc)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!