1. 压力测试的概念\定义
1、这段话是给刚接触\学习性能测试知识的初学者,在实际工作中都会接触到性能测试、压力测试、负载测试等专业名词也容易混淆,下面带大家熟悉下这到底是怎么定义:
1.1、性能测试【 Performance Test 】:是指通过自动化的测试工具模拟多种正常、峰值以及异常负载条件来对系统的各项性能指标进行测试;
1.2、压力测试【 stress testing 】:是指通过测试系统的瓶颈或者不能接受的性能点,来获得系统能提供的最大服务级别的测试;
1.3、负载测试【 Load Test 】:是指通过测试系统在资源超负荷情况下的表现,才以发现设计上的错误或验证系统的负载能力。
你们学习到更多的性能测试知识,会有更多的专业名词待你掌握,这篇文章只带大家熟悉 AB 压力测试工具,想知道 Loadrunner、Jmeter 等高级性能测试工具请关注我的Github给我提Issues,或下方留言
2. AB的历史
AB 全称【ApacheBench】是 Apache 自带的一款功能强大的测试工具,可以快速测试基于 HTTP 协议所有 Web 页面的最大负载压力,扩展性非常强,
下载地址:https://www.apachelounge.com/download/
下载相应系统的zip包,这里我演示的是 https://www.apachelounge.com/download/VC15/binaries/httpd-2.4.33-win64-VC15.zip ApacheBench Ver 2.3
3. 运行平台区分(Windows\Linux|Mac)
3.1、Windows 7、8、10运行方式
使用 cmd/Windows PowerShell,进入 Apache 下的 bin 目录,运行 ad 工具
cd ./Apache24/bin/
start ab
shell/Windows PowerShell会打印以下信息,即表明 ab 正常工作
Usage: ab [options] [http://]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)
3.2、Linux|Mac 运行方式
通过 shell 输入 cd /etc/httpd/bin 路径,运行 ad 工具
cd /etc/httpd/bin
start ab
4. 你的AB第一个压力测试脚本
在 cmd/shell 中输入以下两条命令:
ab -n 10 -c 10 http://127.0.0.1/
# -n 总共10次请求
# -c 是 concurrency ,模拟10个并发用户,即发起10个并发请求
ab -t 10 -c 10 http://127.0.0.1/
# -t 是 timelimit 执行测试的时间,单位是秒
# -c 是 concurrency ,模拟10个并发用户,即发起10个并发请求
#######
# 命令说明:
# 通过 ab 工具模拟用户浏览器行为,Get请求cnblogs网站并返回参数告诉ab你有没有访问我
# ab 的刨坑: 千万别再URL上缺少 http:// ,否者无法发起请求
结果分析:
root~/># ab -t 10 -c 10 http://127.0.0.1/
This is ApacheBench, Version 2.3 <$Revision: 1826891 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://127.0.0.1/ Licensed to The Apache Software Foundation, http://www.apache.org/
#
# ------ 上边的不重要 ------
#
Benchmarking 127.0.0.1 (be patient)
Finished 311 requests
# 一共发了 311 个请求
Server Software:
Server Hostname: 127.0.0.1
Server Port: 80
# 描述你测试 URL 和端口号通常是 80 端口
Document Path: /
Document Length: 147 bytes
# 描述你请求的页面的相关信息,页面大小为 147 字节
#----------
# 从这开始是 ab 的重点
#Concurrency Level: 10
# 并发请求数量为:10
Time taken for tests: 10.179 seconds
# 整个测试耗费 10.179 秒
Complete requests: 311
# 完成请求数有311次
Failed requests: 0
# 失败请求数量
Non-2xx responses: 311
# HTTP Code 不是2xx的
Total transferred: 118907 bytes
# 整个测试场景中产生的网络传输总量
HTML transferred: 45717 bytes
# 整个测试场景中 HTML 内容传输量
Requests per second: 30.55 [#/sec] (mean)
# 每秒的请求平均数,这是重要指标之一
Time per request: 327.299 [ms] (mean)
# 每个请求的平均时间,这是重要指标之一
Time per request: 32.730 [ms] (mean, across all concurrent requests)
# 服务器处理请求的平均时间,这是重要指标之一
Transfer rate: 11.41 [Kbytes/sec] received
# 网络平均转移率
Connection Times (ms)
min mean[+/-sd] median max
Connect: 16 31 10.0 31 94
Processing: 235 284 55.1 265 655
Waiting: 31 189 82.3 203 655
Total: 266 315 58.7 296 686
Percentage of the requests served within a certain time (ms)
50% 296
66% 300
75% 312
80% 312
90% 344
95% 360
98% 577
99% 640
100% 686 (longest request)
5. ab 中的 GET/POST请求
6. 你的AB第一个压力测试脚本