Linux命令(139)之ab
  wwLZeziuqjLR 2023年12月23日 56 0


linux命令之ab

1.ab介绍

linux命令ab(E.g:apachebench)是apache自带的压力测试工具。ab命令会创建多个并发访问线程,模拟多个访问者同时对某一URL进行访问。由于ab命令测试是基于URL的,因此,它既可以用来测试apache httpd的负载压力,也可以对nginx/tomcat/iis等web服务器进行压力测试。

ab命令是基于httpd-tools,因此使用ab命令,需要先安装httpd-tools。

命令:

yum install httpd-tools

[root@centos79 ~]# yum install httpd-tools
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
CentOS79                                                                                                        | 3.6 kB  00:00:00     
软件包 httpd-tools-2.4.6-99.el7.centos.1.x86_64 已安装并且是最新版本
无须任何处理
[root@centos79 ~]# 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@centos79 ~]#

2.ab用法

ab [参数] [http[s]://]hostname[:port]/path

ab参数

参数

说明

-n

所执行的请求个数,默认为一次一个

-c

每次产生的请求个数,默认为一次一个

-t

指定每个请求的超时时间,默认为30s

3.实例

3.1.查看ab的帮助信息

命令:

ab -h

[root@centos79 ~]# ab -h
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)
[root@centos79 ~]#

3.2.使用ab命令进行压力测试(基于nginx)

命令:

ab -n1000 -c100 http://172.20.10.12/
 

[root@centos79 ~]# ab -n1000 -c100 http://172.20.10.12/
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 172.20.10.12 (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.24.0
Server Hostname:        172.20.10.12
Server Port:            80

Document Path:          /
Document Length:        615 bytes

Concurrency Level:      100
Time taken for tests:   0.038 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      848000 bytes
HTML transferred:       615000 bytes
Requests per second:    26250.16 [#/sec] (mean)
Time per request:       3.809 [ms] (mean)
Time per request:       0.038 [ms] (mean, across all concurrent requests)
Transfer rate:          21738.42 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   0.6      1       4
Processing:     1    3   0.9      3       9
Waiting:        0    2   0.9      2       8
Total:          2    3   1.1      3       9

Percentage of the requests served within a certain time (ms)
  50%      3
  66%      4
  75%      4
  80%      4
  90%      4
  95%      6
  98%      8
  99%      8
 100%      9 (longest request)
[root@centos79 ~]#

其中,

字段

说明

Server Software

表示被测试的web服务器软件名称

Server Hostname

表示请求的url主机名或ip地址

Server Port

表示被测试的web服务器软件的监听端口

Document Path

表示请求的URL中的根绝对路径

Document Length

表示HTTP响应数据的正文长度

Concurrency Level

表示并发用户数

Time taken for tests

表示所有这些请求被处理完成所花费的总时间

Complete requests

表示请求总数

Failed requests

表示失败的请求数量,这里的失败是指请求在连接服务器、发送数据等环节发生异常,以及无响应后超时的情况

Write errors

表示请求的写错误

Total transferred

表示所有请求的响应数据长度总和,包括每个 HTTP 响应数据的头信息和正文数据的长度。注意这里不包括 HTTP 请求数据的长度,仅仅为 web 服务器流向用户 PC 的应用层数据总长度。

HTML transferred

表示所有请求的响应数据中正文数据的总和,也就是减去了 Total transferred 中 HTTP 响应数据中的头信息的长度。

Requests per second

吞吐量,计算公式:Complete requests/Time taken for tests

Time per request(mean)

用户平均请求等待时间,计算公式:Time token for tests/(Complete requests/Concurrency Level)

Time per request(mean, across all concurrent requests)

服务器完成一个请求的时间,计算公式:Time per request/Concurrency Level

Transfer rate

表示网络传输速度,计算公式:Total transferred/Time taken for tests

Connection Times

本参数是对Time per request(mean)进行细分和统计。

请求的响应时间可以分成网络链接Connect,系统处理Processing和等待Waiting三个部分。表中 min 表示最小值; mean 表示平均值;[+/-sd]表示标准差(Standard Deviation),也称均方差(mean square error),数值越大表示数据越分散,系统响应时间越不稳定。 median 表示中位数; max表示最大值。
total 并不等于前三行数据相加,因为前三行的数据并不是在同一个请求中采集到的,可能某个请求的网络延迟最短,但是系统处理时间又是最长的呢。所以Total 是从整个请求所需要的时间的角度来统计的。

Percentage of requests served within a certain time(ms)

表示每个请求处理时间的分布情况,基于上述测试,90%的请求处理时间都不超过4ms

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年12月23日 0

暂无评论

wwLZeziuqjLR