使用Apacheab工具对比NginxApache静态页面处理能力

前言:为什么要进行压力测试

        在运维工作中,压力测试是一项非常重要的工作。比如在一个网站上线之前,能承受多大访问量、在大访问量情况下性能怎样,这些数据指标好坏将会直接影响用户体验。但是,在压力测试中存在一个共性,那就是压力测试的结果与实际负载结果不会完全相同,就算压力测试工作做的再好,也不能保证100%和线上性能指标相同。面对这些问题,我们只能尽量去想方设法去模拟。所以,压力测试非常有必要,有了这些数据,我们就能对自己做维护的平台做到心中有数。

目的:

        这篇文章主要是承接我上一篇博文地球最强WEB前端Nginx服务器之搭建基础静态WEB站点,通过Apache自带压力测试工具ab简单的证明一下说Nginx是地球最强的WEB前端绝对不是吹牛逼的。

1. 压力测试工具ab简介:

            abApache自带的压力测试工具,使用简单,虽然不是业界最专业的压力测试工具,但测试结果也是有一定参考价值的。

1.1业界常见的压力测试工具

目前较为常见的网站压力测试工具有webbenchabapache bench)、tcpcopyloadrunner

1webbench

Lionbridge公司开发,主要测试每秒钟请求数和每秒钟数据传输量,同时支持静态、动态、SSL,部署简单,静动态均可测试。适用于小型网站压力测试(单例最多可模拟3万并发)

2abapache bench):

Apache自带的压力测试工具,主要功能用于测试网站每秒钟处理请求个数多见用于静态压力测试,功能较弱,非专业压力测试工具。这也是本文使用的工具。

3tcpcopy:基于底层应用请求复制,可转发各种在线请求到测试服务器,具有分布式压力测试功能,所测试数据与实际生产数据较为接近后起之秀,主要用于中大型压力测试,所有基于tcppackets均可测试

4loadrunner

压力测试界的泰斗,可以创建虚拟用户,可以模拟用户真实访问流程从而录制成脚本,其测试结果也最为逼真模拟最为逼真,并可进行独立的单元测试,但是部署配置较为复杂,需要专业人员才可以。

1.2基础环境准备

由于本文是基于我上一篇博文地球最强WEB前端Nginx服务器之搭建基础静态WEB站点而写的,所以使用的测试环境也使用写上一篇博文时使用的软硬件环境,这也是为了公平起见嘛!^_^


本文例子使用的系统和软件环境:

操作系统:CentOS 6.5 64位版

安装软件:nginx-1.8.0httpd.x86_642.2.15-29.el6.centos

IP地址:192.168.0.12/24


1步:安装Nginx

这步请参考我上一篇博文地球最强WEB前端Nginx服务器之搭建基础静态WEB站点,这里省略了。


2步:安装Apache

使用官方自带的包就可以了。

[[email protected] ~]# yum install -y httpd

Loaded plugins: fastestmirror, security

Loading mirror speeds from cached hostfile

Setting up Install Process

Resolving Dependencies

--> Running transaction check

---> Package httpd.x86_64 0:2.2.15-29.el6.centos will beinstalled

--> Processing Dependency: apr-util-ldap for package:httpd-2.2.15-29.el6.centos.x86_64

--> Running transaction check

---> Package apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1 will beinstalled

--> Finished Dependency Resolution

 

Dependencies Resolved

 

============================================================================================================================

 Package                       Arch                   Version                                Repository                Size

============================================================================================================================

Installing:

 httpd                         x86_64                 2.2.15-29.el6.centos                   LocalDVD                 821 k

Installing for dependencies:

 apr-util-ldap                 x86_64                 1.3.9-3.el6_0.1                        LocalDVD                  15 k

 

Transaction Summary

============================================================================================================================

Install       2 Package(s)

 

Total download size: 837 k

Installed size: 2.9 M

Downloading Packages:

----------------------------------------------------------------------------------------------------------------------------

Total                                                                                      106 MB/s | 837 kB     00:00    

Running rpm_check_debug

Running Transaction Test

Transaction Test Succeeded

Running Transaction

  Installing :apr-util-ldap-1.3.9-3.el6_0.1.x86_64                                                                    1/2

  Installing :httpd-2.2.15-29.el6.centos.x86_64                                                                       2/2

  Verifying  : httpd-2.2.15-29.el6.centos.x86_64                                                                       1/2

  Verifying  : apr-util-ldap-1.3.9-3.el6_0.1.x86_64                                                                    2/2

 

Installed:

  httpd.x86_640:2.2.15-29.el6.centos                                                                                      

 

Dependency Installed:

  apr-util-ldap.x86_640:1.3.9-3.el6_0.1                                                                                   

 

Complete!

提示:安装信息其实没啥用,大部分情况不会有问题,凑字数!!!呵呵!


2. 开始测试

先来简单了解一下ab压力测试工具的常用参数

[[email protected] ~]# ab --help

ab: wrong number of arguments

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

Options are:

    -n requests     Number of requeststo perform

   -c concurrency  Number of multiplerequests 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. Rememberalso to set -T

    -u putfile      File containing data to PUT. Rememberalso to set -T

    -T content-typeContent-type header for POSTing, eg.

                   'application/x-www-form-urlencoded'

                    Defaultis '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 normalheader lines. (repeatable)

    -A attribute    Add Basic WWW Authentication, theattributes

                    are acolon separated username and password.

    -P attribute    Add Basic Proxy Authentication, the attributes

                    are acolon 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 servedtable.

    -S              Do not show confidence estimatorsand warnings.

    -g filename     Output collected data to gnuplot formatfile.

    -e filename     Output CSV file with percentages served

    -r              Don't exit on socket receiveerrors.

    -h              Display usage information (thismessage)

    -Z ciphersuite  Specify SSL/TLS cipher suite (See opensslciphers)

-f protocol     Specify SSL/TLS protocol (SSL2, SSL3,TLS1, or ALL)

本文的重点是前面两个参数:

-c:设置一次产生的请求数,可以理解为并发用户数,默认是一次一个。

-n:设置在测试会话中所执行的请求个数,默认仅执行一个请求。


2.1 测试Nginx静态页面处理能力

启动Nginx服务器查看监听状态:

[[email protected] ~]# /opt/nginx/sbin/nginx

nginx: [warn] 15000 worker_connections exceed open file resourcelimit: 1024

[[email protected] ~]# netstat -tunpl|grep nginx

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      20557/nginx

[[email protected] conf]# ab -c 1000 -n 10000 http://192.168.0.12/index.html

使用Apache的ab工具对比Nginx与Apache静态页面处理能力


[[email protected] conf]# ab -c 1000 -n 50000 http://192.168.0.12/index.html

使用Apache的ab工具对比Nginx与Apache静态页面处理能力


2.2 测试Apache静态页面处理能力

停止Nginx并发启动Apache,为保证测试结果可靠性,把nginx的默认站点主页也拷贝到Apache站点目录下。

[[email protected] ~]# pkill nginx

[[email protected] ~]# cp/opt/nginx/html/index.html /var/www/html/

[[email protected] ~]# service httpd start

Starting httpd: httpd: apr_sockaddr_info_get() failed forCentOS6lsrv02

httpd: Could not reliably determine the server's fully qualifieddomain name, using 127.0.0.1 for ServerName         [  OK  ]

[[email protected] ~]# netstat -tunpl|grep httpd

tcp        0      0 :::80                       :::*                        LISTEN     20647/httpd 


使用ab开启1000并发和请求10000次进行测试:

[[email protected] ~]# ab -c 1000 -n 10000 htpt://192.168.0.12/index.html

使用Apache的ab工具对比Nginx与Apache静态页面处理能力


[[email protected] ~]# ab -c 1000 -n 50000 htpt://192.168.0.12/index.html

使用Apache的ab工具对比Nginx与Apache静态页面处理能力


小结:

1. 以上过程虽然算不上专业的压力测试,但也在一定程度上可以看出Nginx作为前端WEB应用,静态页面处理能力确实比Apache强很多!!!!

2. Nginx处理静态页面处理能力非常出众外,还可以代理多种应用HTTPFTPPOP3/IMAPTOMCAT等),真的非常强大。

 

后期将会陆续发布关于Nginx的其它应用,希望各位感兴趣的运维同行一起交流,共同进步。