ab工具

傻男人 1年前 ⋅ 530 阅读

ab工具

liunx环境

安装

yum -y install httpd-tools

查看版本号

[root@localhost ~]# ab -V
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

参数说明

[root@localhost ~]# ab -help
Usage: ab [options] [http[s]://]hostname[:port]/path
Options are:
    -n requests     Number of requests to perform
    -c concurrency  Number of multiple requests to make at a time
    -t timelimit    Seconds to max. to spend on benchmarking
                    This implies -n 50000
    -s timeout      Seconds to max. wait for each response
                    Default is 30 seconds
    -b windowsize   Size of TCP send/receive buffer, in bytes
    -B address      Address to bind to when making outgoing connections
    -p postfile     File containing data to POST. Remember also to set -T
    -u putfile      File containing data to PUT. Remember also to set -T
    -T content-type Content-type header to use for POST/PUT data, eg.
                    'application/x-www-form-urlencoded'
                    Default is 'text/plain'
    -v verbosity    How much troubleshooting info to print
    -w              Print out 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
                    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
    -k              Use HTTP KeepAlive feature
    -d              Do not show percentiles served table.
    -S              Do not show confidence estimators and warnings.
    -q              Do not show progress when doing more than 150 requests
    -g filename     Output collected data to gnuplot format file.
    -e filename     Output CSV file with percentages served
    -r              Don't exit on socket receive errors.
    -h              Display usage information (this message)
    -Z ciphersuite  Specify SSL/TLS cipher suite (See openssl ciphers)
    -f protocol     Specify SSL/TLS protocol
                    (SSL3, TLS1, TLS1.1, TLS1.2 or ALL)

主要使用参数:

  • -n:在测试会话中,所执行的请求个数,默认时,仅执行一个;表示请求的总数量
  • -c:一次产生的请求个数,默认一次一个;表示请求的用户量;
  • -t:指定每个请求的超时时间,默认是30秒

windows环境

官方下载地址

https://www.apachehaus.com/cgi-bin/download.plx

百度网盘

百度网盘 提取码:u27m

添加bin目录到path的环境变量

请求示例

简单的get请求

ab -n 1000 -c 100 http://localhost:9093/v4/tes1?orderId=123456789 http://localhost:9093/v4/tes2 ab -n 10 -c 5 http://www.baidu.com/

请求http://www.baidu.com/, 每次发送100个请求,总1000个请求

[root@localhost ~]# ab -n 10 -c 5  http://www.baidu.com/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.baidu.com (be patient).....done


Server Software:        BWS/1.1        # 被测试的Web服务器软件名称
Server Hostname:        www.baidu.com        # 请求的URL主机名
Server Port:            80        # 被测试的Web服务器软件的监听端口

Document Path:          /        # 请求的URL中的根绝对路径,通过该文件的后缀名,我们一般可以了解该请求的类型
Document Length:        322742 bytes        # HTTP响应数据的正文长度

Concurrency Level:      5        # 并发用户数,这是我们设置的参数之一
Time taken for tests:   0.461 seconds        # 所有这些请求被处理完成所花费的总时间
Complete requests:      10        # 总请求数量,这是我们设置的参数之一
Failed requests:        9        # 失败的请求数量,这里的失败是指请求在连接服务器、发送数据等环节发生异常,以及无响应后超时的情况。如果接收到的HTTP响应数据的头信息中含有2XX以外的状态码,则会在测试结果中显示另一个名为“Non-2xx responses”的统计项,用于统计这部分请求数,这些请求并不算在失败的请求中。
   (Connect: 0, Receive: 0, Length: 9, Exceptions: 0)        # 
Write errors:           0        # 
Total transferred:      3240608 bytes        # 所有请求的响应数据长度总和,包括每个HTTP响应数据的头信息和正文数据的长度。注意这里不包括HTTP请求数据的长度,仅仅为web服务器流向用户PC的应用层数据总长度。
HTML transferred:       3228577 bytes        # 所有请求的响应数据中正文数据的总和,也就是减去了Total transferred中HTTP响应数据中的头信息的长度
Requests per second:    21.71 [#/sec] (mean)        # 吞吐率,也叫QPS,计算公式:Complete requests/Time taken for tests
Time per request:       230.278 [ms] (mean)        # 用户平均请求等待时间,从用户角度看,完成一个请求所需要的时间。计算公式:Time token for tests/(Complete requests/Concurrency Level)
Time per request:       46.056 [ms] (mean, across all concurrent requests)        # 服务器完成一个请求的时间,计算公式:Time taken for tests/Complete requests,正好是吞吐率的倒数
也可以这么统计:Time per request/Concurrency Level
Transfer rate:          6871.38 [Kbytes/sec] received        # 网络传输速度,计算公式:Total trnasferred/ Time taken for tests,这个统计很好的说明服务器的处理能力达到极限时,其出口宽带的需求量,对于大文件的请求测试,这个值很容易成为系统瓶颈所在。要确定该值是不是瓶颈,需要了解客户端和被测服务器之间的网络情况,包括网络带宽和网卡速度等信息。

#这几行组成的表格主要是针对响应时间也就是第一个Time per request进行细分和统计。一个请求的响应时间可以分成网络链接(Connect),系统处理(Processing)和等待(Waiting)三个部分。表中min表示最小值; mean表示平均值;[+/-sd]表示标准差(Standard Deviation) ,也称均方差(mean square error),这个概念在中学的数学课上学过,表示数据的离散程度,数值越大表示数据越分散,系统响应时间越不稳定。 median表示中位数; max当然就是表示最大值了。

#需要注意的是表中的Total并不等于前三行数据相加,因为前三行的数据并不是在同一个请求中采集到的,可能某个请求的网络延迟最短,但是系统处理时间又是最长的呢。所以Total是从整个请求所需要的时间的角度来统计的。这里可以看到最慢的一个请求花费了2ms(即100% 2 (longest request))。
Connection Times (ms)      
              min  mean[+/-sd] median   max
Connect:       31   36   3.0     36      41
Processing:   129  148  10.4    149     163
Waiting:       33   38   2.4     38      41
Total:        160  183  13.3    186     205


#这部分数据用于描述每个请求处理时间的分布情况,比如以上测试,80%的请求处理时间都不超过2ms,这个处理时间是指前面的Time per request,即对于单个用户而言,平均每个请求的处理时间。
#这个表第一行表示有50%的请求都是在2ms内完成的,可以看到这个值是比较接近平均系统响应时间,以此类推。
Percentage of the requests served within a certain time (ms)
  50%    186
  66%    189
  75%    191
  80%    200
  90%    205
  95%    205
  98%    205
  99%    205
 100%    205 (longest request)

POST

ab -p post.json -T application/json -c 10 -n 100 http://10.82.25.183/post


全部评论: 0

    我有话说: