使用ab对网站进行压测

ab压测

  • ab是apache自带的压力测试工具。ab非常实用,它不仅可以对apache服务器进行网站访问压力测试,也可对以或其它类型的服务器进行压力测试。比如nginx、tomcat、IIS等。
原理:
  • ab命令会创建多个并发访问线程,模拟多个访问者同时对某一URL地址进行访问。
概念
  • 吞吐率(Requests per second)QPS

    • 概念:服务器并发处理能力的量化描述,单位是reqs/s,指的是某个并发用户数下单位时间内处理的请求数。某个并发用户数下单位时间内能处理的最大请求数,称之为最大吞吐率。
      计算公式:总请求数 / 处理完成这些请求数所花费的时间,即
      Request per second = Complete requests / Time taken for tests
  • 并发连接数(The number of concurrent connections)

    • 概念:某个时刻服务器所接受的请求数目,简单的讲,就是一个会话。
  • 并发用户数(The number of concurrent users,Concurrency Level)

    • 概念:要注意区分这个概念和并发连接数之间的区别,一个用户可能同时会产生多个会话,也即连接数。
  • 用户平均请求等待时间(Time per request)

    • 计算公式:处理完成所有请求数所花费的时间/ (总请求数 / 并发用户数),即
      Time per request = Time taken for tests /( Complete requests / Concurrency Level)
  • 服务器平均请求等待时间(Time per request: across all concurrent requests)

    • 计算公式:处理完成所有请求数所花费的时间 / 总请求数,即
      Time taken for / testsComplete requests
      可以看到,它是吞吐率的倒数。
      同时,它也=用户平均请求等待时间/并发用户数,即
      Time per request / Concurrency Level
安装
1、linux
# 1、安装apache
$ yum -y install httpd
# 2、不按照apache 只安装apache的工具包httpd-tools
$ yum -y install httpd-tools
# 3、验证
	# 如果ab安装成功,通过ab –V命令则会显示ab的相应版本
$ ab -V
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
2、windows
  • 进入apache官网 http://httpd.apache.org/ 下载apache即可

  • ab命令在安装目录的bin目录下

使用
1、命令及参数
# 模拟100个用户同时请求1000次
ab -n 1000 -c 100 https://www.baidu.com/

		-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
                    (TLS1, TLS1.1, TLS1.2 or ALL)
    -E certfile     Specify optional client certificate chain and private key
 
2、输出详解:
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
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)
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:        BWS/1.1 # 被测试的服务器所用的软件信息,这里使用的是百度自己开发的反向代理Baidu Front End,类似nginx。
Server Hostname:        www.baidu.com # 被测主机名
Server Port:            443 # 被测主机的服务端口号,一般http请求的默认端口号是80,https默认使用443端口
SSL/TLS Protocol:       TLSv1.2,ECDHE-RSA-AES128-GCM-SHA256,2048,128 # 加密协议


Server Temp Key:        ECDH P-256 256 bits
TLS Server Name:        www.baidu.com

Document Path:          / # 请求的具体文件(本次请求的是根目录)
Document Length:        227 bytes # 请求的文件index.html大小

Concurrency Level:      100 # 并发级别,也就是并发数,请求中-c参数指定的数量
Time taken for tests:   4.744 seconds # 本次测试总共花费的时间
Complete requests:      1000 # 本次测试总共发起的请求数量
Failed requests:        0 # 失败的请求数量。因网络原因或服务器性能原因,发起的请求并不一定全部成功,通过该数值和Complete requests相除可以计算请求的失败率,作为测试结果的重要参考。
Total transferred:      1110915 bytes # 总共传输的数据量,指的是ab从被测服务器接收到的总数据量,包括index.html的文本内容和请求头信息。
HTML transferred:       227000 bytes # 从服务器接收到的index.html文件的总大小,等于Document Length*Complete requests=227 bytes*100=22700 bytes

Requests per second:    210.78 [#/sec] (mean) # 平均(mean)每秒完成的请求数:QPS,这是一个平均值,等于Complete requests/Time taken for tests=1000/4.744= 210.78
Time per request:       474.435 [ms] (mean) # 从用户角度看,完成一个请求所需要的时间(因用户数量不止一个,服务器完成100个请求,平均每个用户才接收到一个完整的返回,所以该值是下一项数值的10倍。)
Time per request:       4.744 [ms] (mean, across all concurrent requests) #  服务器完成一个请求的时间。
Transfer rate:          228.67 [Kbytes/sec] received # 网络传输速度。对于大文件的请求测试,这个值很容易成为系统瓶颈所在。要确定该值是不是瓶颈,需要了解客户端和被测服务器之间的网络情况,包括网络带宽和网卡速度等信息。

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       87  344  89.5    371     522
Processing:    14   80  84.6     33     349
Waiting:       14   74  75.6     33     288
Total:        113  424  46.9    407     598

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

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


Percentage of the requests served within a certain time (ms)
  50%    407
  66%    412
  75%    422
  80%    446
  90%    510
  95%    534
  98%    561
  99%    572
 100%    598 (longest request)
 
 # 这个表第一行表示有50%的请求都是在407ms内完成的,可以看到这个值是比较接近平均系统响应时间(第一个Time per request:       474.435 [ms] (mean) )以此类推,90%的请求是小于等于510ms的。刚才我们看到响应时间最长的那个请求是598ms,那么显然所有请求(100%)的时间都是小于等于598毫秒的,也就是表中最后一行的数据肯定是时间最长的那个请求(longest request)。

报错
Too many open files
解决:
# 查看当前打开文件数限制 
$ ulimit -a 

-t: cpu time (seconds)              unlimited
-f: file size (blocks)              unlimited
-d: data seg size (kbytes)          unlimited
-s: stack size (kbytes)             8192
-c: core file size (blocks)         0
-m: resident set size (kbytes)      unlimited
-u: processes                       126388
-n: file descriptors                1024
-l: locked-in-memory size (kbytes)  64
-v: address space (kbytes)          unlimited
-x: file locks                      unlimited
-i: pending signals                 126388
-q: bytes in POSIX msg queues       819200
-e: max nice                        0
-r: max rt priority                 0
-N 15:                              unlimited

# 临时解决:修改 open files 数量 :
$ ulimit -n 65535 
# 永久
$ echo "* soft nofile 65535" >> /etc/security/limits.conf
$ echo "* hard nofile 65535" >> /etc/security/limits.conf
$ ulimit -n 65535 
# 验证
$ ulimit -a

-t: cpu time (seconds)              unlimited
-f: file size (blocks)              unlimited
-d: data seg size (kbytes)          unlimited
-s: stack size (kbytes)             8192
-c: core file size (blocks)         0
-m: resident set size (kbytes)      unlimited
-u: processes                       126388
-n: file descriptors                65535
-l: locked-in-memory size (kbytes)  64
-v: address space (kbytes)          unlimited
-x: file locks                      unlimited
-i: pending signals                 126388
-q: bytes in POSIX msg queues       819200
-e: max nice                        0
-r: max rt priority                 0
-N 15:                              unlimited


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值