使用Apache的ab工具對比Nginx與Apache靜態頁面處理能力

使用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

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

[root@CentOS6lsrv02 ~]# 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壓力測試工具的常用參數

[root@CentOS6lsrv02 ~]# 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服務器查看監聽狀態:

[root@CentOS6lsrv02 ~]# /opt/nginx/sbin/nginx

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

[root@CentOS6lsrv02 ~]# netstat -tunpl|grep nginx

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

[root@CentOS6lsrv02 conf]# ab -c 1000 -n 10000 http://192.168.0.12/index.html

efbb27bef20aa0a4c3ab470c41effb97.jpg


[root@CentOS6lsrv02 conf]# ab -c 1000 -n 50000 http://192.168.0.12/index.html

1c7f92cc8043e4b1916e754f33bc2bb7.jpg


2.2 測試Apache靜態頁面處理能力

停止Nginx併發啓動Apache,爲保證測試結果可靠性,把nginx的默認站點主頁也拷貝到Apache站點目錄下。

[root@CentOS6lsrv02 ~]# pkill nginx

[root@CentOS6lsrv02 ~]# cp/opt/nginx/html/index.html /var/www/html/

[root@CentOS6lsrv02 ~]# 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  ]

[root@CentOS6lsrv02 ~]# netstat -tunpl|grep httpd

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


使用ab開啓1000併發和請求10000次進行測試:

[root@CentOS6lsrv02 ~]# ab -c 1000 -n 10000 htpt://192.168.0.12/index.html

e97c970bc508d69310a3ec5fee2c442e.jpg


[root@CentOS6lsrv02 ~]# ab -c 1000 -n 50000 htpt://192.168.0.12/index.html

cfca863afb9a766c48dd1a97ab014da1.jpg


小結:

1. 以上過程雖然算不上專業的壓力測試,但也在一定程度上可以看出Nginx作爲前端WEB應用,靜態頁面處理能力確實比Apache強很多!!!!

2. Nginx處理靜態頁面處理能力非常出衆外,還可以代理多種應用HTTPFTPPOP3/IMAPTOMCAT等),真的非常強大。

 

後期將會陸續發佈關於Nginx的其它應用,希望各位感興趣的運維同行一起交流,共同進步。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章