ApacheBench(ab)使用详解

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

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

运行:

1 在Windows系统下,打开cmd命令行窗口,定位到apache安装目录的bin目录下 
2 cd C:Program Files (x86)Apache Software FoundationApache2.2bin
3 键入命令: 
4 ab -n 800 -c 800  http://192.168.0.10/ 
5 (-n发出800个请求,-c模拟800并发,相当800人同时访问,后面是测试url)
6 ab -t 60 -c 100 http://192.168.0.10/ 
7 在60秒内发请求,一次100个请求。 

//如果需要在url中带参数,这样做:

1).只带一个参数:

ab -n 1000 -c 100 http://123.56.153.48/cname/Buildings?Token=sfddgsf--g

2) 有多个参数:

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 p.txt其p.txt须改为完整的文件路径,如: -p D:/param/p.txt
p.txt 中可以写参数,如  p=wdp&fq=78 

结果参数解释: 

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)

备注:

  1. ApacheBench的参数说明:
 1 格式:ab [options] [http://]hostname[:port]/path
 2 参数说明:
 3 -n requests Number of requests to perform
 4 //在测试会话中所执行的请求个数(本次测试总共要访问页面的次数)。默认时,仅执行一个请求。
 5 -c concurrency Number of multiple requests to make
 6 //一次产生的请求个数(并发数)。默认是一次一个。
 7 -t timelimit Seconds to max. wait for responses
 8 //测试所进行的最大秒数。其内部隐含值是-n 50000。它可以使对服务器的测试限制在一个固定的总时间以内。默认时,没有时间限制。
 9 -p postfile File containing data to POST
10 //包含了需要POST的数据的文件,文件格式如“p1=1&p2=2”.使用方法是 -p 111.txt 。 (配合-T)
11 -T content-type Content-type header for POSTing
12 //POST数据所使用的Content-type头信息,如 -T “application/x-www-form-urlencoded” 。 (配合-p)
13 -v verbosity How much troubleshooting info to print
14 //设置显示信息的详细程度 – 4或更大值会显示头信息, 3或更大值可以显示响应代码(404, 200等), 2或更大值可以显示警告和其他信息。 -V 显示版本号并退出。
15 -w Print out results in HTML tables
16 //以HTML表的格式输出结果。默认时,它是白色背景的两列宽度的一张表。
17 -i Use HEAD instead of GET
18 // 执行HEAD请求,而不是GET。
19 -x attributes String to insert as table attributes
20 -y attributes String to insert as tr attributes
21 -z attributes String to insert as td or th attributes
22 -C attribute Add cookie, eg. -C “c1=1234,c2=2,c3=3″ (repeatable)
23 //-C cookie-name=value 对请求附加一个Cookie:行。 其典型形式是name=value的一个参数对。此参数可以重复,用逗号分割。
24 提示:可以借助session实现原理传递 JSESSIONID参数, 实现保持会话的功能,如
25 -C ” c1=1234,c2=2,c3=3, JSESSIONID=FF056CD16DA9D71CB131C1D56F0319F8″ 。
26 -H attribute Add Arbitrary header line, eg. ‘Accept-Encoding: gzip’ Inserted after all normal header lines. (repeatable)
27 -A attribute Add Basic WWW Authentication, the attributes
28 are a colon separated username and password.
29 -P attribute Add Basic Proxy Authentication, the attributes
30 are a colon separated username and password.
31 //-P proxy-auth-username:password 对一个中转代理提供BASIC认证信任。用户名和密码由一个:隔开,并以base64编码形式发送。无论服务器是否需要(即, 是否发送了401认证需求代码),此字符串都会被发送。
32 -X proxy:port Proxyserver and port number to use
33 -V Print version number and exit
34 -k Use HTTP KeepAlive feature
35 -d Do not show percentiles served table.
36 -S Do not show confidence estimators and warnings.
37 -g filename Output collected data to gnuplot format file.
38 -e filename Output CSV file with percentages served
39 -h Display usage information (this message)
40 //-attributes 设置属性的字符串. 缺陷程序中有各种静态声明的固定长度的缓冲区。另外,对命令行参数、服务器的响应头和其他外部输入的解析也很简单,这可能会有不良后果。它没有完整地实现 HTTP/1.x; 仅接受某些’预想’的响应格式。 strstr(3)的频繁使用可能会带来性能问题,即你可能是在测试ab而不是服务器的性能。

询价接口示例: 

原文地址:https://www.cnblogs.com/xiaoyaowuming/p/5622660.html