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

準(zhǔn)備之后我們就可以測(cè)試了
ab -kc 1000 -n 1000 http://localhost/ab.html
這個(gè)指令會(huì)使用1000個(gè)并發(fā),進(jìn)行連接1000次。結(jié)果如下
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)
新聞熱點(diǎn)
疑難解答
圖片精選