Nginx壓力測試工具之WebBench

樑東昇發表於2016-04-23

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

  但是,在壓力測試中存在一個共性,那就是壓力測試的結果與實際負載結果不會完全相同,就算壓力測試工作做的再好,也不能保證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網站可以著重測試購物車、推廣頁面等,因為這些頁面佔整個網站訪問量比重較大


相關文章