Linux 压测工具---ab安装及参数

ab压测工具

ab是apache bench命令的缩写。

原理:

ab命令会创建多个并发访问线程,模拟多个访问者同时对某一URL地址进行访问。它的测试目标是基于URL的,因此,它既可以用来测试apache的负载压力,也可以测试nginx、lighthttp、tomcat、IIS等其它Web服务器的压力。

ab命令对发出负载的计算机要求很低,它既不会占用很高CPU,也不会占用很多内存。但却会给目标服务器造成巨大的负载,其原理类似CC攻击。测试时也需要注意,一次性太大的负载。可能会造成目标服务器资源占用过高,严重时可能会导致死机。

使用ab命令但是不想安装apache,我们可以直接安装apache的工具包httpd-tools,这里已腾讯云的云服务器为例。

安装
yum -y install httpd-tools
参数
[root@VM-2-14-centos ~]# ab --help
ab: wrong number of arguments
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  ##用于测试的最大秒数,等价于 -n 等于50000
    -s timeout      Seconds to max. wait for each response
                    Default is 30 seconds  ##等待每个响应的最长秒数,默认值为30秒
    -b windowsize   Size of TCP send/receive buffer, in bytes  ##TCP发送/接收缓存区的大小(字节)
    -B address      Address to bind to when making outgoing connections   ##建立传出连接时要绑定的地址
    -p postfile     File containing data to POST. Remember also to set -T  ##包含POST数据的文件。还需要设置-T
    -u putfile      File containing data to PUT. Remember also to set -T   ##包含PUT数据的文件,还需要设置-T
    -T content-type Content-type header to use for POST/PUT data, eg.
                    'application/x-www-form-urlencoded'
                    Default is 'text/plain'  ## 用于POST/PUT数据的内容类型头。
    -v verbosity    How much troubleshooting info to print  ##设置显示信息的详细程度 - 4或更大值会显示头信息, 3或更大值可以显示响应代码(404, 200等), 2或更大值可以显示警告和其他信息。
    -w              Print out results in HTML tables  以HTML表的格式输出结果。默认时,它是白色背景的两列宽度的一张表
    -i              Use HEAD instead of GET  ##执行HEAD请求,而不是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) ##对请求附加一个Cookie行。 其典型形式是name=value的一个参数对。 此参数可以重复。 -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. ##对服务器提供BASIC认证信任。 用户名和密码由一个:隔开,并以base64编码形式发送。 无论服务器是否需要(即, 是否发送了401认证需求代码),此字符串都会被发送。 -P attribute Add Basic Proxy Authentication, the attributes are a colon separated username and password. ##对一个中转代理提供BASIC认证信任。 用户名和密码由一个:隔开,并以base64编码形式发送。 无论服务器是否需要(即, 是否发送了401认证需求代码),此字符串都会被发送 -X proxy:port Proxyserver and port number to use ##对请求使用代理服务器。 -V Print version number and exit ##显示版本号并退出。 -k Use HTTP KeepAlive feature ##启用HTTP KeepAlive功能,即, 在一个HTTP会话中执行多个请求。 默认时,不启用KeepAlive功能。 -d Do not show percentiles served table. ##显示”percentage served within XX [ms] table”的消息(为以前的版本提供支持)。 -S Do not show confidence estimators and warnings. ##不显示中值和标准背离值, 而且在均值和中值为标准背离值的1到2倍时,也不显示警告或出错信息。 默认时,会显示 最小值/均值/最大值等数值。(为以前的版本提供支持)。 -q Do not show progress when doing more than 150 requests ##如果处理的请求数大于150, ab每处理大约10%或者100个请求时,会在stderr输出一个进度计数。 此-q标记可以抑制这些信息。 -l Accept variable document length (use this for dynamic pages) ##接受可变文档长度(用于动态页面) -g filename Output collected data to gnuplot format file. ##把所有测试结果写入一个’gnuplot’或者TSV (以Tab分隔的)文件。 此文件可以方便地导入到Gnuplot, IDL, Mathematica, Igor甚至Excel中。 其中的第一行为标题。 -e filename Output CSV file with percentages served ##产生一个以逗号分隔的(CSV)文件, 其中包含了处理每个相应百分比的请求所需要(从1%到100%)的相应百分比的(以微妙为单位)时间。 由于这种格式已经“二进制化”,所以比’gnuplot’格式更有用。 -r Don't exit on socket receive errors. ##套接字接收错误时不退出。 -m method Method name ##方法名称 -h Display usage information (this message) ##显示使用方法。 -I Disable TLS Server Name Indication (SNI) extension ##禁用TLS服务器名称指示(SNI)扩展 -Z ciphersuite Specify SSL/TLS cipher suite (See openssl ciphers) ##指定SSL/TLS密码套件。 -f protocol Specify SSL/TLS protocol (SSL2, TLS1, TLS1.1, TLS1.2 or ALL) ##指定SSL/TLS协议(SSL2、TLS1、TLS1.1、TLS1.2或ALL) -E certfile Specify optional client certificate chain and private key ##指定可选的客户端证书链和私钥
压测返回结果解析

此处以该云服务器所属同一VPC的另一台云服务器为例:

ab -n 100 -c 100 http://172.16.1.6/

host之后必须有”/“,表示此目录而不是此文件;

[root@VM-1-4-centos ~]# ab -n 100 -c 100 http://172.16.1.6/
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.16.1.6 (be patient).....done


Server Software:        Apache/2.4.37   
Server Hostname:        172.16.1.6  ##域名或主机地址
Server Port:            80  ##请求端口号

Document Path:          /  ##文档路径
Document Length:        199691 bytes  页面字节数

Concurrency Level:      100  ##请求的并发数
Time taken for tests:   0.042 seconds  ##总访问时间
Complete requests:      100  ##请求成功数量
Failed requests:        0   ##请求失败数量
Non-2xx responses:      100   ##非2xx响应
Total transferred:      19996600 bytes  ##请求总数据大小(包括header头信息)
HTML transferred:       19969100 bytes  ##HTML页面实际总字节数
Requests per second:    2370.23 [#/sec] (mean)  ##每秒多少请求,这个是非常重要的参数数值,服务器的吞吐量
Time per request:       42.190 [ms] (mean)  ##用户平均请求等待时间
Time per request:       0.422 [ms] (mean, across all concurrent requests)  ##服务器平均处理时间,也就是服务器吞吐量的倒数
Transfer rate:          462856.83 [Kbytes/sec] received  ##每秒获取的数据长度

Connection Times (ms)  ##连接时间(ms)
              min  mean[+/-sd] median   max
Connect:        0    1   0.4      1       2   ##连接时间
Processing:     4   26  11.5     30      39   ##处理时间
Waiting:        0    6   4.1      5      13   ##等待时间
Total:          4   28  11.2     31      40   ##总时间
##           最小值  平均值        中值   最大值
Percentage of the requests served within a certain time (ms)
  50%     31     ##50%用户请求在31ms内返回
  66%     37     ##66%用户请求在37ms内返回
  75%     38     ##75%用户请求在38ms内返回
  80%     39     ##80%用户请求在39ms内返回
  90%     39     ##90%用户请求在39ms内返回
  95%     40     ##95%用户请求在40ms内返回
  98%     40     ##98%用户请求在40ms内返回
  99%     40     ##99%用户请求在40ms内返回
 100%     40 (longest request)     ##100%用户请求在40ms内返回

1、安装apache

yum install httpd #根据提示,输入Y安装即可成功安装

systemctl start httpd.service #启动apache

systemctl stop httpd.service #停止apache

systemctl restart httpd.service #重启apache

systemctl enable httpd.service #设置apache开机启动

2、启动http服务

httpd -version #检查是否安装 

yum install httpd #安装http服务

service httpd start #启动http服务

service httpd status #检查http服务状态(会显示绿色的active(running)表示启动成功)

service httpd restart #重启http服务

使用工具时报错apr_socket_recv: Connection refused (111),主要是因为没有监听端口,源端发送ack报文至目端的80端口,目端回复rst,源端视为拒绝连接,所以只需要开启HTTP服务,监听80端口即可;

你可能感兴趣的:(linux,linux,服务器,运维)