1.ab的简介

    ab是apachebench命令的缩写。

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


2.ab的原理

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

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


3.ab的安装

centos上运行命令 : yum -y install httpd-tools

ubuntu上运行命令:  apt-getinstall apache2-utils

测试安装是否成功:   ab -V


4.ab的参数说明






































[root@vic html]# ab --help

ab: wrong number of arguments

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

Options are:

    -n requests     Number of requests to perform

    -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一次产生的请求个数。默认是一次一个。

-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设置<table>属性的字符串。

-X对请求使用代理服务器。

-y设置<tr>属性的字符串。

-z设置<td>属性的字符串。

-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中。其中的第一行为标题。


-i执行HEAD请求,而不是GET。

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


-q如果处理的请求数大于150,ab每处理大约10%或者100个请求时,会在stderr输出一个进度计数。此-q

标记可以抑制这些信息。


5.性能指标

5.1吞吐量(Requests per second)









服务器并发处理能力的量化描述,单位是reqs/s,指的是在某个并发用户数下单位时间内处理的请求数。

某个并发用户数下单位时间内能处理的最大请求数,称之为最大吞吐率。

记住:吞吐率是基于并发用户数的。这句话代表了两个含义:

a、吞吐率和并发用户数相关

b、不同的并发用户数下,吞吐率一般是不同的

计算公式:总请求数/处理完成这些请求数所花费的时间,即

Request per second=Complete requests/Time takenfor tests

必须要说明的是,这个数值表示当前机器的整体性能,值越大越好。


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

  并发连接数指的是某个时刻服务器所接受的请求数目,简单的讲,就是一个会话。


5.3 并发用户数(Concurrency Level)




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

在HTTP/1.1下,IE7支持两个并发连接,IE8支持6个并发连接,FireFox3支持4个并发连接,所以相应的,

我们的并发用户数就得除以这个基数。


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



计算公式:处理完成所有请求数所花费的时间/(总请求数/并发用户数),即:

Time per request=Time takenfortests/(Complete requests/Concurrency Level)


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






计算公式:处理完成所有请求数所花费的时间/总请求数,即:

Time taken for/testsComplete requests

可以看到,它是吞吐率的倒数。

同时,它也等于用户平均请求等待时间/并发用户数,即

Time per request/Concurrency Level


6.ab的应用





ab的命令参数比较多,我们经常使用的是-c和-n参数。

    ab -c 10 -n 100 http://test.api.com

  -c10表示并发用户数为10

  -n100表示请求总数为100

测试结果:





















































test-185:~# ab -c 10 -n 100 http://mokerapi.test/api/gettoken

This is ApacheBench, Version 2.3 <$Revision: 1430300 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking mokerapi.test (be patient).....done

Server Software:        nginx/1.7.9

Server Hostname:        mokerapi.test

Server Port:            80

Document Path:          /api/gettoken

Document Length:        208 bytes

Concurrency Level:      10

Time taken for tests:   0.109 seconds

Complete requests:      100

Failed requests:        0

Write errors:           0

Total transferred:      43400 bytes

HTML transferred:       20800 bytes


Requests per second:    914.00 [#/sec] (mean)  #吞吐率,计算公式:

Complete requests/Time taken for tests  总请求数/处理完成这些请求数所花费的时间


Time per request:       10.941 [ms] (mean)  # 用户平均请求等待时间,计算公式:

Time token for tests/(Complete requests/Concurrency Level)。处理完成所有请求数所花费

的时间/(总请求数/并发用户数)


Time per request:       1.094 [ms] (mean, across all concurrent requests)   #服务器平均请求等

待时间,计算公式:Time taken for tests/Complete requests,正好是吞吐率的倒数。也可以这么

统计:Time per request/Concurrency Level


Transfer rate:          387.38 [Kbytes/sec] received  #表示这些请求在单位时间内从服务器获取的数

据长度,计算公式:Total trnasferred/ Time taken for tests,这个统计很好的说明服务器的处理能

力达到极限时,其出口宽带的需求量。


Connection Times (ms)

              min  mean[+/-sd] median   max

Connect:        0    1   1.6      0       7

Processing:     4    8   2.7      8      18

Waiting:        4    8   2.7      8      18

Total:          4   10   2.8      9      19

Percentage of the requests served within a certain time (ms)

  50%      9

  66%     10

  75%     12

  80%     12

  90%     13

  95%     16

  98%     19

  99%     19

100%     19 (longest request)

#Percentage of requests served within a certain time(ms)这部分数据用于描述每个请求处理时间

的分布情况,比如以上测试,80%的请求处理时间都不超过7ms,这个处理时间是指前面的

Time per request,即对于单个用户而言,平均每个请求的处理时间


转自:https://www.jianshu.com/p/db3fd0abfd25