Web性能测试:AB 压力测试

发表于:2020-3-05 14:55

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:GI·Joe    来源:博客园

  1. 压力测试的概念\定义
  1、这段话是给刚接触\学习性能测试知识的初学者,在实际工作中都会接触到性能测试、压力测试、负载测试等专业名词也容易混淆,下面带大家熟悉下这到底是怎么定义:
  1.1、性能测试【 Performance Test 】:是指通过自动化的测试工具模拟多种正常、峰值以及异常负载条件来对系统的各项性能指标进行测试;
  1.2、压力测试【 stress testing 】:是指通过测试系统的瓶颈或者不能接受的性能点,来获得系统能提供的最大服务级别的测试;
  1.3、负载测试【 Load Test 】:是指通过测试系统在资源超负荷情况下的表现,才以发现设计上的错误或验证系统的负载能力。
  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 GET请求脚本
  在 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发起POST、PUT请求
  AB的POST请求需要在当前路径下创建个空的notepad文档,命名为【Postdata.txt】,这里演示模拟login操作
  【Postdata.txt】内容如下填写:
   user_name=root&password=123
  # 说明:
  # user_name 是服务器约定好的字段,解释:有个用户叫:root 名
  # password 是服务器约定好的字段,解释:有个root的用户密码是:123
  # 填写完成后直接保存
   ab用到的参数解释(为什么要用这些参数才能完成POST请求)
   -p 是参数文档路径<path>
  -T 是content-type,解释:这个一般指定POST\PUT请求头,例如:此次我们提交的是表单【application/x-www-form-urlencoded】
  post 表单格式为:application/x-www-form-urlencoded
  一般用 -p 参数都会带上 -T 指定请求头
  组合成命令是:
  ab -n 1 -c 1 -p /home/postdata.txt -T application/x-www-form-urlencoded "http://127.0.0.1/login"
  参数细节:
  -p 我放在文件路径是/home/postdata.txt 中
  -T 上边我们讲了这次我们做提交表单的操作所以必须是 application/x-www-form-urlencoded ,如果不带这个参数,服务器有可能会拒绝我们的请求
  后边就是我们请求的URL了
  当我们要修改的时候可以提出下边的参数:
  ab -n 1 -c 1 -u /home/postdata.txt -T application/x-www-form-urlencoded "http://127.0.0.1/login"
  此时的 -p 就替换成 -u 就可以完成Put 请求了
  复制代码
  6. AB附带有趣的参数(AB的细节)
  6.1、客户要求,一个页面在 1秒 内完成 10 个用户的 10次 并发请求,也就是说我们的命令是:
   ab -n 10 -c 10 <Test URL>
  # 怎么才能更直观的 1秒 内完成所有请求
  # ↓  ↓  ↓  ↓  ↓
  # 通过 -s 参数来执行
  -s 参数解释:我发起测试请求时计算计算,如果你不写这个参数,请求超时时间是30秒
  我们来完成客户要求我们做的测试:
  command:# ab -n 10 -c 10 -s 0.001  <Test URL>
  # 如果在1秒内能通过测试,就返回正常的测试数据
  # 如果不能通过测试,会返回下边的提示:
  This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
  Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  Licensed to The Apache Software Foundation, http://www.apache.org/
  Benchmarking 127.0.0.1 (be patient)...apr_pollset_poll: The timeout specified has expired (70007)
  # 意思是: apr_pollset_poll(指定的超时已过期)
  6.2、AB如果输出详细的测试信息:
   # ab 输出HTML测试报告
  在 ab 中加上 -w 参数,即可打印出HTML测试报告
  ab的测试报告截图:
  6.3、AB如果插入Cookies 和 Header:
   通过 ab 参数:-C 和 -H 两个参数实现
  -C 即是 Cookies的第一个大写字母,意思:就是添加cookies
  -H 即是 Header的第一个大写字母,意思:就是添加请求头信息
  用法:
  # ab -n 1 -c 1 -H '_name=_value' -C 'cookie1_name=cookie1_value'  <Test URL>
  # 这样就可以实现带表头、缓存的请求了
   6.4、AB请求带proxy:port
 ab -n 1  -c 1 -X 8080 <Test URL>
   7. AB参数中文翻译:
   ab参数翻译如下:
  -n 即requests,用于指定压力测试/总请求数。
  -c 即concurrency,用于指定压力测试的并发数。
  -t 即timelimit,测试执行最大秒数,它可以让测试限制在一个固定的总时间以内,默认值为50000。
  -s 即timeout,请求最大等待时长,默认30s
  -b 即windowsize,TCP发送/接收的缓冲大小(单位:字节)。
  -p 即postfile,发送POST请求时需要上传的文件,文件格式如"p1=1&p2=2"。使用方法是 -p 123.txt 。 (配合-T)
  -u 即putfile,发送PUT请求时需要上传的文件。(配合-T)
  -T 即content-type,用于设置Content-Type请求头信息,如 -T "application/x-www-form-urlencoded”,默认值为text/plain。(配合-p)
  -v 即verbosity,设置显示信息的详细程度 – 4或更大值会显示头信息, 3或更大值可以显示响应代码(404, 200等), 2或更大值可以显示警告和其他信息。
  -w 以HTML表格形式打印结果。
  -i 使用HEAD请求代替GET请求。
  -x 插入字符串作为table标签的属性。
  -y 插入字符串作为tr标签的属性。
  -z 插入字符串作为td标签的属性。
  -C 添加cookie信息,例如:"Apache=1234"。此参数可以重复,用逗号分割。提示:可以借助session实现原理传递 JSESSIONID参数, 实现保持会话的功能,如-C "c1=1234,c2=2,c3=3, JSESSIONID=FF056CD16DA9D71CB131C1D56F0319F8"。
  -H 添加任意的请求头,例如:"Accept-Encoding: gzip",请求头将会添加在现有的多个请求头之后(可以重复该参数选项以添加多个)。
  -A 添加一个基本的网络认证信息,用户名和密码之间用英文冒号隔开。
  -P 添加一个基本的代理认证信息,用户名和密码之间用英文冒号隔开。如-P proxy-auth-username:password
  -X 指定使用的代理服务器和端口号,例如:"126.10.10.3:88"。
  -V 显示版本号并退出。
  -k 使用HTTP的KeepAlive特性。
  -d 不显示百分比。
  -S 不显示预估和警告信息。
  -q 超过150个请求后不显示进度
  -l 接受可变文档长度(用于动态页面)
  -g filename 输出结果信息到gnuplot格式的文件中。
  -e filename 输出结果信息到CSV格式的文件中。
  -r 指定接收到错误信息时不退出程序。
  -m method 方法名
  -h 帮助


      本文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号