Apache
服務器負載太大而影響程序效率也是很常見的,我們需要對此進行測試。這裏我以目前最常用的Apache服務器爲例。 Apache服務器自帶有一個叫AB(ApacheBench)的工具,在bin目錄下。使用這個輕巧的工具我們可以對服務器進行負載測試,看看在重負荷之下服務器的表現如何。ApacheBench 可以針對某個特定的 URL 仿真出連續的聯機請求,同時還可以仿真出同時間點數個相同的聯機請求,因此利用 ApacheBench 可幫助我們在網站開發期間仿真實際上線可能的情況,利用仿真出來的數據作爲調整服務器設定或程序的依據。
在命令行下輸出:
./ab -n number_of_total_requests \
-c number_of_simultaneous_requests \
http://your_web_server/your_php_app.php
例如:./ab -n 1000 -c 50http://www.domain.com/myapp.php
AB將同時向http://www.domain.com/myapp.php發出50個併發請求,共發出1000次。
測試結果將可能是這樣的:
Server Software: Apache/2.0.16
Server Hostname: localhost
Server Port: 80
Document Path: /myapp.php
Document Length: 1311 bytes
Concurrency Level: 50
Time taken for tests: 8.794 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 1754000 bytes
HTML transferred: 1311000 bytes
Requests per second: 113.71
Transfer rate: 199.45 kb/s received
Connection Times (ms)
min avg max
Connect: 0 0 5
Processing: 111 427 550
Total: 111 427 555
myapp.php每秒鐘可以處理的請求數爲113.71個。將請求數增加,看看服務器能否處理更大的壓力。你也需要調節Apache的MaxClients,ThreadsPerChild,MaxThreadsPerChild等參數,基於你的httpd.conf中的MPM模塊選擇。
如果你想得到更詳細的信息,請到www.apache.org上查閱一些更深入的文檔,包括模塊和第三方的提高效率的工具。修改httpd.conf後,要重啓Apache服務器,然後再用AB測試。你會看到每秒請求數增加或減少。
記下每次的參數,最後選擇最佳效率的那種配置。要指出的是,除了AB,還有許多優秀的服務器性能測試軟件。另外,如果你的服務器不是Apache,請自行尋找測試方法。
APACHE的bin目錄下
格式.ab [options] [http://]hostname[:port]/path
參數
-n requests Number of requests to perform
//在測試會話中所執行的請求個數,即總請求數。默認時,僅執行一個請求
-c concurrency Number of multiple requests to make
//一次產生的請求個數,即併發的請求數。默認是一次一個。
-t timelimit Seconds to max. wait for responses
//測試所進行的最大秒數。其內部隱含值是-n 50000。它可以使對服務器的測試限制在一個固定的總時間以內。默認時,沒有時間限制。
-p postfile File containing data to POST
//包含了需要POST的數據的文件.
-T content-type Content-type header for POSTing
//POST數據所使用的Content-type頭信息。
-v verbosity How much troubleshooting info to print
//設置顯示信息的詳細程度 - 4或更大值會顯示頭信息, 3或更大值可以顯示響應代碼(404, 200等), 2或更大值可以顯示警告和其他信息。 -V 顯示版本號並退出。
-w Print out results in HTML tables
//以HTML表的格式輸出結果。默認時,它是白色背景的兩列寬度的一張表。
-i Use HEAD instead of GET
// 執行HEAD請求,而不是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)
//-C cookie-name=value 對請求附加一個Cookie:行。 其典型形式是name=value的一個參數對。此參數可以重複。
-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.
//-P proxy-auth-username:password 對一箇中轉代理提供BASIC認證信任。用戶名和密碼由一個:隔開,並以base64編碼形式發送。無論服務器是否需要(即, 是否發送了401認證需求代碼),此字符串都會被髮送。
-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
-h Display usage information (this message)
//-attributes 設置 屬性的字符串. 缺陷程序中有各種靜態聲明的固定長度的緩衝區。另外,對命令行參數、服務器的響應頭和其他外部輸入的解析也很簡單,這可能會有不良後果。它沒有完整地實現 HTTP/1.x; 僅接受某些’預想’的響應格式。 strstr(3)的頻繁使用可能會帶來性能問題,即, 你可能是在測試ab而不是服務器的性能。
參數很多,一般我們用 -c 和 -n 參數就可以了.