介绍两个性能压力测试工具
1. AB
Apache服务器自带有一个叫ab(ApacheBench)的工具,在bin目录下。ab专门用于HTTP Server的benchmark testing,可以同时模拟多个并发请求,使用这个轻巧的工具我们可以对服务器进行负载测试.
安装
下载:http://httpd.apache.org/download.cgi,详情安装可自行网上查找
测试
例如命令:ab -n 10 -c 10 https://www.csdn.net/,表示同时处理10个请求并运行10次
参数众多,一般我们用到的是-n 请求数 和-c 并发数
输出结果说明
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking www.jb51.net (be patient)…..done
Server Software: Microsoft-IIS/6.0 //Microsoft-IIS服务器版本6.0
Server Hostname: https://www.csdn.net/ //服务器主机名
Server Port: 80 //服务器端口
Document Path: / //测试的页面文档
Document Length: 32639 bytes //文档大小
Concurrency Level: 10 //并发数
Time taken for tests: 13.548 seconds //整个测试持续的时间
Complete requests: 10 //完成的请求数量
Failed requests: 0 //失败的请求数量
Write errors: 0
Total transferred: 331070 bytes //整个场景中的网络传输量
HTML transferred: 326390 bytes //整个场景中的HTML内容传输量
Requests per second: 0.74 [#/sec] (mean) //每秒事务数 ,后面括号中的 mean 表示这是一个平均值
Time per request: 13547.775 [ms] (mean) //平均事务响应时间 ,后面括号中的 mean 表示这是一个平均值
Time per request: 1354.777 [ms] (mean, across all concurrent requests) //每个请求实际运行时间的平均值
Transfer rate: 23.86 [Kbytes/sec] received //平均每秒网络上的流量,可以帮助排除是否存在网络流量过大导致响应时间延长的问题
Connection Times (ms) //网络上消耗的时间的分解
min mean[+/-sd] median max
Connect: 1 2 0.8 2 3
Processing: 2163 3981 3420.2 2957 13540
Waiting: 1305 3204 3595.3 2096 13169
Total: 2164 3983 3420.0 2959 13541
//以下是整个场景中所有请求的响应情况。在场景中每个请求都有一个响应时间,其中50%的用户响应时间小于2959毫秒,66% 的用户响应时间小于3074毫秒,最大的响应时间小于13541 毫秒。由于对于并发请求,cpu实际上并不是同时处理的,而是按照每个请求获得的时间片逐个轮转处理的,所以基本上第一个Time per request时间约等于第二个Time per request时间乘以并发请求数。
Percentage of the requests served within a certain time (ms)
50% 2959
66% 3074
75% 3974
80% 4008
90% 13541
95% 13541
98% 13541
99% 13541
100% 13541 (longest request)
2. Siege
Siege是linux下的一个web系统的压力测试工具,支持多链接,支持get和post请求,可以对web系统进行多并发下持续请求的压力测试。
安装 Siege
# wget http://download.joedog.org/siege/siege-4.0.4.tar.gz
# tar -xzvf siege-4.0.4.tar.gz; cd siege-4.0.4
# ./configure; make
# make install
或者一条命令:
tar -xzvf siege-4.0.4.tar.gz; cd siege-4.0.4; ./configure; make; make install
wget http://download.joedog.org/siege/siege-4.0.4.tar.gz; tar -xzvf siege-4.0.4.tar.gz; cd siege-4.0.4; ./configure; make; make install;
默认配置文件位置:./.siege/siege.conf
Siege命令常用参数
-c 200 指定并发数200
-r 5 指定测试的次数5
-f urls.txt 制定url的文件
-i internet系统,随机发送url
-b 请求无需等待 delay=0
-t 5 持续测试5分钟
# -r和-t一般不同时使用
常用的siege命令举例
200个并发对http://www.baidu.com发送请求100次
siege -c 200 -r 100 http://www.baidu.com
在urls.txt中列出所有的网址
siege -c 200 -r 100 -f urls.txt
随机选取urls.txt中列出所有的网址
siege -c 200 -r 100 -f urls.txt -i
delay=0,更准确的压力测试,而不是功能测试
siege -c 200 -r 100 -f urls.txt -i -b
指定http请求头 文档类型
siege -H "Content-Type:application/json" -c 200 -r 100 -f urls.txt -i -b
200个并发对http://www.baidu.com发送POST请求100次
siege 'http://www.baidu.com POST a=1&b=2' -c 200 -r 100 -b
设置http请求头参数
siege -c 1 -r 1 'http://www.baidu.com POST' -H "userId:minbo" -b
设置post请求(body json参数,默认是json格式),定义一个data.json文件,把参数贴进去即可
siege -c 1 -r 1 'http://www.baidu.com POST <./data.json' -H "userId:minbo" -b
注:如果是form参数,则需要指定:-H 'Content-Type:application/x-www-form-urlencoded',如下案例
siege -c 2 -r 2 'https://xxx/xx.action POST data={'a':1,'b':4,'c':2}' -H 'X-AUTH-TOKEN:123' -H 'Content-Type:application/x-www-form-urlencoded'
Siege输出结果说明
The server is now under siege.. done.
Transactions: 30000 hits //完成30000次处理
Availability: 100.00 % //100.00 % 成功率
Elapsed time: 68.59 secs //总共使用时间
Data transferred: 817.76 MB //共数据传输 817.76 MB
Response time: 0.04 secs //响应时间,显示网络连接的速度
Transaction rate: 437.38 trans/sec //平均每秒完成 437.38 次处理
Throughput: 11.92 MB/sec //平均每秒传送数据
Concurrency: 17.53 //实际最高并发连接数
Successful transactions: 30000 //成功处理次数
Failed transactions: 0 //失败处理次数
Longest transaction: 3.12 //每次传输所花最长时间
Shortest transaction: 0.00 //每次传输所花最短时间
Siege支持https
测试:siege -c 2 -r 2 https://www.baidu.com/
异常信息:
** Preparing 2 concurrent users for battle.
The server is now under siege...
[error] HTTPS requires libssl: Unable to reach www.baidu.com with this protocol: Socket is already connected
[error] HTTPS requires libssl: Unable to reach www.baidu.com with this protocol: Socket is already connected
[error] HTTPS requires libssl: Unable to reach www.baidu.com with this protocol: Socket is already connected
[error] HTTPS requires libssl: Unable to reach www.baidu.com with this protocol: Socket is already connected
解决办法
安装openssl
wget http://www.openssl.org/source/openssl-1.0.2g.tar.gz
tar zxvf openssl-1.0.2g.tar.gz
cd openssl-1.0.2g/
./config
make depend
make install
siege启用SSL
wget http://download.joedog.org/siege/siege-latest.tar.gz
tar zxvf siege-latest.tar.gz
cd siege-xxx/
./configure --with-ssl=SSL安装路径
make
make install
常见问题
问题
WARNING: The number of users is capped at 255. To increase this limit, search your .siegerc file for 'limit' and change its value. Make sure you read the instructions there...
解决办法
修改配置文件(默认配置文件地址:./.siege/siege.conf),修改limit值,比如limit = 1000,则并发为1000了,默认为255
参考资料
https://blog.csdn.net/lymemoryzz/article/details/74178723
https://www.jb51.net/article/56644.htm
https://blog.csdn.net/qingye2008/article/details/34500949
https://wiki.mikejung.biz/Siege
------------------------------------------------------
------------------------------------------------------
期望和大家一起学习,共同进步,共勉
欢迎交流问题,可加个人QQ 469580884
或者,加我的群号 751925591,一起探讨交流问题
不讲虚的,只做实干家
Talk is cheap,show me the code