简单模拟一下ab压力测试

在学习ab工具之前,我们需了解几个关于压力测试的概念

吞吐率(Requests per second)
概念:服务器并发处理能力的量化描述,单位是reqs/s,指的是某个并发用户数下单位时间内处理的请求数。某个并发用户数下单位时间内能处理的最大请求数,称之为最大吞吐率。
计算公式:总请求数 / 处理完成这些请求数所花费的时间,即
Request per second = Complete requests / Time taken for tests

并发连接数(The number of concurrent connections)
概念:某个时刻服务器所接受的请求数目,简单的讲,就是一个会话。

并发用户数(The number of concurrent users,Concurrency Level)
概念:要注意区分这个概念和并发连接数之间的区别,一个用户可能同时会产生多个会话,也即连接数。

用户平均请求等待时间(Time per request)
计算公式:处理完成所有请求数所花费的时间/ (总请求数 / 并发用户数),即
Time per request = Time taken for tests /( Complete requests / Concurrency Level)

服务器平均请求等待时间(Time per request: across all concurrent requests)
计算公式:处理完成所有请求数所花费的时间 / 总请求数,即
Time taken for / testsComplete requests
可以看到,它是吞吐率的倒数。
同时,它也=用户平均请求等待时间/并发用户数,即
Time per request / Concurrency Level

 

 

简单了解下ab

ab全程是apache benchmark,是apache官方推出的一个工具,创建多个并发访问线程,模拟多个访问者同时对一个URL地址进行访问。它的测试目标是基于URL的,因此它既可以来测试apache的负载压力,也可以测试Nginx等服务器的压力。

ab压力测试需要注意哪些?

 

测试的注意事项:
测试机器与被测试机器要分开,别在同一台机器上测试,否则结果是不准确的
不要对线上的服务器做测试,否则挂了就不好了哦。
观察测试工具ab所在的机器,以及被测试的前端机器的CPU,内存,网络等都不超过最高限度的75%。

下面测试下,两台服务器。

192.168.136.135  作为被测试的WEB服务器

192.168.136.148  作为ab测试的服务器

首先确保ab测试服务器安装ab,使用以下命令yum安装

yum install -y ab  //安装

安装完成之后,执行ab命令,返回一些帮助信息,确保安装成功

访问以下被测试的服务器,确保正常

执行下面的命令进行测试

ab -c 100 -n 5000 http://192.168.136.135/phpinfo.php      //模拟100个并发数,总共访问5000次

返回的结果

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 192.168.136.135 (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requests


Server Software:        nginx     //被测试的服务器是Nginx
Server Hostname:        192.168.136.135   //Hostname
Server Port:            80                          //端口

Document Path:          /phpinfo.php        //path
Document Length:        96660 bytes     //文档大小

Concurrency Level:      100                   //100个并发
Time taken for tests:   11.860 seconds   //时间花费
Complete requests:      5000                //请求了5000次
Failed requests:        486                   //失败了486次
   (Connect: 0, Receive: 0, Length: 486, Exceptions: 0)
Write errors:           0
Total transferred:      484274801 bytes  //整个场景中的网络传输量
HTML transferred:       483509342 bytes  ///整个场景中的HTML内容传输量
Requests per second:    421.60 [#/sec] (mean)   //QPS值,不高   吞吐率,大家最关心的指标之一,相当于 LR 中的每秒事务数,后面括号中的 mean 表示这是一个平均值
Time per request:       237.194 [ms] (mean)  ///用户平均请求等待时间,大家最关心的指标之二,相当于 LR 中的平均事务响应时间,后面括号中的 mean 表示这是一个平均值
Time per request:       2.372 [ms] (mean, across all concurrent requests)   //服务器平均请求处理时间,大家最关心的指标之三
Transfer rate:          39876.53 [Kbytes/sec] received  //平均每秒网络上的流量,可以帮助排除是否存在网络流量过大导致响应时间延长的问题

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   1.3      1      13
Processing:    45  234  44.2    231     540
Waiting:       23  187  40.7    182     442
Total:         47  235  44.0    232     540

Percentage of the requests served within a certain time (ms)
  50%    232     //这是每个请求处理的时间分布情况,平均页面的响应时间是232毫秒以内,66%的在238毫秒以内,主要看90%的处理时间
  66%    238
  75%    243
  80%    247
  90%    280
  95%    302
  98%    382
  99%    442
 100%    540 (longest request)  //最长是540毫秒
    

把并发数改为50测试一下

ab -c 50 -n 5000 http://192.168.136.135/phpinfo.php
.....
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   1.3      1      18
Processing:    52  104  13.2    104     158
Waiting:       36   79   8.1     79     135
Total:         65  105  13.3    104     170

Percentage of the requests served within a certain time (ms)
  50%    104   
  66%    108
  75%    111
  80%    114
  90%    126
  95%    131
  98%    135
  99%    139
 100%    170 (longest request)
[root@localhost ~]# ab -c 50 -n 5000 http://192.168.136.135/phpinfo.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 192.168.136.135 (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requests


Server Software:        nginx
Server Hostname:        192.168.136.135
Server Port:            80

Document Path:          /phpinfo.php
Document Length:        96660 bytes

Concurrency Level:      50
Time taken for tests:   10.987 seconds
Complete requests:      5000
Failed requests:        478
   (Connect: 0, Receive: 0, Length: 478, Exceptions: 0)
Write errors:           0
Total transferred:      484064467 bytes
HTML transferred:       483299467 bytes
Requests per second:    455.07 [#/sec] (mean)  //QPS
Time per request:       109.874 [ms] (mean)
Time per request:       2.197 [ms] (mean, across all concurrent requests)
Transfer rate:          43023.80 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   0.9      1      14
Processing:    32  109  14.4    108     192
Waiting:        9   82  10.1     83     158
Total:         32  109  14.3    109     193

Percentage of the requests served within a certain time (ms)
  50%    109
  66%    112
  75%    115
  80%    118
  90%    129
  95%    136
  98%    144
  99%    152
 100%    193 (longest request)  //最长193

测试结果其实并不实际很准确,因为测试机和被测试机都在一台机器上。这里值是了解。

然后再来一个静态的html测试下

ab -c 100 -n 5000 http://192.168.136.135/ab.html

很快完成测试

Server Software:        nginx
Server Hostname:        192.168.136.135
Server Port:            80

Document Path:          /ab.html
Document Length:        23 bytes

Concurrency Level:      100
Time taken for tests:   0.830 seconds
Complete requests:      5000
Failed requests:        0  //0次失败
Write errors:           0
Total transferred:      1241916 bytes
HTML transferred:       115644 bytes
Requests per second:    6024.71 [#/sec] (mean)  //QPS达到了6000+
Time per request:       16.598 [ms] (mean)
Time per request:       0.166 [ms] (mean, across all concurrent requests)
Transfer rate:          1461.36 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1    7   1.4      7      23
Processing:     5    9   1.6      9      24
Waiting:        0    7   1.5      7      11
Total:         10   16   1.8     16      29

Percentage of the requests served within a certain time (ms)
  50%     16  //响应也很快
  66%     17
  75%     17
  80%     17
  90%     18
  95%     19
  98%     20
  99%     23
 100%     29 (longest request)

所以静态和动态的对比,不但要考虑到服务器的性能,还要考虑到程序的优化。

 

 

 

关于登录的问题
有时候进行压力测试需要用户登录,怎么办?
请参考以下步骤:

先用账户和密码登录后,用开发者工具找到标识这个会话的Cookie值(Session ID)记下来

如果只用到一个Cookie,那么只需键入命令:
ab -n 100 -C key=value http://test.com/

如果需要多个Cookie,就直接设Header:
ab -n 100 -H “Cookie: Key1=Value1; Key2=Value2” http://test.com/

 

posted @ 2017-05-17 17:01  温柔的风  阅读(1159)  评论(0编辑  收藏  举报