Nginx 高階篇(八)ab 壓力測試即 nginx 的效能統計模組

huxiaobai_001發表於2020-03-21

我們總得要對自己的網站或者介面做壓力測試的,總不能寫好了程式碼不做測試就上線啊,誰知道你的網站或者介面能承受多少的併發和訪問量,壓力測試我們可以使用apache的ab小工具來搞或者使用github上提供了一版本hey 這裡我們只講ab小工具哈 hey也很簡單 linux上下載下來直接使用即可!
ab,即Apache Benchmark,只要我們安裝了Apache,就能夠在Apache的安裝目錄中找到它。它的居住地址是Apache安裝目錄/bin/ab.exe,我的是位於bin/ab.exe 現在,我們就來看看如何使用ab.exe來進行壓力測試。
雖然ab可以配置的引數選項比較多,但是,一般情況下我們只需要使用形如ab -n 數字 -c 數字 url路徑的命令即可。譬如,我們對位於本地Apache伺服器上、URL為localhost/index.php的頁面進行壓力測試。測試總次數為1000,併發數為100(相當於100個使用者同時訪問,他們總共訪問1000次)
輸入命令:

ab -n 1000 -c 100 http://192.168.1.168/index/index

引數介紹 自己看吧:

Benchmarking localhost (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:        Apache/2.2.25 (伺服器軟體名稱及版本資訊)
Server Hostname:        localhost (伺服器主機名)
Server Port:            80 (伺服器埠)
Document Path:          /index.php (供測試的URL路徑)
Document Length:        10 bytes (供測試的URL返回的文件大小)
Concurrency Level:      100 (併發數)
Time taken for tests:   0.247 seconds (壓力測試消耗的總時間)
Complete requests:      1000 (壓力測試的總次數)
Failed requests:        0 (失敗的請求數)
Write errors:           0 (網路連線寫入錯誤數)
Total transferred:      198000 bytes (傳輸的總資料量)
HTML transferred:       10000 bytes (HTML文件的總資料量)
Requests per second:    4048.34 [#/sec] (mean) (平均每秒的請求數)
Time per request:       24.701 [ms] (mean) (所有併發使用者(這裡是100)都請求一次的平均時間)
Time per request:       0.247 [ms] (mean, across all concurrent requests) (單個使用者請求一次的平均時間)
Transfer rate:          782.78 [Kbytes/sec] received (傳輸速率,單位:KB/s)
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.3      0       1
Processing:     6   23   4.2     24      30
Waiting:        5   20   5.3     21      29
Total:          6   23   4.2     24      30

Percentage of the requests served within a certain time (ms)
  50%     24
  66%     25
  75%     26
  80%     26
  90%     27
  95%     27
  98%     28
  99%     29
 100%     30 (longest request)
ab -n 20000 -c 10000 http://192.168.1.168/index/index

如果請求量較大 linux客戶埠會報錯連結太多 只需要執行命令:

#這個數值可以調整的更大
ulimit -n 20000

ulimit是幹哈滴 請自行百度!

除此之外 我們還需要Nginx的效能統計模組 這個你得需要編譯安裝哦
–with-http_stub_status_module 加上這個模組即可!

pkill -9 nginx;
cd /usr/local/src/nginx-1.16.1
make clean
#重新編譯安裝Nginx 不要忘記之前的引數
./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/ngx_http_consistent_hash-master --with-http_stub_status_module --add-module=/usr/local/src/echo-nginx-module-0.62rc1

這還沒完 你得能訪問它啊
找到nginx.conf檔案 先進一個location

 location /status {
            stub_status on;
            access_log off;
            #也可以限制ip  除了1.9的ip其他ip禁止訪問 因為統計資訊比較敏感
            allow 192.168.1.9
            deny all;
        }

然後你去訪問
192.168.1.168/status

Nginx高階篇(八)ab壓力測試即nginx的效能統計模組

完事!

測壓
併發10000 總請求量20000 你會看到 臥槽 我瀏覽器訪問 咋卡死啦!還會發現ab的測試報告裡面咋這麼多失敗的請求呢?
不卡死才怪呢!不失敗才怪!
我們接下來講講如何優化Nginx!

本作品採用《CC 協議》,轉載必須註明作者和本文連結

胡軍

相關文章