ab 是 apachebench 命令的缩写,apache 自带的压力测试工具。ab 非常实用,它不仅可以对 apache 服务器进行网站访问压力测试,也可以对其它类型的服务器进行压力测试。
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
测试安装是否成功:
[root@vic html]# ab -V
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
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,它可以使对服务器的测试限制在一个固定的总时间以内。默认时,没有时间限制-V:显示版本号并退出
性能指标
吞吐量(Requests per second)
服务器并发处理能力的量化描述,单位是 reqs/s,指的是在某个并发用户数下单位时间内处理的请求数。某个并发用户数下单位时间内能处理的最大请求数,称之为最大吞吐率。
记住:吞吐率是基于并发用户数的。这句话代表了两个含义:
- 吞吐率和并发用户数相关
- 不同的并发用户数下,吞吐率一般是不同的
计算公式:总请求数 / 处理完成这些请求数所花费的时间,即:
Request per second = Complete requests / Time taken for tests
必须要说明的是,这个数值表示当前机器的整体性能,值越大越好。
并发连接数(The number of concurrent connections)
并发连接数指的是某个时刻服务器所接受的请求数目,简单的讲,就是一个会话。
并发用户数(Concurrency Level)
要注意区分这个概念和并发连接数之间的区别,一个用户可能同时会产生多个会话,也即连接数。在 HTTP/1.1 下,IE7 支持两个并发连接,IE8 支持 6 个并发连接,FireFox3 支持 4 个并发连接,所以相应的,我们的并发用户数就得除以这个基数。
用户平均请求等待时间(Time per request)
计算公式:处理完成所有请求数所花费的时间 /(总请求数 / 并发用户数),即:
Time per request = Time taken for tests / (Complete requests / Concurrency Level)
服务器平均请求等待时间(Time per request: across all concurrent requests)
计算公式:处理完成所有请求数所花费的时间 / 总请求数,即:
Time per request = Time taken for tests / Complete requests
可以看到,它是吞吐率的倒数。
同时,它也等于用户平均请求等待时间 / 并发用户数,即 Time per request / Concurrency Level。
ab 的应用
ab 的命令参数比较多,我们经常使用的是 -c 和 -n 参数。
ab -c 10 -n 100 http://www.myvick.cn/index.php
同时处理 100 个请求并运行 10 次 index.php:
- -c 10 表示并发用户数为 10
- -n 100 表示请求总数为 100
执行输出:
[root@vic html]# ab -c 10 -n 100 http://www.myvick.cn/index.php
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking www.myvick.cn (be patient).....done
Server Software: nginx/1.13.6
Server Hostname: www.myvick.cn
Server Port: 80
Document Path: /index.php
Document Length: 799 bytes
Concurrency Level: 10
Time taken for tests: 0.668 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 96200 bytes
HTML transferred: 79900 bytes
Requests per second: 149.71 [#/sec] (mean)
Time per request: 66.797 [ms] (mean)
Time per request: 6.680 [ms] (mean, across all concurrent requests)
Transfer rate: 140.64 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 1 2 0.7 2 5
Processing: 2 26 81.3 3 615
Waiting: 1 26 81.3 3 615
Total: 3 28 81.3 6 618
Percentage of the requests served within a certain time (ms)
50% 6
66% 6
75% 7
80% 7
90% 10
95% 209
98% 209
99% 618
100% 618 (longest request)
输出字段解释:
| 字段 | 说明 |
|---|---|
| Server Software | 测试服务器的名字 |
| Server Hostname | 请求的 URL 主机名 |
| Server Port | web 服务器监听的端口 |
| Document Path | 请求的 URL 中的根绝对路径,通过该文件的后缀名,我们一般可以了解该请求的类型 |
| Document Length | HTTP 响应数据的正文长度 |
| Concurrency Level | 并发用户数,这是我们设置的参数之一 |
| Time taken for tests | 所有这些请求被处理完成所花费的总时间,单位秒 |
| Complete requests | 总请求数量,这是我们设置的参数之一 |
| Failed requests | 表示失败的请求数量,这里的失败是指请求在连接服务器、发送数据、接收数据时发生错误或者请求超时;如果返回的是 HTTP 错误码,则不算失败 |
| Write errors | 写入错误的数量 |
| Total transferred | 所有请求的响应数据长度总和,包括每个 HTTP 响应数据的头信息和正文数据的长度 |
| HTML transferred | 所有请求的响应数据中正文数据的总和,也就是减去了 Total transferred 中 HTTP 响应数据中的头信息的长度 |
| Requests per second | 吞吐率,计算公式:Complete requests / Time taken for tests,这个数值表示当前机器的整体性能,值越大越好 |
| Time per request (mean) | 用户平均请求等待时间,计算公式:Time taken for tests / (Complete requests / Concurrency Level) |
| Time per request (mean, across all concurrent requests) | 服务器平均请求等待时间,计算公式:Time taken for tests / Complete requests,正好是吞吐率的倒数 |
| Transfer rate | 表示这些请求在单位时间内从服务器获取的数据长度,等于 Total transferred / Time taken for tests |
| Connection Times | 网络连接、系统调用等时间的统计,分为 Connect(连接)、Processing(处理)、Waiting(等待)、Total(总计)四行 |
| Percentage of the requests served within a certain time | 这部分数据用于描述每个请求处理时间的分布情况,例如 50% 的请求处理时间在 6ms 以内,100% 的请求处理时间在 618ms 以内(最长请求) |
文章来源
本文转载自 https://www.cnblogs.com/myvic/p/7703973.html,作者:myvic
参考资料:
系列导航
- 上一篇:开发工具 018:Linux:创建自建服务
- 回到系列首页:开发工具 001:常用开发库知识体系详解