使用ab軟體進行壓力測驗
- 一、ab
- 1.ab是什么?
- 2.ab的原理
- 二、壓力測驗
- 三、使用ab做壓力測驗
- 1.安裝ab
- 2.查看ab版本
- 3.ab幫助檔案查看引數
- 4.常用引數
- 5.ab測驗發送請求
- 6.監控負載均衡器和nginx服務器
- 7.ab測驗輸出說明
一、ab
1.ab是什么?
ab是apachebench命令的縮寫,ab是apache自帶的壓力測驗工具,ab非常實用,它不僅可以對apache服務器進行網站訪問壓力測驗,也可以對或其它型別的服務器進行壓力測驗,比如nginx、tomcat、IIS等
2.ab的原理
ab的原理:ab命令會創建多個并發訪問執行緒,模擬多個訪問者同時對某一URL地址進行訪問,它的測驗目標是基于URL的,因此,它既可以用來測驗apache的負載壓力,也可以測驗nginx、lighthttp、tomcat、IIS等其它Web服務器的壓力,
ab命令對發出負載的計算機要求很低,它既不會占用很高CPU,也不會占用很多記憶體,但卻會給目標服務器造成巨大的負載,其原理類似CC攻擊,自己測驗使用也需要注意,否則一次上太多的負載,可能造成目標服務器資源耗完,嚴重時甚至導致死機,
二、壓力測驗
對整個系統進行測驗 -->看整個服務器集群能承受多少的訪問量–>跟服務器的硬體和軟體配置有關系
因素:cpu、網路帶寬、磁盤IO
指標:QPS/TPS/IOPS/吞吐量
1.QPS
- QPS Query Per Second,既每秒請求、查詢次數
- nginx的web服務–>發http請求的數量
2.TPS
- Transcantion Per Second,既每秒事務數
3.IOPS
- IOPS 是指單位時間內系統能處理的I/O請求數量,一般以每秒處理的I/O請求數量為單位,I/O請求通常為讀或寫資料操作請求,
- 看資料庫或者磁盤–>值越大越好
優化
- 硬體層面–>提升硬體
- 軟體層面–>os層面–>nginx負載均衡–>后端的nginx配置
三、使用ab做壓力測驗
1.安裝ab
[root@ab-test ~]# yum -y install httpd-tools
2.查看ab版本
也可以查看ab是否安裝成功
[root@ab-test ~]# ab -V
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
3.ab幫助檔案查看引數
[root@ab-test ~]# ab -h
Usage: ab [options] [http[s]://]hostname[:port]/path
Options are:
-n requests Number of requests to perform
-c concurrency Number of multiple requests to make at a time
-t timelimit Seconds to max. to spend on benchmarking
This implies -n 50000
-s timeout Seconds to max. wait for each response
Default is 30 seconds
-b windowsize Size of TCP send/receive buffer, in bytes
-B address Address to bind to when making outgoing connections
-p postfile File containing data to POST. Remember also to set -T
-u putfile File containing data to PUT. Remember also to set -T
-T content-type Content-type header to use for POST/PUT data, eg.
'application/x-www-form-urlencoded'
Default is 'text/plain'
-v verbosity How much troubleshooting info to print
-w Print out results in HTML tables
-i Use HEAD instead of GET
-x attributes String to insert as table attributes
-y attributes String to insert as tr attributes
-z attributes String to insert as td or th attributes
-C attribute Add cookie, eg. 'Apache=1234'. (repeatable)
-H attribute Add Arbitrary header line, eg. 'Accept-Encoding: gzip'
Inserted after all normal header lines. (repeatable)
-A attribute Add Basic WWW Authentication, the attributes
are a colon separated username and password.
-P attribute Add Basic Proxy Authentication, the attributes
are a colon separated username and password.
-X proxy:port Proxyserver and port number to use
-V Print version number and exit
-k Use HTTP KeepAlive feature
-d Do not show percentiles served table.
-S Do not show confidence estimators and warnings.
-q Do not show progress when doing more than 150 requests
-l Accept variable document length (use this for dynamic pages)
-g filename Output collected data to gnuplot format file.
-e filename Output CSV file with percentages served
-r Don't exit on socket receive errors.
-m method Method name
-h Display usage information (this message)
-I Disable TLS Server Name Indication (SNI) extension
-Z ciphersuite Specify SSL/TLS cipher suite (See openssl ciphers)
-f protocol Specify SSL/TLS protocol
(SSL2, TLS1, TLS1.1, TLS1.2 or ALL)
-E certfile Specify optional client certificate chain and private key
4.常用引數
-n: 總的請求個數
-c: 一次并發的請求數
-t: 持續的時間,默認沒有限制
?-k:它會增加請求頭Connection: Keep-Alive,相當于開啟了HTTP長連接, 這樣做一方面可以降低測驗服務器動態埠被耗盡的風險,另一方面也有助于給目標服務器更大的壓力,測驗出更接近極限的結果
說明:ab使用的是HTTP/1.0,預設開啟的是短鏈接,用-k引數則可以打開長連接
5.ab測驗發送請求
一次并發100個請求,一共10000個請求
[root@ab-test ~]# ab -c 100 -n 10000 -k http://192.168.0.109/index.html
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.0.112 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software: nginx/1.21.1
Server Hostname: 192.168.0.112
Server Port: 80
Document Path: /index.html
Document Length: 20 bytes
Concurrency Level: 100
Time taken for tests: 2.485 seconds
Complete requests: 10000
Failed requests: 0
Keep-Alive requests: 10000
Total transferred: 2560000 bytes
HTML transferred: 200000 bytes
Requests per second: 4024.91 [#/sec] (mean)
Time per request: 24.845 [ms] (mean)
Time per request: 0.248 [ms] (mean, across all concurrent requests)
Transfer rate: 1006.23 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.9 0 13
Processing: 3 24 11.4 26 99
Waiting: 3 24 11.4 26 99
Total: 3 24 11.6 26 105
Percentage of the requests served within a certain time (ms)
50% 26
66% 29
75% 31
80% 32
90% 42
95% 46
98% 48
99% 51
100% 105 (longest request)
6.監控負載均衡器和nginx服務器
監控負載均衡器和nginx服務器查看CPU使用情況、記憶體使用情況等,
[root@LB ~]#dstat -a -m
7.ab測驗輸出說明
- Document Length: # 請求的頁面大小
- Concurrency Level: # 每次的并發請求數
- Time taken for tests: # 測驗總共耗時
- Complete requests: # 完成的請求總數
- Failed requests: # 失敗的請求數量
- Write errors: # 錯誤的請求數量
- Total transferred: # 總共傳輸資料量
- Requests per second 吞吐率(reqs/s):服務器每秒能夠處理的請求數量
- Time per request 平均請求處理時間,它的兩個值是:
第一行為每次并發請求的耗時,
第二行為每個請求的耗時
第一行值為第二行的值乘上并發請求數量,
可以嘗試將并發數改為20,
這樣就會看到第一行是第二行的20倍
- Transfer rate 表示吞吐量(BPS):即:每秒從服務器獲取的資料的長度
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/291962.html
標籤:其他
