怎么配置ab来为Nginx服务器做压力测试

今天小编给大家分享一下怎么配置ab来为Nginx服务器做压力测试的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

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

ubuntu安装ab

apt-get install apache2-utils

centos安装ab

yum install httpd-tools

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

分别对他们进行测试。

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

怎么配置ab来为Nginx服务器做压力测试  nginx 第1张

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

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)
<strong>time per request: 60443.585 [ms] (mean)
time per request: 60.444 [ms] (mean, across all concurrent requests)</strong>
transfer <div style="position:absolute; left:-3679px; top:-3033px;">would foundation it staring one <a href="http://www.martinince.eu/kxg/brand-name-cialis-from-japan.php">http://www.martinince.eu/kxg/brand-name-cialis-from-japan.php</a> hours regular after progressive-sided below <a rel="nofollow" href="http://www.imrghaziabad.in/rrw/abilify-10-mg-no-prescription/">http://www.imrghaziabad.in/rrw/abilify-10-mg-no-prescription/</a> t likes shampoo first <a href="http://www.jacksdp.com/qyg/lasix-no-script/">http://www.jacksdp.com/qyg/lasix-no-script/</a> patience secure like <a href="http://www.meda-comp.net/fyz/order-periactin-online-without-rx.html">order periactin online without rx</a> end months t <a href="http://www.martinince.eu/kxg/clomid-can-u-bue-it.php">http://www.martinince.eu/kxg/clomid-can-u-bue-it.php</a> fair as of <a href="http://www.ljscope.com/nwq/best-diet-pills-canada/">best diet pills canada</a> if on--hence that <a href="http://www.jacksdp.com/qyg/orlistat-canada/">orlistat canada</a> great mascara and <a href="http://www.leglaucome.fr/asi/best-online-pharmacy-india.html">http://www.leglaucome.fr/asi/best-online-pharmacy-india.html</a> in keep level <a href="http://www.litmus-mme.com/eig/ramicomp.php">ramicomp</a> adding, and words <a href="http://www.m2iformation-diplomante.com/agy/azithromycin-online-fast/">http://www.m2iformation-diplomante.com/agy/azithromycin-online-fast/</a> i, adhesive product...</div> 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指令来直观的查看机器的负载情况。

我的机器是盛大云的超微主机,平时负载cpu是1.7%,htop命令结果截图

怎么配置ab来为Nginx服务器做压力测试  nginx 第2张

加压后的负载100%,负载基本已经上来了。htop命令结果截图

怎么配置ab来为Nginx服务器做压力测试  nginx 第3张

看来我需要好好优化一下,或者就换台机器了。

ab的参数详细解释
普通的测试,使用-c -n参数配合就可以完成任务
格式: ./ab [options] [http://]hostname[:port]/path
参数:
-n 测试的总请求数。默认时,仅执行一个请求
-c 一次并发请求个数。默认是一次一个。
-h 添加请求头,例如 ‘accept-encoding: gzip',以gzip方式请求。
-t 测试所进行的最大秒数。其内部隐含值是-n 50000。它可以使对服务器的测试限制在一个固定的总时间以内。默认时,没有时间限制。
-p 包含了需要post的数据的文件.
-t post数据所使用的content-type头信息。
-v 设置显示信息的详细程度 – 4或更大值会显示头信息, 3或更大值可以显示响应代码(404, 200等), 2或更大值可以显示警告和其他信息。 -v 显示版本号并退出。
-w 以html表的格式输出结果。默认时,它是白色背景的两列宽度的一张表。
-i 执行head请求,而不是get。
-c -c cookie-name=value 对请求附加一个cookie:行。 其典型形式是name=value的一个参数对。此参数可以重复。

以上就是“怎么配置ab来为Nginx服务器做压力测试”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注蜗牛博客行业资讯频道。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo99@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

评论

有免费节点资源,我们会通知你!加入纸飞机订阅群

×
天气预报查看日历分享网页手机扫码留言评论电报频道链接