Ubuntu下使用Apache Bench对网站进行压力测试

1. 什么是网站的压力测试

一般来说,衡量网站的性能指标,需要从以下几点进行考量:

  • 响应时间
  • 吞吐量
  • 并发量
  • 性能计数器

1.1 响应时间

响应时间抽象点来说可以是执行一个操作的从开始到完成的总耗时,包括:

  • 客户端和服务器传输 - 几十毫秒 ~ 几秒都有可能
  • 服务器处理请求查询数据库 - 几毫秒
  • 机械硬盘寻址 - 几毫秒
  • 机械硬盘1M数据 - 2毫秒
  • SSD读取1M数据 - 0.3 毫秒
  • 内存读取1M数据 - 10微秒
  • 程序简单逻辑处理 - 微秒

1.2 吞吐量

吞吐量指单位时间内系统处理的请求数量,体现系统的整体处理能力。

对于网站,可用“请求数/秒”、“页面数/秒”或“访问人数/天”、“处理业务数/小时”等来衡量。

重要指标有TPS(每秒处理的事物数)、QPS(每秒查询的请求数)、HPS(每秒HTTP请求数)等。

1.3 并发量

并发量指系统能够同时处理的请求的数目,这个数字反映了系统的负载性能。对于网站而言,并发数指网站用户同时提交请求的用户数目。

1.4 性能计数器

性能计数器描述服务器或操作系统性能的一些数据KPI指标。

如System Load、对象与线程数、内存使用、CPU使用、磁盘与网络I/O等使用情况。

通过对这些指标设置报警阈值,当监控系统发现性能计数器超过阈值时,就向开发人员和运维报警,及时发现异常并处理。

2. 安装Apache Bench对网站进行压力测试

ab是apache自带的压力测试工具。ab进行的一切测试本质上是基于HTTP的。

2.1 安装Apache Bench命令

sudo apt-get install apache2-utils

2.2 AB命令参数说明

Usage: ab [options] [http[s]://]hostname[:port]/path
Options are:

##总的请求数 
-n requests Number of requests to perform

##一次同时并发的请求数 总的请求数(n)=次数*一次并发数(c) 
-c concurrency Number of multiple requests to make

-t timelimit Seconds to max. wait for responses
-b windowsize Size of TCP send/receive buffer, in bytes
-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 for POSTing, 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.
-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 (SSL2, SSL3, TLS1, or ALL)

一般来说简单实用,我们只用关注 -n 和 -c 参数就行了。

2.3 测试兔子先生网站

ab -n 100 -c 10 https://www.xtuz.net/
Benchmarking www.xtuz.net (be patient).....done


Server Software:        nginx/1.16.1
Server Hostname:        www.xtuz.net
Server Port:            443
SSL/TLS Protocol:       TLSv1.2,ECDHE-RSA-CHACHA20-POLY1305,2048,256
TLS Server Name:        www.xtuz.net

# 页面路径
Document Path:          /

# 页面大小
Document Length:        14495 bytes

# 并发数目
Concurrency Level:      10

# 整个测试持续的时间 
Time taken for tests:   8.400 seconds

# 完成请求数
Complete requests:      100

# 失败请求数
Failed requests:        98
   (Connect: 0, Receive: 0, Length: 98, Exceptions: 0)
Total transferred:      1469695 bytes
HTML transferred:       1448495 bytes

# 每秒处理请求数
Requests per second:    11.90 [#/sec] (mean)

## 平均每个请求处理时间为840毫秒  注:这里将一次10个并发请求看成一个整体 
Time per request:       839.989 [ms] (mean)

## 平均每个并发请求处理 时间 为84毫秒 
Time per request:       83.999 [ms] (mean, across all concurrent requests)

## 传输速度
Transfer rate:          170.87 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        2    4   4.6      2      18
Processing:    74  740 462.6    628    2702
Waiting:       74  740 462.6    628    2702
Total:         91  744 461.5    631    2705

Percentage of the requests served within a certain time (ms)
## 在这100个请求中有50%在631毫秒内完成 
  50%    631
  66%    755
  75%    861
  80%    965
  90%   1368
  95%   1803
  98%   2355
  99%   2705
 100%   2705 (longest request)

发送100个请求,并发数为10

结论是 每秒可以处理11个请求

2.4 2. 测试时出现的Failed requests原因分析:

只要出现 Failed Request 就会对应有说明

Failed requests:        98
(Connect: 0, Receive: 0, Length: 98, Exceptions: 0)

分别有Connect、Receive、Length、Exceptions。

  • Connect 无法送出要求、目标主机连接失败、要求的过程中被中断。
  • Revieve 表示接受过程中被终端,例如主机主动断开连接

  • Length 响应的内容长度不一致 ( 以 Content-Length 头值为判断依据 )。

  • Exception 发生无法预期的错误。

兔子先生 测试失败请求都落在Length类别上,原因是动态页面,可以不用考虑


相关主题: