ab压力测试

一、ab安装与介绍

Apache Benchmark(简称ab) 是Apache安装包中自带的压力测试工具 ,简单易用


1.ab(apache benchmark)安装

[root@ppzy ~] yum -y install httpd-tools


2.ab或 ab -help


[root@ppzy ~]# ab

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 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

                    (SSL2, SSL3, TLS1, TLS1.1, TLS1.2 or ALL)

[root@ppzy ~]#



ab [可选的参数选项] 需要进行压力测试的url

参数说明:


-n即requests,用于指定压力测试总共的执行次数。

-c即concurrency,用于指定的并发数。

-t即timelimit,等待响应的最大时间(单位:秒)。

-b即windowsize,TCP发送/接收的缓冲大小(单位:字节)。

-p即postfile,发送POST请求时需要上传的文件,此外还必须设置-T参数。

-u即putfile,发送PUT请求时需要上传的文件,此外还必须设置-T参数。

-T即content-type,用于设置Content-Type请求头信息,例如:application/x-www-form-urlencoded,默认值为text/plain。

-v即verbosity,指定打印帮助信息的冗余级别。

-w以HTML表格形式打印结果。

-i使用HEAD请求代替GET请求。

-x插入字符串作为table标签的属性。

-y插入字符串作为tr标签的属性。

-z插入字符串作为td标签的属性。

-C添加cookie信息,例如:"Apache=1234"(可以重复该参数选项以添加多个)。

-H添加任意的请求头,例如:"Accept-Encoding: gzip",请求头将会添加在现有的多个请求头之后(可以重复该参数选项以添加多个)。

-A添加一个基本的网络认证信息,用户名和密码之间用英文冒号隔开。

-P添加一个基本的代理认证信息,用户名和密码之间用英文冒号隔开。

-X指定使用的和端口号,例如:"126.10.10.3:88"。

-V打印版本号并退出。

-k使用HTTP的KeepAlive特性。

-d不显示百分比。

-S不显示预估和警告信息。

-g输出结果信息到gnuplot格式的文件中。

-e输出结果信息到CSV格式的文件中。

-r指定接收到错误信息时不退出程序。

-h显示用法信息,其实就是ab -help。




二、ab实战使用

1.ab的使用


模拟并发请求1000次,总共请求10000次


[root@ppzy ~]# ab -c 1000 -n 10000 http://172.16.18.84:8080/v1/echo/service/hello

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 172.16.18.84 (be patient)

Completed 1000 requests

Completed 2000 requests

Completed 3000 requests

Completed 4000 requests

Completed 5000 requests

Completed 6000 requests

Completed 7000 requests

Completed 8000 requests

Completed 9000 requests

Completed 10000 requests

Finished 10000 requests



Server Software:         #服务器软件名称及版本信息

Server Hostname:        172.16.18.84 #服务器主机名

Server Port:            8080 #服务器端口


Document Path:          /v1/echo/service/hello #供测试的URL路径

Document Length:        105 bytes #供测试的URL返回的文档大小


Concurrency Level:      1000 #并发数

Time taken for tests:   8.143 seconds #压力测试消耗的总时间

Complete requests:      10000 #的总次数

Failed requests:        0 #失败的请求数

Write errors:           0 #网络连接写入错误数

Total transferred:      2660000 bytes #传输的总数据量

HTML transferred:       1050000 bytes #HTML文档的总数据量

Requests per second:    1227.98 [#/sec] (mean) #平均每秒的请求数) 这个是非常重要的参数数值,服务器的吞吐量

Time per request:       814.344 [ms] (mean) #所有并发用户(这里是1000)都请求一次的平均时间

Time per request:       0.814 [ms] (mean, across all concurrent requests) #单个用户请求一次的平均时间

Transfer rate:          318.99 [Kbytes/sec] received #每秒获取的数据长度 (传输速率,单位:KB/s)


Connection Times (ms)

              min  mean[+/-sd] median   max

Connect:       28  249 516.5     28    3055

Processing:    28  295 407.3    125    4589

Waiting:       28  279 406.7    117    4589

Total:         56  544 610.6    274    4861


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

  50%    274 #50%的请求在274ms内返回

  66%    593 #60%的请求在593ms内返回

  75%   1057

  80%   1062

  90%   1313

  95%   1611

  98%   2357

  99%   3070

 100%   4861 (longest request)

[root@ppzy ~]#




2.模拟post请求


在当前目录下创建一个文件post.txt


编辑文件post.txt写入


cid=4&status=1


相当于post传递cid,status参数


ab -c 10 -n 100  -p 'post.txt' -T 'application/x-www-form-urlencoded' 'http://test.api.com/ttk/auth/info/'

你可能感兴趣的:(ab压力测试)