使用Apache的ab工具對比Nginx與Apache靜態頁面處理能力html
前言:爲何要進行壓力測試前端
在運維工做中,壓力測試是一項很是重要的工做。好比在一個網站上線以前,能承受多大訪問量、在大訪問量狀況下性能怎樣,這些數據指標好壞將會直接影響用戶體驗。可是,在壓力測試中存在一個共性,那就是壓力測試的結果與實際負載結果不會徹底相同,就算壓力測試工做作的再好,也不能保證100%和線上性能指標相同。面對這些問題,咱們只能儘可能去千方百計去模擬。因此,壓力測試很是有必要,有了這些數據,咱們就能對本身作維護的平臺作到心中有數。linux
目的:nginx
這篇文章主要是承接我上一篇博文地球最強WEB前端Nginx服務器之搭建基礎靜態WEB站點,經過Apache自帶壓力測試工具ab簡單的證實一下說Nginx是地球最強的WEB前端絕對不是吹牛逼的。web
1. 壓力測試工具ab簡介:apache
ab是Apache自帶的壓力測試工具,使用簡單,雖然不是業界最專業的壓力測試工具,但測試結果也是有必定參考價值的。windows
1.1業界常見的壓力測試工具centos
目前較爲常見的網站壓力測試工具備webbench、ab(apache bench)、tcpcopy、loadrunner。服務器
1)webbench:cookie
由Lionbridge公司開發,主要測試每秒鐘請求數和每秒鐘數據傳輸量,同時支持靜態、動態、SSL,部署簡單,靜動態都可測試。適用於小型網站壓力測試(單例最多可模擬3萬併發)
2)ab(apache bench):
Apache自帶的壓力測試工具,主要功能用於測試網站每秒鐘處理請求個數多見用於靜態壓力測試,功能較弱,非專業壓力測試工具。這也是本文使用的工具。
3)tcpcopy:基於底層應用請求複製,可轉發各類在線請求到測試服務器,具備分佈式壓力測試功能,所測試數據與實際生產數據較爲接近後起之秀,主要用於中大型壓力測試,全部基於tcp的packets都可測試
4)loadrunner:
壓力測試界的泰斗,能夠建立虛擬用戶,能夠模擬用戶真實訪問流程從而錄製成腳本,其測試結果也最爲逼真模擬最爲逼真,並可進行獨立的單元測試,可是部署配置較爲複雜,須要專業人員才能夠。
1.2基礎環境準備
因爲本文是基於我上一篇博文地球最強WEB前端Nginx服務器之搭建基礎靜態WEB站點而寫的,因此使用的測試環境也使用寫上一篇博文時使用的軟硬件環境,這也是爲了公平起見嘛!^_^
本文例子使用的系統和軟件環境:
操做系統:CentOS 6.5 64位版
安裝軟件:nginx-1.8.0、httpd.x86_64(2.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
[root@CentOS6lsrv02 conf]# ab -c 1000 -n 50000 http://192.168.0.12/index.html
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
[root@CentOS6lsrv02 ~]# ab -c 1000 -n 50000 htpt://192.168.0.12/index.html
小結:
1. 以上過程雖然算不上專業的壓力測試,但也在必定程度上能夠看出Nginx做爲前端WEB應用,靜態頁面處理能力確實比Apache強不少!!!!
2. Nginx處理靜態頁面處理能力很是出衆外,還能夠代理多種應用(HTTP、FTP、POP3/IMAP、TOMCAT等),真的很是強大。
後期將會陸續發佈關於Nginx的其它應用,但願各位感興趣的運維同行一塊兒交流,共同進步。