欢迎来到小居数码网-一家分享数码知识,生活小常识的网站,希望可以帮助到您。

当前位置:生活小常识 > 数码知识 >
优质

AB压力测试(ab压测工具)

数码知识

周璟瑜优秀作者

原创内容 来源:小居数码网 时间:2024-08-07 22:07:01 阅读() 收藏:21 分享:55

导读:您正在阅读的是关于【数码知识】的问题,本文由科普作家协会,生活小能手,著名生活达人等整理监督编写。本文有4475个文字,大小约为17KB,预计阅读时间12分钟。

在学习ab工具之前,我们需了解几个关于压力测试的概念

ab压测工具

  1. 吞吐率(Requests per second)

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

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

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

概念:某个时刻服务器所接受的请求数目,简单的讲,就是一个会话。

  1. 并发用户数(The number of concurrent users,Concurrency Level)

概念:要注意区分这个概念和并发连接数之间的区别,一个用户可能同时会产生多个会话,也即连接数。

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

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

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

计算公式:处理完成所有请求数所花费的时间 / 总请求数,即 Time taken for / testsComplete requests。可以看到,它是吞吐率的倒数,同时,它也=用户平均请求等待时间/并发用户数,即 Time per request / Concurrency Level。

ab工具简介

ab全称为:apache bench

  • 在官网上的解释如下:
  • ab是Apache超文本传输协议(HTTP)的性能测试工具。其设计意图是描绘当前所安装的Apache的执行性能,主要是显示你安装的Apache每秒可以处理多少个请求。

  • 其他网站解释:
  • ab是apache自带的压力测试工具。ab非常实用,它不仅可以对apache服务器进行网站访问压力测试,也可以对或其它类型的服务器进行压力测试。比如nginx、tomcat、IIS等。

    下载ab工具

    Window官方下载地址:https:///cgi-bin/download.plx

    下载完成后解压,修改解压根目录下的conf/httpd.conf文件的端口配置,默认是80端口,应该是被占用了,无法安装,可以自行修改为其他,我在这里修改为8088端口,然后输入命令安装:httpd -k install

    启动ab工具

    如果你需要在命令行的任意路径下可以输入ab测试,可以把bin目录路径加到环境变量的path中去,我是在bin目录下打开命令行的

    开始测试

    输入命令ab -n 100 -c 10 http://test.com/ 其中-n表示请求数,-c表示并发数

    其余命令请参见 http://apache.jz123.cn/programs/ab.html

    ab参数说明

    有关 ab 命令的使用,我们可以通过帮助命令进行查看。如下:

    [root@localhost bin]# ab --helpab: wrong number of argumentsUsage: ab [options] [http[s]://]hostname[:port]/pathOptions 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    -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                    (SSL3, TLS1, TLS1.1, TLS1.2 or ALL)

    下面我们对这些参数,进行相关说明。如下:

    参数

    说明

    -n

    在测试会话中所执行的请求个数。默认时,仅执行一个请求。

    -c

    一次产生的请求个数。默认是一次一个。

    -t

    测试所进行的最大秒数。其内部隐含值是 -n 50000,它可以使对服务器的测试限制在一个固定的总时间以内。默认时,没有时间限制。

    -p

    包含了需要 POST 的数据的文件。

    -P

    对一个中转代理提供 BASIC 认证信任。用户名和密码由一个:隔开,并以 base64 编码形式发送。无论服务器是否需要(即, 是否发送了 401 认证需求代码),此字符串都会被发送。

    -T

    POST数据所使用的 Content-type 头信息。

    -v

    设置显示信息的详细程度 -4 或更大值会显示头信息,3 或更大值可以显示响应代码(404, 200等), 2或更大值可以显示警告和其他信息。

    -V

    显示版本号并退出。

    -w

    以 HTML 表的格式输出结果。默认时,它是白色背景的两列宽度的一张表。

    -i

    执行 HEAD 请求,而不是 GET。

    -X

    对请求使用代理服务器。

    -x

    设置 属性的字符串。

    -y

    设置

    -z

    设置

    属性的字符串。

    -C

    对请求附加一个 Cookie 行。其典型形式是 name=value 的一个参数对,此参数可以重复。

    -H

    对请求附加额外的头信息。此参数的典型形式是一个有效的头信息行,其中包含了以冒号分隔的字段和值的对(如,“Accept-Encoding:zip/zop;8bit”)。

    -A

    对服务器提供 BASIC 认证信任。用户名和密码由一个:隔开,并以 base64 编码形式发送。无论服务器是否需要(即,是否发送了401认证需求代码),此字符串都会被发送。

    -h

    显示使用方法。

    -d

    不显示 “percentage served within XX [ms] table” 的消息(为以前的版本提供支持)。

    -e

    产生一个以逗号分隔的(CSV)文件,其中包含了处理每个相应百分比的请求所需要(从1%到100%)的相应百分比的(以微妙为单位)时间。由于这种格式已经“二进制化”,所以比 gnuplot 格式更有用。

    -g

    把所有测试结果写入一个 gnuplot 或者TSV(以Tab分隔的)文件。此文件可以方便地导入到Gnuplot,IDL,Mathematica,Igor 甚至 Excel 中。其中的第一行为标题。

    -k

    启用 HTTP KeepAlive 功能,即在一个 HTTP 会话中执行多个请求。默认时,不启用 KeepAlive 功能。

    -q

    如果处理的请求数大于 150,ab 每处理大约 10% 或者 100 个请求时,会在 stderr 输出一个进度计数。此 -q 标记可以抑制这些信息。

    属性的字符串。

    测试结果分析

    上面的命令运行完毕后就出来测试报告了

    这段展示的是web服务器的信息,可以看到服务器采用的是nginx,域名是wan.bigertech.com,端口是80

    这段是关于请求的文档的相关信息,所在位置“/”,文档的大小为338436 bytes(此为http响应的正文长度)

    这段展示了压力测试的几个重要指标

  • Concurrency Level: 100 ==>并发请求数
  • Time taken for tests: 50.872 seconds ==>整个测试持续的时间
  • Complete requests: 1000 ==>完成的请求数
  • Failed requests: 0 ==>失败的请求数
  • Total transferred: 13701482 bytes ==>整个场景中的网络传输量
  • HTML transferred: 13197000 bytes ==>整个场景中的HTML内容传输量
  • Requests per second: 19.66 [#/sec] (mean) ==>吞吐率,大家最关心的指标之一,相当于 LR 中的每秒事务数,后面括号中的 mean 表示这是一个平均值
  • Time per request: 5087.180 [ms] (mean) ==>用户平均请求等待时间,大家最关心的指标之二,相当于 LR 中的平均事务响应时间,后面括号中的 mean 表示这是一个平均值
  • Time per request: 50.872 [ms] (mean, across all concurrent requests) ==>服务器平均请求处理时间,大家最关心的指标之三
  • Transfer rate: 263.02 [Kbytes/sec] received ==>平均每秒网络上的流量,可以帮助排除是否存在网络流量过大导致响应时间延长的问题
  • 这段表示网络上消耗的时间的分解
  • 这段是每个请求处理时间的分布情况,50%的处理时间在4930ms内,66%的处理时间在5008ms内…,重要的是看**90%**的处理时间。
  • 关于登录的问题

    有时候进行压力测试需要用户登录,怎么办? 请参考以下步骤:

    1. 先用账户和密码登录后,用开发者工具找到标识这个会话的Cookie值(Session ID)记下来
    2. 如果只用到一个Cookie,那么只需键入命令: ab -n 100 -C key=value http://test.com/ 如果需要多个Cookie,就直接设Header: ab -n 100 -H “Cookie: Key1=Value1; Key2=Value2” http://test.com/

    总结

    总的来说ab工具ab小巧简单,上手学习较快,可以提供需要的基本性能指标,但是没有图形化结果,不能监控。因此ab工具可以用作临时紧急任务和简单测试。

    同类型的压力测试工具还有:webbench、siege、http_load等

    开始测试

    如果你需要在命令行的任意路径下可以输入ab测试,可以把bin目录路径加到环境变量的path中去

    我是在bin目录下打开命令行的 注意,下面的是错误的,因为需要在域名后加上/path

    ab -n 100 -c 10 http://

    报错信息如下:

    ab: invalid URLUsage: ab [options] [http://]hostname[:port]/path

    必须要接有path 所以我换一下测试命令:

    ab -n 100 -c 10 http:///s

    -n 表示请求数,-c 表示并发数. s为path,表示指定测试地址,不指定可能会报”ab: invalid url” 错误. 另外还有-t 表示多少s内并发和请求

    测试出来的数据如下:

    D:apacheabApache24bin>ab -n 100 -c 10 http:///sThis is ApacheBench, Version 2.3 <$Revision: 1807734 gt; Copyright 1996 Adam Twiss, Zeus Technology Ltd, http:// (be patient).....doneServer Software:        BWS/1.1   ##服务器软件和版本Server Hostname:          ##请求的地址/域名Server Port:            80   ##端口Document Path:          /s  ##请求的路径Document Length:        112435 bytes  ##页面数据/返回的数据量Concurrency Level:      10   ##并发数Time taken for tests:   4.764 seconds  ##共使用了多少时间 Complete requests:      100  ##请求数 Failed requests:        99  ##失败请求  百度为什么失败这么多,应该是百度做了防范     (Connect: 0, Receive: 0, Length: 99, Exceptions: 0)Total transferred:      11342771 bytes  ##总共传输字节数,包含http的头信息等 HTML transferred:       11247622 bytes  ##html字节数,实际的页面传递字节数 Requests per second:    20.99 [#/sec] (mean)  ##每秒多少请求,这个是非常重要的参数数值,服务器的吞吐量 Time per request:       476.427 [ms] (mean)   ##用户平均请求等待时间 Time per request:       47.643 [ms] (mean, across all concurrent requests)  ##服务器平均处理时间,也就是服务器吞吐量的倒数 Transfer rate:          2325.00 [Kbytes/sec] received ##每秒获取的数据长度Connection Times (ms)              min  mean[+/-sd] median   maxConnect:       22   41  12.4     39      82##连接的最小时间,平均值,中值,最大值Processing:   113  386 211.1    330    1246##处理时间Waiting:       25   80  43.9     73     266##等待时间Total:        152  427 210.1    373    1283##合计时间Percentage of the requests served within a certain time (ms)  50%    373   ## 50%的请求在373ms内返回   66%    400   ## 60%的请求在400ms内返回   75%    426  80%    465  90%    761  95%    930  98%   1192  99%   1283 100%   1283 (longest request)

    因为ab工具消耗小,所以有些人也用来进行ddos攻击,算一种ddos攻击工具

    [Linux]ApacheBench(ab)压力测试工具

    AB的简介

    ab是apachebench命令的缩写。

    ab是apache自带的压力测试工具。ab非常实用,它不仅可以对apache服务器进行网站访问压力测试,也可以对或其它类型的服务器进行压力测试。比如nginx、tomcat、IIS等

    ab的原理

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

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

    ab的安装

    安装命令:yum -y install httpd-tools 查看版本命令:ab -V 查看参数说明:ab --help

    ab的使用 常用方式:ab -c 500 -n 5000 http://localhost/

    [root@H__D /]# ab -c 500 -n 5000 http://localhost/This is ApacheBench, Version 2.3 <$Revision: 1430300 gt;Copyright 1996 Adam Twiss, Zeus Technology Ltd, http:///Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking localhost (be patient)Completed 500 requestsCompleted 1000 requestsCompleted 1500 requestsCompleted 2000 requestsCompleted 2500 requestsCompleted 3000 requestsCompleted 3500 requestsCompleted 4000 requestsCompleted 4500 requestsCompleted 5000 requestsFinished 5000 requestsServer Software:        nginx/1.15.3        #测试服务器的名字Server Hostname:        localhost            #请求的URL主机名Server Port:            80                          #请求端口Document Path:          /                        #请求路径Document Length:        613 bytes          #HTTP响应数据的正文长度Concurrency Level:      500                    #并发用户数,这是我们设置的参数之一Time taken for tests:   0.407 seconds     #所有这些请求被处理完成所花费的总时间 单位秒Complete requests:      5000                  #总请求数量,这是我们设置的参数之一Failed requests:        0                           #表示失败的请求数量Write errors:           0                            Total transferred:      4230000 bytes       #所有请求的响应数据长度总和。包括每个HTTP响应数据的头信息和正文数据的长度HTML transferred:       3065000 bytes     #所有请求的响应数据中正文数据的总和,也就是减去了Total transferred中HTTP响应数据中的头信息的长度Requests per second:    12284.44 [#/sec] (mean)    #吞吐量,计算公式:Complete requests/Time taken for tests  总请求数/处理完成这些请求数所花费的时间Time per request:       40.702 [ms] (mean)             #用户平均请求等待时间,计算公式:Time token for tests/(Complete requests/Concurrency Level)。处理完成所有请求数所花费的时间/(总请求数/并发用户数)     Time per request:       0.081 [ms] (mean, across all concurrent requests)    #服务器平均请求等待时间,计算公式:Time taken for tests/Complete requests,正好是吞吐率的倒数。也可以这么统计:Time per request/Concurrency LevelTransfer rate:          10149.06 [Kbytes/sec] received。  #表示这些请求在单位时间内从服务器获取的数据长度,计算公式:Total trnasferred/ Time taken for tests,这个统计很好的说明服务器的处理能力达到极限时,其出口宽带的需求量。Connection Times (ms)              min  mean[+/-sd] median   maxConnect:        0    2   0.8      1       6Processing:     3    5   7.9      5     399Waiting:        0    4   7.9      4     399Total:          4    7   8.0      7     404WARNING: The median and mean for the initial connection time are not within a normal deviation        These results are probably not that reliable.Percentage of the requests served within a certain time (ms)  50%      7               #50%用户请求在7ms内返回  66%      7               #60%用户请求在7ms内返回  75%      7  80%      7  90%      7  95%      7  98%     12              #98%用户请求在12ms内返回  99%     12 100%    404 (longest request)  

    使用注意

    1、MAC中应该是自带了Apache。

    2、在使用ab命令时,并发了过高会出现错误:Too many open files,由于系统打开文件数量限制了。

    查看系统打开文件数量,命令:ulimit -a

    修改打开文件数量,修改成1024,命令:ulimit -n 1024

    查看修改后情况,命令:ulimit -n

    上面就是小居数码小编今天给大家介绍的关于(ab压测工具)的全部内容,希望可以帮助到你,想了解更多关于数码知识的问题,欢迎关注我们,并收藏,转发,分享。

    94%的朋友还想知道的:

    (315)个朋友认为回复得到帮助。

    部分文章信息来源于以及网友投稿,转载请说明出处。

    本文标题:AB压力测试(ab压测工具):http://sjzlt.cn/shuma/153551.html

    猜你喜欢