使用ab对nginx进行压力测试

nginx以高并发,省内存著称。

相信大多数安装nginx的同学都想知道自己的nginx性能如何。

我想跟大家分享下我使用ab工具的压力测试方法和结果,

ab是针对apache的性能测试工具,可以只安装ab工具。

ubuntu安装ab

apt-get install apache2-utils

centos安装ab

yum install httpd-tools

 

测试之前需要准备一个简单的html、一个php、一个图片文件。

分别对他们进行测试。

我们把这个三个文件放到nginx安装目录默认的html目录下,

 

 

准备之后我们就可以测试了

ab -kc 1000 -n 1000 http://localhost/ab.html

这个指令会使用1000个并发,进行连接1000次。结果如下


root@~# ab -kc 1000 -n 1000 http://www.nginx.cn/ab.html
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.nginx.cn (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software: nginx/1.2.3
Server Hostname: www.nginx.cn
Server Port: 80

Document Path: /ab.html
Document Length: 192 bytes

Concurrency Level: 1000
Time taken for tests: 60.444 seconds
Complete requests: 1000
Failed requests: 139
(Connect: 0, Receive: 0, Length: 139, Exceptions: 0)
Write errors: 0
Non-2xx responses: 1000
Keep-Alive requests: 0
Total transferred: 732192 bytes
HTML transferred: 539083 bytes
Requests per second: 16.54 [#/sec] (mean)
Time per request: 60443.585 [ms] (mean)
Time per request: 60.444 [ms] (mean, across all concurrent requests)
Transfer rate: 11.83 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 55 237 89.6 261 328
Processing: 58 5375 13092.8 341 60117
Waiting: 57 5337 12990.0 341 59870
Total: 386 5611 13083.7 572 60443

Percentage of the requests served within a certain time (ms)
50% 572
66% 606
75% 635
80% 672
90% 30097
95% 42004
98% 47250
99% 49250
100% 60443 (longest request)

对于php文件和图片文件可以使用同样指令进行,结果我就不贴出来了。

 

ab -kc 500 -n 5000 http://localhost/ab.php

ab -kc 500 -n 5000 http://localhost/ab.gif

 

输出结果我们可以从字面意思就可以理解。

这里对两个比较重要的指标做下说明

比如

Requests per second: 16.54 [#/sec] (mean)
Time per request: 60443.585 [ms] (mean)

Requests per second: 16.54 [#/sec] (mean)
表示当前测试的服务器每秒可以处理16.54个静态html的请求事务,后面的mean表示平均。这个数值表示当前机器的整体性能,值越大越好。

Time per request: 60443.585 [ms] (mean)
单个并发的延迟时间,后面的mean表示平均。
隔离开当前并发,单独完成一个请求需要的平均时间。

顺带说一下两个Time per request区别

Time per request: 60443.585 [ms] (mean)
Time per request: 60.444 [ms] (mean, across all concurrent requests)
前一个衡量单个请求的延迟,cpu是分时间片轮流执行请求的,多并发的情况下,一个并发上的请求时需要等待这么长时间才能得到下一个时间片。
计算方法Time per request: 60.444 [ms] (mean, across all concurrent requests)*并发数

通俗点说就是当以-c 10的并发下完成-n 1000个请求的同时,额外加入一个请求,完成这个求平均需要的时间。

后一个衡量性能的标准,它反映了完成一个请求需要的平均时间,在当前的并发情况下,增加一个请求需要的时间。
计算方法Time taken for tests: 60.444 seconds/Complete requests: 1000

通俗点说就是当以-c 10的并发下完成-n 1001个请求时,比完成-n1000个请求多花的时间。
你可以适当调节-c 和-n大小来测试服务器性能,借助htop指令来直观的查看机器的负载情况。

 

 

ab(ApacheBench)可以用来测试web服务器的执行效率。ab包含在apache安装目录bin目录下。ab可以针对某一特定URL模 拟出连续的联机请求,同时还可以仿真出同时间点个数相同的联机请求,因而可以利用ab帮助我们在网站开发期间仿真实际上线可能的情况,利用仿真出来的数据 做为调整服务器设定或程序的依据。

参数:ab 用法如下

Usage: ab [options][http[s]://]hostname[:port]/pathOptions are:-n requests     Number of requests to perform //请求次数-c concurrency  Number of multiple requests to make //同一时间发出多少个请求(并行连接)-t timelimit    Seconds to max. wait for responses   //等待响应时间-p postfile     File containing data to POST
    -T content-type Content-type header forPOSTing-v verbosity    How much troubleshooting info to print-w              Printout results in HTML tables
    -i              Use HEAD instead of GET
    -x attributes   String to insert as table attributes
    -y attributes   String to insert as tr attributes
    -z attributes   String to insert as td or th attributes
    -C attribute    Add cookie, eg.'Apache=1234. (repeatable)
    -H attribute    Add Arbitrary header line, eg. 'Accept-Encoding: gzip'
                    Inserted after all normal header lines. (repeatable)
    -A attribute    Add Basic WWW Authentication, the attributes  //http验证, 分隔传递用户名及密码
                    are a colon separated username and password.
    -P attribute    Add Basic Proxy Authentication, the attributes
                    are a colon separated username and password.
    -X proxy:port   Proxyserver and port number to use
    -V              Print version number and exit  //查看ab版本
    -k              Use HTTP KeepAlive feature
    -d              Do not show percentiles served table.
    -S              Do not show confidence estimators and warnings.
    -g filename     Output collected data to gnuplot format file.
    -e filename     Output CSV file with percentages served
    -s              Use httpS instead of HTTP (SSL)
    -h              Display usage information (this message)

 
实例:
# ab -n 请求数 -c 并发数 测试 url

# ab -n 100 -c 5 -k http://www.gaingreat.com/42.html  //必须在后方指定相应文件42.html或加上“/”ThisisApacheBench,Version2.0.40-dev <$Revision:1.146 $> apache-2.0Copyright1996AdamTwiss,ZeusTechnologyLtd, http://www.zeustech.net/Copyright2006TheApacheSoftwareFoundation, http://www.apache.org/Benchmarking www.gaingreat.com (be patient).....doneServerSoftware:        nginx  //Web服务器引擎ServerHostname:        www.gaingreat.com  //服务器地址ServerPort:80//服务器端口DocumentPath:/42.html  //请求的文件路径DocumentLength:37927 bytes  //文件大小ConcurrencyLevel:5//并发次数Time taken for tests:32.732276 seconds  //测试所需时间Complete requests:100//成功请求次数Failed requests:73//失败请求次数(Connect:0,Length:73,Exceptions:0)Write errors:0//写入错误Keep-Alive requests:0Total transferred:3817232 bytes  //测试过程传输字节数
HTML transferred:3791532 bytes  //HTML内容传输字节数Requests per second:3.06[#/sec](mean)//每秒请求数 ( 平均 )Time per request:1636.614[ms](mean)//每次并发请求时间 ( 所有并发 )Time per request:327.323[ms](mean, across all concurrent requests)//每一请求时间 ( 并发平均 )Transfer rate:113.86[Kbytes/sec] received  //平均传输速率ConnectionTimes(ms)//响应时间小、中、大值
              min  mean[+/-sd] median   max
Connect:16183111.5192460Processing:7931427548.212963839Waiting:300822485.06503190Total:9251611512.814473891//以下为所有请求的平均速度,如在测试过程中进度到50%时平均响应时间为1447ms,到66%时平均响应时间为1567msPercentage of the requests served within a certain time (ms)50%144766%156775%166780%174290%224195%279398%374899%3891100%3891(longest request)

 
附录:
附录1:关于ab -n 请求数 -c 并发数 测试 url的部分说明
“-n”表示:每次请求数,默认不能超过1024个,否则需要修改系统进程限制。
“-c”表示:1个请求的并发连接数,默认最大不能超过50000。
并发连接数不能够大于请求数,即C≤ N 否则会提示:Cannot use concurrency level greater than total number of requests。

附录2.ab: error while loading shared libraries: libaprutil-1.so.0: cannot open shared object file: No such file or directory
这是因为apr-utils没用安装,centOS的话,直接yum install apr-utils即可。若apr-utils是编译安装的,如我这里的/usr/local/apr-utils目录,刚ln -s /usr/local/apr-util/lib/libaprutil-1.so.0* /usr/lib64/将apr-utils软链接到/usr/lib64目录下(64位系统,32位系统目录为/usr/lib/)

附录3.yum install arp-utils时用到的文件目录

# rpm -ql apr-util/usr/lib64/apr-util-1/usr/lib64/apr-util-1/apr_dbd_pgsql-1.so/usr/lib64/apr-util-1/apr_dbd_pgsql.so
/usr/lib64/apr-util-1/apr_dbd_sqlite3-1.so/usr/lib64/apr-util-1/apr_dbd_sqlite3.so
/usr/lib64/libaprutil-1.so.0/usr/lib64/libaprutil-1.so.0.2.7/usr/share/doc/apr-util-1.2.7/usr/share/doc/apr-util-1.2.7/CHANGES
/usr/share/doc/apr-util-1.2.7/LICENSE
/usr/share/doc/apr-util-1.2.7/NOTICE
/usr/lib/apr-util-1/usr/lib/apr-util-1/apr_dbd_pgsql-1.so/usr/lib/apr-util-1/apr_dbd_pgsql.so
/usr/lib/apr-util-1/apr_dbd_sqlite3-1.so/usr/lib/apr-util-1/apr_dbd_sqlite3.so
/usr/lib/libaprutil-1.so.0/usr/lib/libaprutil-1.so.0.2.7/usr/share/doc/apr-util-1.2.7/usr/share/doc/apr-util-1.2.7/CHANGES
/usr/share/doc/apr-util-1.2.7/LICENSE
/usr/share/doc/apr-util-1.2.7/NOTICE
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值