在学习ab工具之前,我们需了解几个关于压力测试的概念
概念:服务器并发处理能力的量化描述,单位是reqs/s,指的是某个并发用户数下单位时间内处理的请求数。某个并发用户数下单位时间内能处理的最大请求数,称之为最大吞吐率。 计算公式:总请求数 / 处理完成这些请求数所花费的时间,即 Request per second = Complete requests / Time taken for tests
概念:某个时刻服务器所接受的请求数目,简单的讲,就是一个会话。
概念:要注意区分这个概念和并发连接数之间的区别,一个用户可能同时会产生多个会话,也即连接数。
计算公式:处理完成所有请求数所花费的时间/ (总请求数 / 并发用户数),即 Time per request = Time taken for tests /( Complete requests / Concurrency Level)
计算公式:处理完成所有请求数所花费的时间 / 总请求数,即 Time taken for / testsComplete requests 可以看到,它是吞吐率的倒数。 同时,它也=用户平均请求等待时间/并发用户数,即 Time per request / Concurrency Level
ab全称为:apache bench
ab是Apache超文本传输协议(HTTP)的性能测试工具。其设计意图是描绘当前所安装的Apache的执行性能,主要是显示你安装的Apache每秒可以处理多少个请求。
ab是apache自带的压力测试工具。ab非常实用,它不仅可以对apache服务器进行网站访问压力测试,也可以对或其它类型的服务器进行压力测试。比如nginx、tomcat、IIS等。
进入apache官网 http://httpd.apache.org/ 下载apache即可
以windows环境下,apache安装路径为C:\apache\Apache24\为例
打开终端,输入命令
cd C:\apache\Apache24\bin
即可启动ab
ab -n 100 -c 10 http://test.com/
其中-n表示请求数,-c表示并发数
上面的命令运行完毕后就出来测试报告了
Server Software: nginx
Server Hostname: wan.web3.xin
Server Port: 80
Document Path: /
Document Length: 1 bytes
Concurrency Level: 10
Time taken for tests: 0.010 seconds
Complete requests: 100
Failed requests: 0
Keep-Alive requests: 100
Total transferred: 12700 bytes
HTML transferred: 100 bytes
Requests per second: 9800.08 [#/sec] (mean)
Time per request: 1.020 [ms] (mean)
Time per request: 0.102 [ms] (mean, across all concurrent requests)
Transfer rate: 1215.44 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.1 0 0
Processing: 0 1 0.4 1 3
Waiting: 0 1 0.4 1 3
Total: 0 1 0.5 1 3
Percentage of the requests served within a certain time (ms)
50% 1
66% 1
75% 1
80% 1
90% 1
95% 2
98% 2
99% 3
100% 3 (longest request)
这段展示的是web服务器的信息,可以看到服务器采用的是nginx,域名是wan.web3.xin,端口是80
Server Software: nginx
Server Hostname: wan.web3.xin
Server Port: 80
这段是关于请求的文档的相关信息,所在位置“/”,文档的大小为1 bytes(此为http响应的正文长度)
Document Path: /
Document Length: 1 bytes
这段展示了压力测试的几个重要指标
Concurrency Level: 10
Time taken for tests: 0.010 seconds
Complete requests: 100
Failed requests: 0
Keep-Alive requests: 100
Total transferred: 12700 bytes
HTML transferred: 100 bytes
Requests per second: 9800.08 [#/sec] (mean)
Time per request: 1.020 [ms] (mean)
Time per request: 0.102 [ms] (mean, across all concurrent requests)
Transfer rate: 1215.44 [Kbytes/sec] received
Concurrency Level: 10
Time taken for tests: 0.010 seconds
Complete requests: 100
Failed requests: 0
Total transferred: 12700 bytes
HTML transferred: 100 bytes
Requests per second: 9800.08 [#/sec] (mean)
Time per request: 1.020 [ms] (mean)
Time per request: 0.102 [ms] (mean, across all concurrent requests)
Transfer rate: 1215.44 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.1 0 0
Processing: 0 1 0.4 1 3
Waiting: 0 1 0.4 1 3
Total: 0 1 0.5 1 3
Percentage of the requests served within a certain time (ms)
50% 1
66% 1
75% 1
80% 1
90% 1
95% 2
98% 2
99% 3
100% 3 (longest request)
有时候进行压力测试需要用户登录,怎么办?
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等