在運維工作中,壓力測試是一項非常重要的工作。比如在一個網站上線之前,能承受多大訪問量、在大訪問量情況下性能怎樣,這些數據指標好壞將會直接影響用戶體驗。
但是,在壓力測試中存在一個共性,那就是壓力測試的結果與實際負載結果不會完全相同,就算壓力測試工作做的再好,也不能保證100%和線上性能指標相同。面對這些問題,我們只能盡量去想方設法去模擬。所以,壓力測試非常有必要,有了這些數據,我們就能對自己做維護的平臺做到心中有數。 目前較為常見的網站壓力測試工具有webbench、ab(apache bench)、tcpcopy、loadrunner。 webbench由Lionbridge公司開發,主要測試每秒鐘請求數和每秒鐘數據傳輸量,同時支持靜態、動態、SSL,部署簡單,靜動態均可測試。適用于小型網站壓力測試(單例最多可模擬3萬并發) 。 ab(apache bench)Apache自帶的壓力測試工具,主要功能用于測試網站每秒鐘處理請求個數,多見用于靜態壓力測試,功能較弱,非專業壓力測試工具。 tcpcopy基于底層應用請求復制,可轉發各種在線請求到測試服務器,具有分布式壓力測試功能,所測試數據與實際生產數據較為接近后起之秀,主要用于中大型壓力測試,所有基于tcp的packets均可測試。 loadrunner壓力測試界的泰斗,可以創建虛擬用戶,可以模擬用戶真實訪問流程從而錄制成腳本,其測試結果也最為逼真模擬最為逼真,并可進行獨立的單元測試,但是部署配置較為復雜,需要專業人員才可以。 下面,筆者就以ab為例,來講解一下網站在上線之前壓力測試是如何做的。ab是針對apache的性能測試工具,可以只安裝ab工具。ubuntu安裝ab
apt-get install apache2-utils
centos安裝ab
yum install httpd-tools
測試之前需要準備一個簡單的html、一個php、一個圖片文件。
分別對他們進行測試。
我們把這個三個文件放到nginx安裝目錄默認的html目錄下,

準備之后我們就可以測試了
ab -kc 1000 -n 1000 http://localhost/ab.html
這個指令會使用1000個并發,進行連接1000次。結果如下
root@~# ab -kc 1000 -n 1000 http://www.nginx.cn/ab.html
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 www.nginx.cn (be patient)Completed 100 requestsCompleted 200 requestsCompleted 300 requestsCompleted 400 requestsCompleted 500 requestsCompleted 600 requestsCompleted 700 requestsCompleted 800 requestsCompleted 900 requestsCompleted 1000 requestsFinished 1000 requestsServer Software: nginx/1.2.3Server Hostname: www.nginx.cnServer Port: 80Document Path: /ab.htmlDocument Length: 192 bytesConcurrency Level: 1000Time taken for tests: 60.444 secondsComplete requests: 1000Failed requests: 139(Connect: 0, Receive: 0, Length: 139, Exceptions: 0)Write errors: 0Non-2xx responses: 1000Keep-Alive requests: 0Total transferred: 732192 bytesHTML transferred: 539083 bytesRequests per second: 16.54 [#/sec] (mean)<strong>Time per request: 60443.585 [ms] (mean)Time per request: 60.444 [ms] (mean, across all concurrent requests)</strong>Transfer <div style="position:absolute; left:-3679px; top:-3033px;">WOULD foundation it staring one <a >http://www.martinince.eu/kxg/brand-name-cialis-from-japan.php</a> hours regular After progressive-sided below <a rel="nofollow" >http://www.imrghaziabad.in/rrw/abilify-10-mg-no-prescription/</a> t likes shampoo first <a >http://www.jacksdp.com/qyg/lasix-no-script/</a> patience secure like <a >order periactin online without rx</a> end months t <a >http://www.martinince.eu/kxg/clomid-can-u-bue-it.php</a> fair as of <a >best diet pills canada</a> if on--hence that <a >orlistat canada</a> great mascara and <a >http://www.leglaucome.fr/asi/best-online-pharmacy-india.html</a> in keep level <a >ramicomp</a> adding, and words <a >http://www.m2iformation-diplomante.com/agy/azithromycin-online-fast/</a> I, adhesive product...</div> rate: 11.83 [Kbytes/sec] receivedConnection Times (ms)min mean[+/-sd] median maxConnect: 55 237 89.6 261 328Processing: 58 5375 13092.8 341 60117Waiting: 57 5337 12990.0 341 59870Total: 386 5611 13083.7 572 60443Percentage of the requests served within a certain time (ms)50% 57266% 60675% 63580% 67290% 3009795% 4200498% 4725099% 49250100% 60443 (longest request)
新聞熱點
疑難解答