天天看點

Nginx壓力測試工具之WebBench

在運維工作中,壓力測試是一項非常重要的工作。比如在一個網站上線之前,能承受多大通路量、在大通路量情況下性能怎樣,這些資料名額好壞将會直接影響使用者體驗。

  但是,在壓力測試中存在一個共性,那就是壓力測試的結果與實際負載結果不會完全相同,就算壓力測試工作做的再好,也不能保證100%和線上性能名額相同。面對這些問題,我們隻能盡量去想方設法去模拟。是以,壓力測試非常有必要,有了這些資料,我們就能對自己做維護的平台做到心中有數。

  目前較為常見的網站壓力測試工具有webbench、ab(apache bench)、tcpcopy、loadrunner。

  webbench由Lionbridge公司開發,主要測試每秒鐘請求數和每秒鐘資料傳輸量,同時支援靜态、動态、SSL,部署簡單,靜動态均可測試。适用于小型網站壓力測試(單例最多可模拟3萬并發) 。

  ab(apache bench)Apache自帶的壓力測試工具,主要功能用于測試網站每秒鐘處理請求個數,多見用于靜态壓力測試,功能較弱,非專業壓力測試工具。

  tcpcopy基于底層應用請求複制,可轉發各種線上請求到測試伺服器,具有分布式壓力測試功能,所測試資料與實際生産資料較為接近後起之秀,主要用于中大型壓力測試,所有基于tcp的packets均可測試。

  loadrunner壓力測試界的泰鬥,可以建立虛拟使用者,可以模拟使用者真實通路流程進而錄制成腳本,其測試結果也最為逼真模拟最為逼真,并可進行獨立的單元測試,但是部署配置較為複雜,需要專業人員才可以。

  下面,筆者就以webbench為例,來講解一下網站在上線之前壓力測試是如何做的。

安裝webbench

<span class="sh_preproc" style="color: rgb(160, 32, 240);">#wget</span> http<span class="sh_symbol">:</span><span class="sh_comment" style="color: rgb(255, 0, 0);">//home.tiscali.cz/~cz210552/distfiles/webbench-1.5.tar.gz</span>
<span class="sh_preproc" style="color: rgb(160, 32, 240);">#tar</span> <span class="sh_usertype">zxvf</span><span class="sh_normal"> </span>webbench<span class="sh_number" style="color: rgb(255, 0, 255);">-1.5</span><span class="sh_symbol">.</span>tar<span class="sh_symbol">.</span>gz
<span class="sh_preproc" style="color: rgb(160, 32, 240);">#cd</span> webbench<span class="sh_number" style="color: rgb(255, 0, 255);">-1.5</span>
<span class="sh_preproc" style="color: rgb(160, 32, 240);">#make</span> <span class="sh_symbol">&&</span> make install      

進行壓力測試,并發200時。

<span class="sh_preproc" style="color: rgb(160, 32, 240);"># webbench</span> <span class="sh_symbol">-</span>c <span class="sh_number" style="color: rgb(255, 0, 255);">200</span> <span class="sh_symbol">-</span>t <span class="sh_number" style="color: rgb(255, 0, 255);">60</span> http<span class="sh_symbol">:</span><span class="sh_comment" style="color: rgb(255, 0, 0);">//down.chinaz.com/index.php</span>      

參數解釋:-c為并發數,-t為時間(秒)

Webbench <span class="sh_symbol">-</span> Simple Web Benchmark <span class="sh_number" style="color: rgb(255, 0, 255);">1.5</span>
<span class="sh_function" style="font-weight: bold;">Copyright</span> <span class="sh_symbol">(</span>c<span class="sh_symbol">)</span> Radim Kolar <span class="sh_number" style="color: rgb(255, 0, 255);">1997-2004</span><span class="sh_symbol">,</span> GPL Open <span class="sh_usertype">Source</span><span class="sh_normal"> </span>Software<span class="sh_symbol">.</span>
Benchmarking<span class="sh_symbol">:</span> <span class="sh_usertype">GET</span><span class="sh_normal"> </span>http<span class="sh_symbol">:</span><span class="sh_comment" style="color: rgb(255, 0, 0);">//down.chinaz.com/index.php</span>
<span class="sh_number" style="color: rgb(255, 0, 255);">200</span> clients<span class="sh_symbol">,</span> running <span class="sh_number" style="color: rgb(255, 0, 255);">60</span> sec<span class="sh_symbol">.</span>
Speed<span class="sh_symbol">=</span><span class="sh_number" style="color: rgb(255, 0, 255);">1454</span> pages<span class="sh_symbol">/</span>min<span class="sh_symbol">,</span> <span class="sh_number" style="color: rgb(255, 0, 255);">2153340</span> bytes<span class="sh_symbol">/</span>sec<span class="sh_symbol">.</span>
Requests<span class="sh_symbol">:</span> <span class="sh_number" style="color: rgb(255, 0, 255);">1454</span> susceed<span class="sh_symbol">,</span> <span class="sh_number" style="color: rgb(255, 0, 255);">0</span> failed<span class="sh_symbol">.</span>      

當并發200時,網站通路速度正常

并發800時

<span class="sh_preproc" style="color: rgb(160, 32, 240);">#webbench</span> <span class="sh_symbol">-</span>c <span class="sh_number" style="color: rgb(255, 0, 255);">800</span> <span class="sh_symbol">-</span>t <span class="sh_number" style="color: rgb(255, 0, 255);">60</span> http<span class="sh_symbol">:</span><span class="sh_comment" style="color: rgb(255, 0, 0);">//down.chinaz.com/index.php</span>
Webbench <span class="sh_symbol">-</span> Simple Web Benchmark <span class="sh_number" style="color: rgb(255, 0, 255);">1.5</span>
<span class="sh_function" style="font-weight: bold;">Copyright</span> <span class="sh_symbol">(</span>c<span class="sh_symbol">)</span> Radim Kolar <span class="sh_number" style="color: rgb(255, 0, 255);">1997-2004</span><span class="sh_symbol">,</span> GPL Open <span class="sh_usertype">Source</span><span class="sh_normal"> </span>Software<span class="sh_symbol">.</span>
Benchmarking<span class="sh_symbol">:</span> <span class="sh_usertype">GET</span><span class="sh_normal"> </span>http<span class="sh_symbol">:</span><span class="sh_comment" style="color: rgb(255, 0, 0);">//down.chinaz.com/index.php</span>
<span class="sh_number" style="color: rgb(255, 0, 255);">800</span> clients<span class="sh_symbol">,</span> running <span class="sh_number" style="color: rgb(255, 0, 255);">60</span> sec<span class="sh_symbol">.</span>
Speed<span class="sh_symbol">=</span><span class="sh_number" style="color: rgb(255, 0, 255);">1194</span> pages<span class="sh_symbol">/</span>min<span class="sh_symbol">,</span> <span class="sh_number" style="color: rgb(255, 0, 255);">2057881</span> bytes<span class="sh_symbol">/</span>sec<span class="sh_symbol">.</span>
Requests<span class="sh_symbol">:</span> <span class="sh_number" style="color: rgb(255, 0, 255);">1185</span> susceed<span class="sh_symbol">,</span> <span class="sh_number" style="color: rgb(255, 0, 255);">9</span> failed<span class="sh_symbol">.</span>      

當并發連接配接為800時,網站通路速度稍慢

并發1600時

<span class="sh_preproc" style="color: rgb(160, 32, 240);">#webbench</span> <span class="sh_symbol">-</span>c <span class="sh_number" style="color: rgb(255, 0, 255);">1600</span> <span class="sh_symbol">-</span>t <span class="sh_number" style="color: rgb(255, 0, 255);">60</span> http<span class="sh_symbol">:</span><span class="sh_comment" style="color: rgb(255, 0, 0);">//down.chinaz.com/index.php</span>
Webbench <span class="sh_symbol">-</span> Simple Web Benchmark <span class="sh_number" style="color: rgb(255, 0, 255);">1.5</span>
<span class="sh_function" style="font-weight: bold;">Copyright</span> <span class="sh_symbol">(</span>c<span class="sh_symbol">)</span> Radim Kolar <span class="sh_number" style="color: rgb(255, 0, 255);">1997-2004</span><span class="sh_symbol">,</span> GPL Open <span class="sh_usertype">Source</span><span class="sh_normal"> </span>Software<span class="sh_symbol">.</span>
Benchmarking<span class="sh_symbol">:</span> <span class="sh_usertype">GET</span><span class="sh_normal"> </span>http<span class="sh_symbol">:</span><span class="sh_comment" style="color: rgb(255, 0, 0);">//down.chinaz.com/index.php</span>
<span class="sh_number" style="color: rgb(255, 0, 255);">1600</span> clients<span class="sh_symbol">,</span> running <span class="sh_number" style="color: rgb(255, 0, 255);">60</span> sec<span class="sh_symbol">.</span>
Speed<span class="sh_symbol">=</span><span class="sh_number" style="color: rgb(255, 0, 255);">1256</span> pages<span class="sh_symbol">/</span>min<span class="sh_symbol">,</span> <span class="sh_number" style="color: rgb(255, 0, 255);">1983506</span> bytes<span class="sh_symbol">/</span>sec<span class="sh_symbol">.</span>
Requests<span class="sh_symbol">:</span> <span class="sh_number" style="color: rgb(255, 0, 255);">1183</span> susceed<span class="sh_symbol">,</span> <span class="sh_number" style="color: rgb(255, 0, 255);">73</span> failed<span class="sh_symbol">.</span>           

當并發連接配接為1600時,網站通路速度便非常慢了

并發2000時

<span class="sh_preproc" style="color: rgb(160, 32, 240);">#webbench</span> <span class="sh_symbol">-</span>c <span class="sh_number" style="color: rgb(255, 0, 255);">2000</span> <span class="sh_symbol">-</span>t <span class="sh_number" style="color: rgb(255, 0, 255);">60</span> http<span class="sh_symbol">:</span><span class="sh_comment" style="color: rgb(255, 0, 0);">//down.chinaz.com/index.php</span>
Webbench <span class="sh_symbol">-</span> Simple Web Benchmark <span class="sh_number" style="color: rgb(255, 0, 255);">1.5</span>
<span class="sh_function" style="font-weight: bold;">Copyright</span> <span class="sh_symbol">(</span>c<span class="sh_symbol">)</span> Radim Kolar <span class="sh_number" style="color: rgb(255, 0, 255);">1997-2004</span><span class="sh_symbol">,</span> GPL Open <span class="sh_usertype">Source</span><span class="sh_normal"> </span>Software<span class="sh_symbol">.</span>
Benchmarking<span class="sh_symbol">:</span> <span class="sh_usertype">GET</span><span class="sh_normal"> </span>http<span class="sh_symbol">:</span><span class="sh_comment" style="color: rgb(255, 0, 0);">//down.chinaz.com/index.php</span>
<span class="sh_number" style="color: rgb(255, 0, 255);">2000</span> clients<span class="sh_symbol">,</span> running <span class="sh_number" style="color: rgb(255, 0, 255);">60</span> sec<span class="sh_symbol">.</span>
Speed<span class="sh_symbol">=</span><span class="sh_number" style="color: rgb(255, 0, 255);">2154</span> pages<span class="sh_symbol">/</span>min<span class="sh_symbol">,</span> <span class="sh_number" style="color: rgb(255, 0, 255);">1968292</span> bytes<span class="sh_symbol">/</span>sec<span class="sh_symbol">.</span>
Requests<span class="sh_symbol">:</span> <span class="sh_number" style="color: rgb(255, 0, 255);">2076</span> susceed<span class="sh_symbol">,</span> <span class="sh_number" style="color: rgb(255, 0, 255);">78</span> failed<span class="sh_symbol">.</span>      

當并發2000時,網站便出現"502 Bad Gateway",由此可見web伺服器已無法再處理使用者通路請求

總結:

1、壓力測試工作應該放到産品上線之前,而不是上線以後

2、測試時盡量跨公網進行,而不是内網

3、測試時并發應當由小逐漸加大,比如并發100時觀察一下網站負載是多少、打開是否流程,并發200時又是多少、網站打開緩慢時并發是多少、網站打不開時并發又是多少

4、 應盡量進行單元測試,如B2C網站可以着重測試購物車、推廣頁面等,因為這些頁面占整個網站通路量比重較大

繼續閱讀