大型网站分布式架构(三)—— 使用Apache Bench进行压力测试

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_37490221/article/details/81950041

常用压力测试工具

  • 基础简易版:Apache Bench
  • 进阶常用版:Jmeter
  • 高级多功能版:Tsung

相对而言Jmeter和Tsung更加多功能,支持多协议,支持最大并发数也更大,同时还支持分布式,在预生产环境当中Tsung绝对是压力测试的绝佳选择。但本文只针对Apache Bench测试工具展开,更加轻量级和简易,使用更方便,是后台开发人员的绝佳选择。


安装Apache Bench

Apache Bench工具在Apache安装包中,安装Apache即拥有了Apache Bench压力测试工具

[root@centos6-1 ~]# yum -y install httpd

查询版本

  • Apache版本
  • Apache Bench版本
[root@centos6-1 ~]# httpd -version
Server version: Apache/2.2.15 (Unix)
Server built:   Oct 19 2017 16:43:38
[root@centos6-1 ~]# 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 即Apache Bench的简称

启动Tomcat

[root@centos6-1 ~]# cd /export/servers/apache-tomcat-8.0.53/bin/
[root@centos6-1 bin]# ./startup.sh 
Using CATALINA_BASE:   /export/servers/apache-tomcat-8.0.53
Using CATALINA_HOME:   /export/servers/apache-tomcat-8.0.53
Using CATALINA_TMPDIR: /export/servers/apache-tomcat-8.0.53/temp
Using JRE_HOME:        /export/servers/jdk1.8.0_65/jre
Using CLASSPATH:       /export/servers/apache-tomcat-8.0.53/bin/bootstrap.jar:/export/servers/apache-tomcat-8.0.53/bin/tomcat-juli.jar
Tomcat started.
[root@centos6-1 bin]# jps
91426 Bootstrap
91444 Jps
[root@centos6-1 bin]# 

浏览器访问Tomcat

访问之前构建和部署在Tomcat中的appdemo项目,构建和部署过程详见:

Tomcat启动成功

Apache Bench模拟并发

[root@centos6-1 bin]# ab -k -n 10000 -c 1000  http://192.168.214.150:8080/appdemo-0.0.1-SNAPSHOT/    
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.214.150 (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:        Apache-Coyote/1.1
Server Hostname:        192.168.214.150
Server Port:            8080

Document Path:          /appdemo-0.0.1-SNAPSHOT/
Document Length:        197 bytes

Concurrency Level:      1000
Time taken for tests:   4.676 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    0
Total transferred:      3714942 bytes
HTML transferred:       2016098 bytes
Requests per second:    2138.54 [#/sec] (mean)
Time per request:       467.610 [ms] (mean)
Time per request:       0.468 [ms] (mean, across all concurrent requests)
Transfer rate:          775.83 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  150 254.0     93    1233
Processing:    18  307 264.4    181    1434
Waiting:        4  271 250.2    149    1349
Total:         55  456 329.2    329    1434

Percentage of the requests served within a certain time (ms)
  50%    329
  66%    415
  75%    570
  80%    682
  90%    961
  95%   1309
  98%   1400
  99%   1410
 100%   1434 (longest request)
[root@centos6-1 bin]# 

查看Tomcat日志

这里写图片描述

刚好超出10000行访问记录,并且都是在5s发出的请求,Apache Bench模拟并发访问成功

Apache Bench命令参数

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

options有很多参数选项,常用的有-n和-c,可以通过ab -help获得所有参数类型和解释

[root@centos6-1 bin]# ab -help
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)
[root@centos6-1 bin]#

而刚才模拟的命令

ab -k -n 10000 -c 1000  http://192.168.214.150:8080/appdemo-0.0.1-SNAPSHOT/
表示:总共产生10000个请求,每次并发请求1000个,访问http://192.168.214.150:8080/appdemo-0.0.1-SNAPSHOT/

产生的结果是5秒钟内发出了10000个请求,同时Tomcat成功地处理了Apache Bench模拟发出的请求。

异常解决方案

1.socket: Too many open files (24)

当前并发数超过了允许打开的文件个数,默认同时打开的文件个数为1024个,可以通过以下命令查看和修改

//查看
[root@centos6-1 bin]# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 7329
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024  //这个就是默认值
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 7329
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
//修改
[root@centos6-1 bin]# ulimit -n 2048

2.apr_socket_recv: Connection reset by peer (104)

apr_socket_recv这个是操作系统内核的一个参数,在高并发的情况下,内核会认为系统受到了SYN flood攻击,会发送cookies(possible SYN flooding on port 80. Sending cookies),这样会减慢影响请求的速度,所以在应用服务武器上设置下这个参数为0禁用系统保护就可以进行大并发测试了:

net.ipv4.tcp_syncookies = 0

总结

到此我们就了解了如何通过Apache Bench模拟并发访问,也看到Tomcat在默认配置情况下每秒处理了2000个请求数。但是,这并不能代表真实的情况,在真实项目中并发量还受到很多因素的影响,例如:对象的产生导致的内存的消耗,以及与数据库的连接占用的资源,服务器的内存和CPU,单机或集群等等。所以不同的项目并发情况各有不同,这也是为什么要进行压力测试的原因。

猜你喜欢

转载自blog.csdn.net/weixin_37490221/article/details/81950041
今日推荐