Apache Bench(AB)
小巧簡單
localhost:config-cache biguodong$ ab --help
ab: wrong number of arguments
Usage: ab [options] [http[s]://]hostname[:port]/path
Options are:
-n requests 請求總數
-c concurrency 請求并發(fā)數
-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)
-g filename Output collected data to gnuplot format file.
-e filename Output CSV file with percentages served
舉個例子
ab -n 1000 -c 10 http://127/0.0.1:8080/test
返回結果
localhost:config-cache biguodong$ ab -n 1000 -c 10 http://127.0.0.1:8080/test
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (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:
Server Hostname: 127.0.0.1
Server Port: 8080
Document Path: /test
Document Length: 4 bytes
Concurrency Level: 10//并發(fā)量
Time taken for tests: 6.699 seconds//整個測試使用時間
Complete requests: 1000//完成的請求數目
Failed requests: 0//失敗的請求書
Total transferred: 136000 bytes//標示所有請求的響應數據長度總和娘纷,包括每個http響應數據的頭信息和正文數據的信息塌忽,不包括http請求數據的長度,僅包括服務器流向用戶pc的數據總長度失驶;
HTML transferred: 4000 bytes//所有請求響應數據中正文數據的總和(除去頭信息)
Requests per second: 149.29 [#/sec] (mean)//吞吐率,是與并發(fā)數有關的枣购,是用Complete requests比上Time taken for tests即 1000/6.699
Time per request: 66.985 [ms] (mean)//用戶平均等待時間
Time per request: 6.699 [ms] (mean, across all concurrent requests)//服務器平均等待時間
Transfer rate: 19.83 [Kbytes/sec] received//單位時間內從服務器獲取到的數據長度Total transferred比上Time taken for tests即13600/6.699
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 1.0 0 8
Processing: 1 66 590.1 5 6059
Waiting: 0 35 417.3 4 5932
Total: 1 67 590.0 6 6059
Jmeter壓力測試
幾個參數
線程組的幾個概念:
- 線程數:即虛擬用戶數嬉探,有多少個虛擬用戶并發(fā)模擬,如果模擬100個用戶棉圈,輸入100就好了涩堤;
- Ramp-Up Period(in seconds)虛擬用戶增長時長:比如模擬一段時間的登錄時間段,比如20分鐘分瘾,但是胎围,一般測試肯定不會這么長,我們估計出登錄頻率最高的時間段德召,如果我們設置為300白魂,意思是100個用戶在5min中內登錄完畢;
- 循環(huán)次數:單個虛擬用戶執(zhí)行的次數上岗;
步驟
-
先添加一個http請求
添加一個Http請求
效果如下:
效果圖
-
如果我們的請求需要添加header信息
添加Header
效果如下:
效果圖
-
添加斷言查看結果
響應斷言福荸,用來判斷你的請求響應是否達到預期
響應斷言
效果如下:
效果圖
配置查看結果樹
查看結果樹
配置聚合報告,做統(tǒng)計信息處理肴掷;
聚合報告
響應斷言配合查看結果樹
響應斷言配合查看結果樹
可以查看每個請求的請求request 和 response
響應斷言配合聚合報告
響應斷言配合聚合報告
結果集中統(tǒng)計了所有測試的響應時間敬锐、錯誤率等聚合信息;
在配置一個圖形結果
圖形結果
效果如下:
效果圖
這里的各項數據含義自己要好好弄清楚奧,這里不贅述了
其他元件的使用自己去安裝一個jmeter壓力測試起來吧~~
代碼來做壓力測試
CountDownLatch
(字面的意思是計數器往下減的一個閉鎖)的使用
概念圖
- 假設計數器的值為 3
- 線程 A 調用了 await() 方法后呆瞻,當前線程 A 進入awaiting狀態(tài)台夺;
- 在其他進程的代碼里,每次執(zhí)行countDown()這個方法后痴脾,計數器的值就會減 1颤介;
- 當計數器從 3 主鍵變成 0 之后鹏倘,線程A就繼續(xù)執(zhí)行编丘;
CountDownLatch可以阻塞線程,并在達到一定條件后繼續(xù)執(zhí)行贾铝;
適合線程打到一定的條件后繼續(xù)其他的操作薯定;
Semphore
概念圖
字面意思:信號量始绍,類比交通工具-紅綠燈
可以阻塞進行,并且可以控制同一時間的并發(fā)量话侄;
適合控制線程的并發(fā)數亏推;
代碼拿來:
這里自定義了線程池学赛,以及自定義拒絕策略實現,這里為了測試在 add() 方法中添加了 sleep()等待
@Slf4j
@NotThreadSafe
public class CountExample1 {
/**
* 執(zhí)行總數吞杭,5000次加一操作
*/
private static final int REQUEST_TOTAL = 5000;
/**
* 同一時間的并發(fā)總數
*/
private static final int CONCURRENCY_COUNT = 20;
private static final int MAXIMUM_POOL_SIZE = 100;
private static int count = 0;
public static void main(String[] args) throws Exception{
// 所有請求執(zhí)行完畢之后統(tǒng)計我們的執(zhí)行結果盏浇,因此這里使用的是 REQUEST_TOTAL
final CountDownLatch countDownLatch = new CountDownLatch(REQUEST_TOTAL);
final Semaphore semaphore = new Semaphore(CONCURRENCY_COUNT);
// 初始化一個線程池
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("con-%s").build();
ExecutorService threadPoolExecutor = new ThreadPoolExecutor(CONCURRENCY_COUNT, MAXIMUM_POOL_SIZE, 30, TimeUnit.SECONDS, new ArrayBlockingQueue<>(30), threadFactory, new BlockingPolicy());
for(int i = 0; i < REQUEST_TOTAL; i ++) {
threadPoolExecutor.execute(() -> {
try{
// 每個線程執(zhí)行前需要嘗試獲取信號量(是否允許被執(zhí)行,如果達到并發(fā)數芽狗,當前線程會被阻塞)
semaphore.acquire();
add();
// 執(zhí)行完畢之后釋放信號量
semaphore.release();
}catch (Exception e){
log.error("caught some error", e);
}
// 線程執(zhí)行完畢后執(zhí)行次數 - 1
countDownLatch.countDown();
//countDownLatch.countDown();
});
}
// 所有線程執(zhí)行完畢之后绢掰,輸出我們的結果
countDownLatch.await();
// 線程池執(zhí)行完畢之后關閉線程池
threadPoolExecutor.shutdown();
log.info(">>>>>>>>>>>> : count:{}", count);
}
private static void add() throws InterruptedException{
TimeUnit.MILLISECONDS.sleep(100);
count ++;
}
}
上述測試了線程的隊列、線程池的拒接策略等問題童擎,實際中可將QueueSize 調整大于 5000滴劲,自定義拒絕策略中,把任務又重新放回了隊列中顾复;
不斷調整Sleep()時間大小班挖,越大,返回的count越小芯砸,因為每個線程執(zhí)行的時間加長萧芙,導致協會主存的count是同一個數的概率增大!
16:41:59.647 [main] INFO com.mmall.concurrency.example.count.bigd.CountExample1 - >>>>>>>>>>>> : count:4859
快使用起來吧~~