Apache Bench 壓測工具


Apache Bench 壓測工具


Apach Bench 壓測工具

 本篇主要講解Apach Bench的基本使用 包括基本命令 和 對返回結果參數的詳解,它可以很輕鬆的發送一些併發請求 ,ab命令可以創建很多的併發訪問線程,模擬多個訪問者同時對某一URL地址進行訪問 是一款非常好用的工具。

 1.Mac版本無需下載

 如果你是Mac用戶 那麼恭喜 Mac電腦自帶了Apach Bench工具,如果你是Windos用戶那麼請你面向百度 查詢如何安裝吧,我這裡不做解釋。


 2.基本命令講解


Apache Bench 用法

 Usage: ab [options] [http[s]://]hostname[:port]/path

 options 有很多可選項這裡我先說常用的幾個(最下面會貼出所有的 options )

 -n 發起的總請求數 

-c 併發數(模擬多少客戶端同時請求)
-t 測試所進行的最大秒數,限制測試在某時間內

 測試: 向百度發送 2000個請求,併發數200

 ab -c 200 -n 2000 https://www.baidu.com/
 johnny@localhost:~$ ab -c 200 -n 2000 https://www.baidu.com/
This is ApacheBench, Version 2.3
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
 Benchmarking www.baidu.com (be patient)
Completed 200 requests
Completed 400 requests
Completed 600 requests
Completed 800 requests
Completed 1000 requests
Completed 1200 requests
Completed 1400 requests
Completed 1600 requests
Completed 1800 requests
Completed 2000 requests
Finished 2000 requests
 Server Software: BWS/1.1
Server Hostname: www.baidu.com
Server Port: 443
SSL/TLS Protocol: TLSv1.2,ECDHE-RSA-AES128-GCM-SHA256,2048,128
TLS Server Name: www.baidu.com
 Document Path: /
Document Length: 227 bytes
 Concurrency Level: 200 #併發數
Time taken for tests: 21.999 seconds #完成這次測試的時間 既完成2000個請求的時間
Complete requests: 2000 #總請求數
Failed requests: 0 #失敗數
Total transferred: 2163799 bytes #整個場景中的網絡傳輸量,表示所有請求的響應數據長度總和,包括Http頭信息

HTML transferred: 454000 bytes #整個場景中的HTML內容傳輸量,表示所有請求的響應數據中正文數 據的總和 不帶Http頭的
Requests per second: 90.91 [#/sec] (mean) #重點! 吞吐量 -> Complete requests / Time taken for tests = (2000 / 21.999)
Time per request: 2199.874 [ms] (mean) #重點! 每次併發(200) 執行完成的時間 總請求時間/(總請求數/併發數) = (21.999 / (2000/200) )
Time per request: 10.999 [ms] (mean, across all concurrent requests) #重點! 併發數內的每個請求的平均相應時間-> 每次併發執行總時間/併發數 = (2199.874/ 200 )
Transfer rate: 96.05 [Kbytes/sec] received
 Connection Times (ms)
min mean[+/-sd] median max
Connect: 216 1523 470.0 1519 2320
Processing: 34 480 449.2 346 1959
Waiting: 34 390 390.0 322 1653
Total: 308 2002 212.5 1930 3753
 Percentage of the requests served within a certain time (ms)
50% 1930 #50%的用戶 相應時間小於 19毫秒
66% 1989
75% 2063
80% 2224 #80%的用戶 相應時間小宇 22毫秒
90% 2287
95% 2306
98% 2670
99% 2799
100% 3753 (longest request)

返回結果重點部分:

 Requests per second: 90.91 [#/sec] (mean) #重點! 吞吐量 -> Complete requests / Time taken for tests = (2000 / 21.999)
Time per request: 2199.874 [ms] (mean) #重點! 每次併發(200) 執行完成的時間 總請求時間/(總請求數/併發數) = (21.999 / (2000/200) )
Time per request: 10.999 [ms] (mean, across all concurrent requests) #重點! 併發數內的每個請求的平均相應時間-> 每次併發執行總時間/併發數 = (2199.874/ 200 )

 3.編寫接口 使用Apache Bench 測試


 private static int count = 0;
private static AtomicInteger atomicInteger = new AtomicInteger(0);
 /**
* 測試 count++
*/
@RequestMapping("/bench1")
public void bench2() throws InterruptedException {
count++;
Thread.sleep(100);
}
 /**
* 測試 ActomicInteger
*/
@RequestMapping("/bench2")
public void bench3() throws InterruptedException {
atomicInteger.getAndIncrement();
Thread.sleep(100);
}
 /**
* 打印最終的結果
*/
@RequestMapping("/printCount")
public void printCount() {
log.info("【count: {}】", count);
log.info("【AtomicCount: {}】", atomicInteger.get());
}

 第一個接口是 測試 count++ 第二個接口使 測試 ActomicInteger 第三個接口使 打印 count和ActomicInteger的值


 分別對接口 1 和 2 發送 2000個請求 併發數200

Apache Bench 壓測工具

訪問接口3 結果:

Apache Bench 壓測工具


 4.擴展Semaphore 和 CountDownLatch 模擬 ab併發請求

Semaphore 和 CountDownLatch 模擬 ab 發送2000個請求 200併發 ,不懂 Semaphore 和 CountDownLatch 自行百度吧。。。

 // 請求總數
public static int clientTotal = 2000;
 // 同時併發執行的線程數 

public static int threadTotal = 200;
// 信號量
final Semaphore semaphore = new Semaphore(threadTotal);
 // 計數器閉鎖
final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
 for (int i=0;i<clienttotal> executorService.execute(new Runnable() {
@Override
public void run() {
try {
semaphore.acquire();
add();
semaphore.release();
} catch (Exception e){
System.out.println(e);
}
// 每個線程執行時計數器都減1
countDownLatch.countDown();
}
});
}
###  5.ab命令所有的 options 貼圖/<clienttotal>
 Options are:
-n requests #執行的請求數,即一共發起多少請求。
-c concurrency #請求併發數。
-t timelimit #測試所進行的最大秒數。其內部隱含值是-n 50000,它可以使對服務器的測試限制在一個固定的總時間以內。默認時,沒有時間限制。
-s timeout #指定每個請求的超時時間,默認是30秒。
-b windowsize #指定tcp窗口的大小,單位是字節。
-B address #指定在發起連接時綁定的ip地址是什麼。
-p postfile #指定要POST的文件,同時要設置-T參數。
-u putfile #指定要PUT的文件,同時要設置-T參數。

-T content-type #指定使用POST或PUT上傳文本時的文本類型,默認是'text/plain'。
-v verbosity #設置詳細模式等級。
-w #將結果輸出到html的表中。
-i #使用HEAD方式代替GET發起請求。
-y attributes #以表格方式輸出時,設置html表格tr屬性。
-z attributes #以表格方式輸出時,設置html表格th或td屬性。
-C attribute #添加cookie,比如'Apache=1234'。(可重複)
-H attribute #為請求追加一個額外的頭部,比如'Accept-Encoding: gzip'。(可重複)
-A attribute #對服務器提供BASIC認證信任。用戶名和密碼由一個:隔開,並以base64編碼形式發送。無論服務器是否需要(即,是否發送了401認證需求代碼),此字符串都會被髮送。
-P attribute #對一箇中轉代理提供BASIC認證信任。用戶名和密碼由一個:隔開,並以base64編碼形式發送。無論服務器是否需要(即, 是否發送了401認證需求代碼),此字符串都會被髮送。
-X proxy:port #指定代理服務器的IP和端口。
-V #打印版本信息。
-k #啟用HTTP KeepAlive功能,即在一個HTTP會話中執行多個請求。默認時,不啟用KeepAlive功能。
-d #不顯示"percentage served within XX [ms] table"的消息(為以前的版本提供支持)。
-q #如果處理的請求數大於150,ab每處理大約10%或者100個請求時,會在stderr輸出一個進度計數。此-q標記可以抑制這些信息。

-g filename #把所有測試結果寫入一個'gnuplot'或者TSV(以Tab分隔的)文件。此文件可以方便地導入到Gnuplot,IDL,Mathematica,Igor甚至Excel中。其中的第一行為標題。
-e filename #產生一個以逗號分隔的(CSV)文件,其中包含了處理每個相應百分比的請求所需要(從1%到100%)的相應百分比的(以微妙為單位)時間。由於這種格式已經“二進制化”,所以比'gnuplot'格式更有用。
-r #當收到錯誤時不要退出。
-h #輸出幫助信息
-Z ciphersuite #指定SSL/TLS密碼套件
-f protocol #指定SSL/TLS協議(SSL3, TLS1, TLS1.1, TLS1.2 or ALL)



 5.總結

本篇主要講解了Apache Bench的基本使用 以及對壓測結果的返回進行分析 包括 吞吐量 平均併發請求時間等等 對於一些有併發的接口 可以自己測測 。。。

個人博客網站 https://www.askajohnny.com 歡迎訪問!

本文由博客一文多發平臺 https://openwrite.cn?from=article_bottom 發佈!


分享到:


相關文章: