Linux使用ab进行并发压力测试

简介介绍

Apache Benchmark (简称ab) 是Apache安装包中自带的压力测试工具 ,简单易用。 使用起来非常的简单和方便。
ab是apache自带的压力测试工具。ab非常实用,它不仅可以对apache服务器进行网站访问压力测试,也可以对或其它类型的服务器进行压力测试。比如nginx、tomcat、IIS等。

原理

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

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

安装

不同版本的指令不同
ubuntu:

	apt install apache2-utils -y

centos:

	yum -y install httpd-tools
	
	# 安装依赖 yum-utils中的yumdownload 工具
	# 如果没有找到 yumdownload 命令可以
	yum install yum-utils

安装是否成功检验
执行指令:ab -V

(base) root@gzk8s-master1:# ab -V
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/

参数说明

执行指令

# ab --help
或
# man ab
(base) root@gzk8s-master1:# 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
    -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
    -l              Accept variable document length (use this for dynamic pages)
    -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.
    -m method       Method name
    -h              Display usage information (this message)
    -I              Disable TLS Server Name Indication (SNI) extension
    -Z ciphersuite  Specify SSL/TLS cipher suite (See openssl ciphers)
    -f protocol     Specify SSL/TLS protocol
                    (SSL2, TLS1, TLS1.1, TLS1.2 or ALL)
    -E certfile     Specify optional client certificate chain and private key

对相关参数进行说明:
-n 即requests,用于指定压力测试总共的执行次数。
-c 即concurrency,用于指定的并发数。
-t 即timelimit,等待响应的最大时间(单位:秒)。
-b 即windowsize,TCP发送/接收的缓冲大小(单位:字节)。
-p 即postfile,发送POST请求时需要上传的文件,此外还必须设置-T参数。
-u 即putfile,发送PUT请求时需要上传的文件,此外还必须设置-T参数。
-T 即content-type,用于设置Content-Type请求头信息,例如:application/x-www-form-urlencoded,默认值为text/plain。
-v 即verbosity,指定打印帮助信息的冗余级别。
-w 以HTML表格形式打印结果。
-i 使用HEAD请求代替GET请求。
-x 插入字符串作为table标签的属性。
-y 插入字符串作为tr标签的属性。
-z 插入字符串作为td标签的属性。
-C 添加cookie信息,例如:“Apache=1234”(可以重复该参数选项以添加多个)。
-H 添加任意的请求头,例如:“Accept-Encoding: gzip”,请求头将会添加在现有的多个请求头之后(可以重复该参数选项以添加多个)。
-A 添加一个基本的网络认证信息,用户名和密码之间用英文冒号隔开。
-P 添加一个基本的代理认证信息,用户名和密码之间用英文冒号隔开。
-X 指定使用的和端口号,例如:“126.10.10.3:88”。
-V 打印版本号并退出。
-k 使用HTTP的KeepAlive特性。
-d 不显示百分比。
-S 不显示预估和警告信息。
-g 输出结果信息到gnuplot格式的文件中。
-e 输出结果信息到CSV格式的文件中。
-r 指定接收到错误信息时不退出程序。
-h 显示用法信息,其实就是ab -help。

性能指标

在进行性能测试过程中有几个指标比较重要:

1. 吞吐率(Requests per second)

服务器并发处理能力的量化描述,单位是reqs/s,指的是在某个并发用户数下单位时间内处理的请求数。某个并发用户数下单位时间内能处理的最大请求数,称之为最大吞吐率。

记住:吞吐率是基于并发用户数的。这句话代表了两个含义:
a、吞吐率和并发用户数相关
b、不同的并发用户数下,吞吐率一般是不同的

计算公式:总请求数/处理完成这些请求数所花费的时间,即
Request per second = Complete requests / Time taken for tests

必须要说明的是,这个数值表示当前机器的整体性能,值越大越好。

2.并发连接数(The number of concurrent connections)

并发连接数指的是某个时刻服务器所接受的请求数目,简单的讲,就是一个会话。

3.并发用户数(Concurrency Level)

要注意区分这个概念和并发连接数之间的区别,一个用户可能同时会产生多个会话,也即连接数。在HTTP/1.1下,IE7支持两个并发连接,IE8支持6个并发连接,FireFox3支持4个并发连接,所以相应的,我们的并发用户数就得除以这个基数。

4.用户平均请求等待时间(Time per request)

计算公式:处理完成所有请求数所花费的时间/(总请求数/并发用户数),即:
Time per request = Time taken for tests /(Complete requests / Concurrency Level)

5.服务器平均请求等待时间(Time per request:across all concurrent requests)

计算公式:处理完成所有请求数所花费的时间/总请求数,即:Time taken for / testsComplete requests
可以看到,它是吞吐率的倒数。

同时,它也等于用户平均请求等待时间/并发用户数,即 Time per request /Concurrency Level

实际使用

简单使用说明
最基本的两个选项 -c -n

例子: ab -n 1000 -c 100 http://www.baidu.com/

-n 1000 即: 共发送1000个请求
-c 100 即:每次并发100个

测试结果:(该测试是基于k8s平台上关于hpa研究过程中的测试)

开启压测,进行对测试的cluster IP进行测试。

(base) root@gzk8s-master1:# kubectl get svc -n ztb-test
NAME       TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
test-hpa   ClusterIP   10.103.15.188   <none>        8080/TCP   32d

执行压测命令

(base) root@gzk8s-master1:/home/cyye# ab -n 1000000 -c 10 http://10.103.15.188:8080/hello
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 10.103.15.188 (be patient)
Completed 100000 requests
Completed 200000 requests
Completed 300000 requests
Completed 400000 requests
Completed 500000 requests
Completed 600000 requests
Completed 700000 requests
Completed 800000 requests
Completed 900000 requests
Completed 1000000 requests
Finished 1000000 requests


Server Software:        
Server Hostname:        10.103.15.188
Server Port:            8080

Document Path:          /hello
Document Length:        44 bytes

Concurrency Level:      10
Time taken for tests:   82.861 seconds
Complete requests:      1000000
Failed requests:        0
Total transferred:      177000000 bytes
HTML transferred:       44000000 bytes
Requests per second:    12068.41 [#/sec] (mean)
Time per request:       0.829 [ms] (mean)
Time per request:       0.083 [ms] (mean, across all concurrent requests)
Transfer rate:          2086.04 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       7
Processing:     0    1   0.3      1      17
Waiting:        0    0   0.3      0      16
Total:          0    1   0.4      1      18

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      1
  90%      1
  95%      1
  98%      2
  99%      2
 100%     18 (longest request)

结果分析

服务器信息

Server Software表示被测试的Web服务器软件名称。

Server Hostname表示请求的URL主机名。

Server Port表示被测试的Web服务器软件的监听端口。

服务域名是10.103.15.188,端口号8080端口。

Server Software:        
Server Hostname:        10.103.15.188
Server Port:            8080

文档信息

Document Path表示请求的URL中的根绝对路径,通过该文件的后缀名,我们一般可以了解该请求的类型。

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

文档路径 /hello,文档长度44字节(http相应的正文长度)。

Document Path:          /hello
Document Length:        44 bytes

重要指标

Concurrency Level表示并发用户数,这是我们设置的参数之一。

Time taken for tests表示所有这些请求被处理完成所花费的总时间。

Complete requests表示总请求数量,这是我们设置的参数之一。

Failed requests表示失败的请求数量,这里的失败是指请求在连接服务器、发送数据等环节发生异常,以及无响应后超时的情况。如果接收到的HTTP响应数据的头信息中含有2XX以外的状态码,则会在测试结果中显示另一个名为“Non-2xx responses”的统计项,用于统计这部分请求数,这些请求并不算在失败的请求中。

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

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

Concurrency Level:      10  // 10个并发请求
Time taken for tests:   82.861 seconds  // 整个测试持续时间
Complete requests:      1000000  // 完成了1000000个请求
Failed requests:        0  // 失败的请求数0
Total transferred:      177000000 bytes  // 整个场景中的网络传输量
HTML transferred:       44000000 bytes  // 整个场景中的HTML内容传输量
Requests per second:     12068.41 [#/sec] (mean)  // 吞吐率(大家最关心的指标之一),详情上述。
Time per request:       0.829 [ms] (mean)  // 用户平均请求等待时间(大家最关心的指标之二),详情上述。
Time per request:       0.083 [ms] (mean, across all concurrent requests)  // 服务器平均请求处理时间(大家最关心的指标之三),详情上述。
Transfer rate:          2086.04 [Kbytes/sec] received  // 平均每秒网络上的流量,可以帮助排除是否存在网络流量过大导致响应时间延长的问题。

网络上消耗的时间的分解

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       7
Processing:     0    1   0.3      1      17
Waiting:        0    0   0.3      0      16
Total:          0    1   0.4      1      18

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

需要注意的是表中的Total并不等于前三行数据相加,因为前三行的数据并不是在同一个请求中采集到的,可能某个请求的网络延迟最短,但是系统处理时间又是最长的呢。所以Total是从整个请求所需要的时间的角度来统计的。这里可以看到最慢的一个请求花费了2ms(即100% 2 (longest request))。

每个请求处理时间的分布情况,50%的处理时间在512ms内,66%的处理时间在603ms内…,重要的是看90%的处理时间:

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      1
  90%      1
  95%      1
  98%      2
  99%      2
 100%     18 (longest request)

  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值