老鬼的博客 来都来啦,那就随便看看吧~
apache ab压测工具
发布于: 2019-11-11 更新于: 2026-03-13 分类于:  阅读次数: 

一:介绍

1
ab是apachebench命令的缩写,是apache自带的一个压力测试工具。

二:下载安装

  • ab windows下载地址

  • 下载图

    20191111150059.png

  • 配置path

    1
    2
    3
    将下载好的文件放在一个固定目录下,然后将根目录下的bin配置到path中,我的目录是:
    D:\my-tools\httpd-2.4.41-o111c-x86-vc15-r2\bin,配置好在cmd执行ab -help,ab -V
    查看版本号。

三:使用

1.压力测试的概念

  • 吞吐率(Requests per second)
1
2
3
概念:服务器并发处理能力的量化描述,单位是reqs/s,指的是某个并发用户数下单位时间内处理的请求数。某个并发用户数下单位时间内能处理的最大请求数,称之为最大吞吐率。
计算公式:总请求数 / 处理完成这些请求数所花费的时间,即
Request per second = Complete requests / Time taken for tests
  • 并发连接数(The number of concurrent connections)
1
概念:某个时刻服务器所接受的请求数目,简单的讲,就是一个会话。
  • 并发用户数(The number of concurrent users,Concurrency Level)
1
概念:要注意区分这个概念和并发连接数之间的区别,一个用户可能同时会产生多个会话,也即连接数。
  • 用户平均请求等待时间(Time per request)
1
2
计算公式:处理完成所有请求数所花费的时间/ (总请求数 / 并发用户数),即
Time per request = Time taken for tests /( Complete requests / Concurrency Level)
  • 服务器平均请求等待时间(Time per request: across all concurrent requests)
1
2
3
4
5
计算公式:处理完成所有请求数所花费的时间 / 总请求数,即
Time taken for / testsComplete requests
可以看到,它是吞吐率的倒数。
同时,它也=用户平均请求等待时间/并发用户数,即
Time per request / Concurrency Level

2.简介

1
ab [options] [http://]hostname[:port]/path

3.选项(options)

  • -n
1
2
3
4
5
-n即requests,用于指定压力测试总共的执行次数,默认是1,最大是50000
1.默认处理一个请求
abs "https://my.matchcess.com/pwsleep/user/index?userId=000057f1205c43c6a4b375377b16e6ee&inviteId=e150e043c0034e888c30a5f4d62c81b0"
2.处理1000个请求
abs -n 1000 "https://my.matchcess.com/pwsleep/user/index?userId=000057f1205c43c6a4b375377b16e6ee&inviteId=e150e043c0034e888c30a5f4d62c81b0"
  • -c
1
2
3
4
5
-c即concurrency,用于指定压力测试的并发数,默认是1,请注意每次请求的个数不能大于测试会话中所执行的请求个数,即:-c不能大于-n
1.下面这个请求并发用户数50,执行次数10次会报错,因为-c不能大于-n
abs -n 10 -c 50 "https://my.matchcess.com/pwsleep/user/index?userId=000057f1205c43c6a4b375377b16e6ee&inviteId=e150e043c0034e888c30a5f4d62c81b0"
2.并发用户数10,执行次数10次
abs -n 10 -c 10 "https://my.matchcess.com/pwsleep/user/index?userId=000057f1205c43c6a4b375377b16e6ee&inviteId=e150e043c0034e888c30a5f4d62c81b0"
  • -t
1
2
3
即timelimit,等待响应的最大时间(单位:秒)。它可以使对服务器的测试限制在一个固定的总时间以内。默认时,没有时间限制。
比如:abs -n 1000 -c 100 -t 5 "https://my.matchcess.com/pwsleep/user/index?userId=000057f1205c43c6a4b375377b16e6ee&inviteId=e150e043c0034e888c30a5f4d62c81b0"
这个的意思是并发用户数100,执行次数1000次,压测总的时间在5s内返回,其实5s内执行不了那么多次,我大致测试了一下执行了200多条记录,-t这个选项一般用不到。
  • -b
1
即windowsize,TCP发送/接收的缓冲大小(单位:字节)。
  • -T
1
2
3
即content-type,用于设置Content-Type请求头信息,例如:application/x-www-form-urlencoded,默认值为text/plain。
下面指定的Content-Type是application/json
abs -T "application/json" "https://my.matchcess.com/pwsleep/common/face-sleep"
  • -p
1
2
3
即postfile,发送POST请求时需要上传的文件,此外还必须设置-T参数。
下面指定的Content-Type是application/json,传递的数据在桌面的data.txt,-p是路径,可以是相对和绝对路径。
abs -T "application/json" -p "C:\Users\Administrator\Desktop\data.txt" "https://my.matchcess.com/pwsleep/common/face-sleep"
  • -v
1
2
3
4
5
设置详细级别-4及以上打印标题信息,3及以上打印响应代码(404、200等),2及以上打印警告和信息。默认是1
abs -v 1 "https://my.matchcess.com/pwsleep/user/index?userId=000057f1205c43c6a4b375377b16e6ee&inviteId=e150e043c0034e888c30a5f4d62c81b0"
abs -v 2 "https://my.matchcess.com/pwsleep/user/index?userId=000057f1205c43c6a4b375377b16e6ee&inviteId=e150e043c0034e888c30a5f4d62c81b0"
abs -v 3 "https://my.matchcess.com/pwsleep/user/index?userId=000057f1205c43c6a4b375377b16e6ee&inviteId=e150e043c0034e888c30a5f4d62c81b0"
abs -v 4 "https://my.matchcess.com/pwsleep/user/index?userId=000057f1205c43c6a4b375377b16e6ee&inviteId=e150e043c0034e888c30a5f4d62c81b0"
  • -w
1
2
以HTML表格形式打印结果。结果是table的html标签,可以复制出来写到html文件中。
abs -w "https://my.matchcess.com/pwsleep/user/index?userId=000057f1205c43c6a4b375377b16e6ee&inviteId=e150e043c0034e888c30a5f4d62c81b0"
  • -i
1
2
3
4
5
-i使用HEAD请求代替GET请求
1.HEAD请求
abs -i "https://my.matchcess.com/pwsleep/user/index?userId=000057f1205c43c6a4b375377b16e6ee&inviteId=e150e043c0034e888c30a5f4d62c81b0"
2.GET请求
abs "https://my.matchcess.com/pwsleep/user/index?userId=000057f1205c43c6a4b375377b16e6ee&inviteId=e150e043c0034e888c30a5f4d62c81b0"
  • -x
1
2
3
插入字符串作为table标签的属性。
下面-x会在输出结果的table里面加上 style='color:red';
abs -x "style='color:red;'" "https://my.matchcess.com/pwsleep/user/index?userId=000057f1205c43c6a4b375377b16e6ee&inviteId=e150e043c0034e888c30a5f4d62c81b0"
  • -y
1
2
插入字符串作为tr标签的属性
abs -x "style='color:red;'" -y "aaa" "https://my.matchcess.com/pwsleep/user/index?userId=000057f1205c43c6a4b375377b16e6ee&inviteId=e150e043c0034e888c30a5f4d62c81b0"
  • -z
1
2
插入字符串作为td标签的属性
abs -x "style='color:red;'" -y "aaa" -z "bbb" "https://my.matchcess.com/pwsleep/user/index?userId=000057f1205c43c6a4b375377b16e6ee&inviteId=e150e043c0034e888c30a5f4d62c81b0"
  • -C
1
添加cookie信息,例如:"Apache=1234"(可以重复该参数选项以添加多个)。
  • -H
1
2
添加任意的请求头,例如:"Accept-Encoding: gzip",请求头将会添加在现有的多个请求头之后(可以重复该参数选项以添加多个)。
abs -H "Accept-Encoding:gzip" "https://my.matchcess.com/pwsleep/user/index?userId=000057f1205c43c6a4b375377b16e6ee&inviteId=e150e043c0034e888c30a5f4d62c81b0"
  • -A
1
添加一个基本的网络认证信息,用户名和密码之间用英文冒号隔开。
  • -P
1
添加一个基本的代理认证信息,用户名和密码之间用英文冒号隔开。
  • -X
1
指定使用的代理服务器和端口号,例如:"126.10.10.3:88"。
  • -V
1
打印版本号并退出。
  • -k
1
使用HTTP的KeepAlive特性。
  • -d
1
2
3
4
5
不显示百分比。
1.显示百分比
abs -n 1000 -c 100 "https://my.matchcess.com/pwsleep/user/index?userId=000057f1205c43c6a4b375377b16e6ee&inviteId=e150e043c0034e888c30a5f4d62c81b0"
2.不显示百分比
abs -n 1000 -c 100 -d "https://my.matchcess.com/pwsleep/user/index?userId=000057f1205c43c6a4b375377b16e6ee&inviteId=e150e043c0034e888c30a5f4d62c81b0"
  • -S
1
不显示预估和警告信息。
  • -g
1
输出结果信息到gnuplot格式的文件中。
  • -e
1
输出结果信息到CSV格式的文件中。
  • -r
1
指定接收到错误信息时不退出程序。

4.压测结果分析

  • 测试结果的文本和分析
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
C:\Users\Administrator\Desktop>abs -n 1000 -c 100  "https://my.matchcess.com/pwsleep/user/index?userId=000057f1205c43c6a4b375377b16e6ee&inviteId=e150e043c0034e888c30a5f4d62c81b0"
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking my.matchcess.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software: nginx/1.16.1 (服务器软件名称及版本信息)
Server Hostname: my.matchcess.com(服务器主机名)
Server Port: 443(服务器端口)
SSL/TLS Protocol: TLSv1.2,ECDHE-RSA-AES128-GCM-SHA256,2048,128(SSL协议版本)
Server Temp Key: X25519 253 bits(SSL的配置的key)
TLS Server Name: my.matchcess.com(TLS的服务器主机名)

Document Path: /pwsleep/user/index?userId=000057f1205c43c6a4b375377b16e6ee&inviteId=e150e043c0034e888c30a5f4d62c81b0(供测试的URL路径)
Document Length: 2301 bytes(供测试的URL返回的文档大小)

Concurrency Level: 100(并发用户数)
Time taken for tests: 17.509 seconds(压力测试消耗的总时间)
Complete requests: 1000(压力测试的的总次数)
Failed requests: 0(失败的请求数)
Total transferred: 2612929 bytes(传输的总数据量)
HTML transferred: 2301000 bytes (HTML文档的总数据量)
Requests per second: 57.11 [#/sec] (mean)(平均每秒的请求数)
Time per request: 1750.900 [ms] (mean)(所有并发用户(这里是100)都请求一次的平均时间)
Time per request: 17.509 [ms] (mean, across all concurrent requests)(单个用户请求一次的平均时间)
Transfer rate: 145.74 [Kbytes/sec] received(传输速率,单位:KB/s)

Connection Times (ms)
min mean[+/-sd] median max
Connect: 22 1578 284.8 1644 1818
Processing: 50 82 14.1 84 208
Waiting: 45 82 14.1 83 208
Total: 97 1660 283.6 1727 1892

Percentage of the requests served within a certain time (ms)
50% 1727
66% 1739
75% 1750
80% 1756
90% 1783
95% 1811
98% 1836
99% 1853
100% 1892 (longest request)

C:\Users\Administrator\Desktop>

四:备注

1
2
如果使用ab访问https报错误,错误内容如下:SSL not compiled in; no https support
请使用abs指令代替ab指令。
*************感谢您的阅读*************