首頁  >  文章  >  運維  >  怎麼配置ab來為Nginx伺服器做壓力測試

怎麼配置ab來為Nginx伺服器做壓力測試

PHPz
PHPz轉載
2023-05-17 16:40:51729瀏覽

ab是apache的效能測試工具,可以只安裝ab工具。

ubuntu安裝ab

apt-get install apache2-utils

centos安裝ab

yum install httpd-tools

測試之前需要準備一個簡單的html、一個php、一個圖片檔案。

分別對他們進行測試。

我們把這個三個檔案放到nginx安裝目錄預設的html目錄下,

怎麼配置ab來為Nginx伺服器做壓力測試

已經可以測試了

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 requests
completed 200 requests
completed 300 requests
completed 400 requests
completed 500 requests
completed 600 requests
completed 700 requests
completed 800 requests
completed 900 requests
completed 1000 requests
finished 1000 requests
server software: nginx/1.2.3
server hostname: www.nginx.cn
server port: 80

document path: /ab.html
document length: 192 bytes

concurrency level: 1000
time taken for tests: 60.444 seconds
complete requests: 1000
failed requests: 139
(connect: 0, receive: 0, length: 139, exceptions: 0)
write errors: 0
non-2xx responses: 1000
keep-alive requests: 0
total transferred: 732192 bytes
html transferred: 539083 bytes
requests 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 href="http://www.martinince.eu/kxg/brand-name-cialis-from-japan.php">http://www.martinince.eu/kxg/brand-name-cialis-from-japan.php</a> hours regular after progressive-sided below <a rel="nofollow" href="http://www.imrghaziabad.in/rrw/abilify-10-mg-no-prescription/">http://www.imrghaziabad.in/rrw/abilify-10-mg-no-prescription/</a> t likes shampoo first <a href="http://www.jacksdp.com/qyg/lasix-no-script/">http://www.jacksdp.com/qyg/lasix-no-script/</a> patience secure like <a href="http://www.meda-comp.net/fyz/order-periactin-online-without-rx.html">order periactin online without rx</a> end months t <a href="http://www.martinince.eu/kxg/clomid-can-u-bue-it.php">http://www.martinince.eu/kxg/clomid-can-u-bue-it.php</a> fair as of <a href="http://www.ljscope.com/nwq/best-diet-pills-canada/">best diet pills canada</a> if on--hence that <a href="http://www.jacksdp.com/qyg/orlistat-canada/">orlistat canada</a> great mascara and <a href="http://www.leglaucome.fr/asi/best-online-pharmacy-india.html">http://www.leglaucome.fr/asi/best-online-pharmacy-india.html</a> in keep level <a href="http://www.litmus-mme.com/eig/ramicomp.php">ramicomp</a> adding, and words <a href="http://www.m2iformation-diplomante.com/agy/azithromycin-online-fast/">http://www.m2iformation-diplomante.com/agy/azithromycin-online-fast/</a> i, adhesive product...</div> rate: 11.83 [kbytes/sec] received

connection times (ms)
min mean[+/-sd] median max
connect: 55 237 89.6 261 328
processing: 58 5375 13092.8 341 60117
waiting: 57 5337 12990.0 341 59870
total: 386 5611 13083.7 572 60443

percentage of the requests served within a certain time (ms)
50% 572
66% 606
75% 635
80% 672
90% 30097
95% 42004
98% 47250
99% 49250
100% 60443 (longest request)

對於php檔案和圖片檔案可以使用同樣指令進行,結果我就不貼出來了。

ab -kc 500 -n 5000 http://localhost/ab.php

ab -kc 500 -n 5000 http://localhost/ab.gif

輸出結果我們可以從字面意思就可以理解。

這裡對兩個比較重要的指標做下說明

例如

requests per second: 16.54 [#/sec] (mean)
time per request: 60443.585 [ms] (mean)

requests per second: 16.54 [#/sec] (mean)

#表示目前測試的伺服器每秒可以處理16.54個靜態html的請求事務,後面的mean表示平均。這個數值表示目前機器的整體效能,數值越大越好。

time per request: 60443.585 [ms] (mean)

單一並發的延遲時間,後面的mean表示平均。
隔離開目前並發,單獨完成一個請求所需的平均時間。

順帶說一下兩個time per request區別

time per request: 60443.585 [ms] (mean)
time per request: 60.444 [ms] (mean, across all concurrent requests)

前一個衡量單一請求的延遲,cpu是分時間片輪流執行請求的,多並發的情況下,一個並發上的請求時需要等待這麼長時間才能得到下一個時間片。
計算方法time per request: 60.444 [ms] (mean, across all concurrent requests)*並發數

通俗點說就是當以-c 10的並發下完成-n 1000個請求的同時,額外加入一個請求,完成這個求平均需要的時間。

後一個衡量性能的標準,它反映了完成一個請求需要的平均時間,在當前的並發情況下,增加一個請求需要的時間。
計算方法time taken for tests: 60.444 seconds/complete requests: 1000

通俗點說就是當以-c 10的並發下完成-n 1001個請求時,比完成-n1000個請求多花的時間。
你可以適當地調整-c 和-n大小來測試伺服器效能,借助htop指令來直覺的查看機器的負載狀況。

我的機器是盛大雲端的超微主機,平常負載cpu是1.7%,htop指令結果截圖

怎麼配置ab來為Nginx伺服器做壓力測試

加壓後的負載100%,負載基本上已經上來了。 htop指令結果截圖

怎麼配置ab來為Nginx伺服器做壓力測試

看來我需要好好優化一下,或是就換台機器了。

ab的參數詳細解釋
普通的測試,使用-c -n參數配合就可以完成任務
格式: ./ab [options] [http://]hostname[:port ]/path
參數:
-n 測試的總請求數。預設時,僅執行一個請求
-c 一次並發請求個數。預設是一次一個。
-h 新增請求頭,例如 ‘accept-encoding: gzip',以gzip方式請求。
-t 測試所進行的最大秒數。其內部隱含值是-n 50000。它可以使伺服器的測試限制在一個固定的總時間以內。預設時,沒有時間限制。
-p 包含了需要post的資料的檔案.
-t post資料所使用的content-type頭資訊。
-v 設定顯示訊息的詳細程度 – 4或更大值會顯示頭訊息, 3或更大值可以顯示回應碼(404, 200等), 2或更大值可以顯示警告和其他資訊。 -v 顯示版本號並退出。
-w 以html表的格式輸出結果。預設時,它是白色背景的兩列寬度的一張表。
-i 執行head請求,而不是get。
-c -c cookie-name=value 對請求附加一個cookie:行。其典型形式是name=value的一個參數對。此參數可以重複。

以上是怎麼配置ab來為Nginx伺服器做壓力測試的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除