apache-ab并发负载压力测试

来源:互联网 发布:ovid medline数据库 编辑:程序博客网 时间:2024/05/22 06:48

apache-ab并发负载压力测试


ab命令原理 

Apache的ab命令模拟多线程并发请求,测试服务器负载压力,也可以测试nginx、lighthttp、IIS等其它Web服务器的压力。 
ab命令对发出负载的计算机要求很低,既不会占用很多CPU,也不会占用太多的内存,但却会给目标服务器造成巨大的负载,因此是某些DDOS攻击之必备良药,老少皆宜。自己使用也须谨慎。否则一次上太多的负载,造成目标服务器直接因内存耗光死机,而不得不硬重启,得不偿失。

在带宽不足的情况下,最好是本机进行测试,建议使用内网的另一台或者多台服务器通过内网进行测试,这样得出的数据,准确度会高很多。远程对web服务器进行压力测试,往往效果不理想(因为网络延时过大或带宽不足)

下载安装: 

http://mirror.bit.edu.cn/apache//httpd/binaries/win32/?C=M;O=A 
找到 httpd-2.2.21-win32-x86-no_ssl.msi  

有的电脑安装后运行会报 缺少 "vcruntime140.dll"这个文件,在网上找下这个文件下载下来,复制到 windows/system32即可


参数文档: 

http://httpd.apache.org/docs/2.2/programs/ab.html


ab常用参数的介绍:

-n :总共的请求执行数,缺省是1;
-c: 并发数,缺省是1;
-t:测试所进行的总时间,秒为单位,缺省50000s
-p:POST时的数据文件
-w: 以HTML表的格式输出结果
-T:post数据所使用的Content-Type头信息,如果-T 'application/x-www-form-urlencoded'

执行测试用例:ab -n 1000 -c 100 -w http://localhost/index.html >>c:\1.html
上面的测试用例表示100并发的情况下,共测试访问index.php脚本1000次,并将测试结果保存到c:\1.html文件中。

//如果需要在url中带参数,这样做 
ab -t 60 -c 100 -T "text/plain" -p p.txt http://192.168.0.10/hello.html 
p.txt 是和ab.exe在一个目录 
p.txt 中可以写参数,如  p=wdp&fq=78 

运行: 

在Windows系统下,打开cmd命令行窗口,定位到apache安装目录的bin目录下 
cd C:\Program Files (x86)\Apache Software Foundation\Apache2.2\bin

键入命令: 
ab -n 800 -c 800  http://192.168.0.10/ 
(-n发出800个请求,-c模拟800并发,相当800人同时访问,后面是测试url)

ab -t 60 -c 100 http://192.168.0.10/ 
在60秒内发请求,一次100个请求。 
  




结果参数解释: 

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 192.168.0.10 (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 
Finished 800 requests

Server Software:        Microsoft-HTTPAPI/2.0 
Server Hostname:        192.168.0.10 
Server Port:            80

Document Path:          / 
Document Length:        315 bytes       HTTP响应数据的正文长度

Concurrency Level:      800 
Time taken for tests:   0.914 seconds    所有这些请求处理完成所花费的时间 
Complete requests:      800             完成请求数 
Failed requests:        0                失败请求数 
Write errors:           0                
Non-2xx responses:      800 
Total transferred:      393600 bytes     网络总传输量 
HTML transferred:       252000 bytes     HTML内容传输量 
Requests per second:    875.22 [#/sec] (mean) 吞吐量-每秒请求数 
Time per request:       914.052 [ms] (mean)  服务器收到请求,响应页面要花费的时间 
Time per request:       1.143 [ms] (mean, across all concurrent requests) 并发的每个请求平均消耗时间 
Transfer rate:          420.52 [Kbytes/sec] received 平均每秒网络上的流量,可以帮助排除是否存在网络流量过大导致响应时间延长的问题


网络上消耗的时间的分解: 
Connection Times (ms) 
              min  mean[+/-sd] median   max 
Connect:        0    1   0.5      1       3 
Processing:   245  534 125.2    570     682 
Waiting:       11  386 189.1    409     669 
Total:        246  535 125.0    571     684

整个场景中所有请求的响应情况。在场景中每个请求都有一个响应时间 
其中 50% 的用户响应时间小于 571 毫秒 
80 % 的用户响应时间小于 652 毫秒 
最大的响应时间小于 684 毫秒 
Percentage of the requests served within a certain time (ms) 
  50%    571 
  66%    627 
  75%    646 
  80%    652 
  90%    666 
  95%    677 
  98%    681 
  99%    682 

100%    684 (longest request)


Requests per second: 19.66 [#/sec] (mean)

//吞吐率,大家最关心的指标之一,相当于 LR 中的每秒事务数,后面括号中的 mean 表示这是一个平均值

Time per request: 5087.180 [ms] (mean)

//用户平均请求等待时间,大家最关心的指标之二,相当于 LR 中的平均事务响应时间,后面括号中的 mean 表示这是一个平均值

Time per request: 50.872 [ms] (mean, across all concurrent requests)

//服务器平均请求处理时间,大家最关心的指标之三

关于登录的问题


有时候进行压力测试需要用户登录,怎么办?
请参考以下步骤:

先用账户和密码登录后,用开发者工具找到标识这个会话的Cookie值(Session ID)记下来
如果只用到一个Cookie,那么只需键入命令:
ab -n 100 -C key=value http://test.com/

如果需要多个Cookie,就直接设Header:
ab -n 100 -H “Cookie: Key1=Value1; Key2=Value2” http://test.com/

转自:http://www.cnblogs.com/zengxiangzhan/archive/2012/12/07/2807141.html

http://www.jianshu.com/p/43d04d8baaf7


--- end ---