併發模擬工具

@Controller
public class TestController {
    @RequestMapping("/test")
    @ResponseBody
    public String test() {
        return "test";
    }

    @RequestMapping("/isLive")
    @ResponseBody
    public String test2() {
        return "live";
    }
}

1 Postman

Http請求模擬工具

 

 


2 Apache Bench

Apache Bench(AB):Apache附帶的工具,測試網站性能

ab -n 1000 -c 50 http://localhost:8080/test

  • -n:本次測試請求的總數
  • -c:併發數

Concurrency Level:      併發量                                                            
Time taken for tests:   整個測試所用的時間                                                 
Complete requests:      完成的請求數                                                          
Failed requests:        失敗的請求數                                                              
Total transferred:      所有請求的響應數據的長度總和,包括每個http響應數據的頭信息和正文數據的長度(不包括http請求數據的長度,僅僅爲web服務器流向用戶pc的應用層數據總長度)                                            
HTML transferred:       所有請求的響應數據中正文數據的總和(減去了Total transferred中http響應數據中的頭信息的長度)                                                
Requests per second:    吞吐率(和併發數相關,計算:Complete requests/Time taken for tests)                                         
Time per request:       用戶平均請求等待時間                                           
Time per request:       服務器平均請求等待時間            
Transfer rate:          單位時間內從服務器獲取的數據的長度(計算:Total transferred/Time taken for tests)

3 JMeter

JMeter:Apache組織開發的壓力測試工具

3.1 Windows 安裝 

3.3 使用

 

 

 

添加監聽器

 

 

 


4 代碼:Semaphore、CountDownLatch等

@Slf4j
public class ConcurrencyTest {
    // 請求總數
    public static int clientTotal = 5000;
    // 同時併發執行的線程數
    public static int threadTotal = 200;
    public static int count = 0;

    public static void main(String[] args) throws Exception {
        ExecutorService executorService = Executors.newCachedThreadPool();
        final Semaphore semaphore = new Semaphore(threadTotal);
        final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
        for (int i = 0; i < clientTotal ; i++) {
            executorService.execute(() -> {
                try {
                    semaphore.acquire();
                    add();
                    semaphore.release();
                } catch (Exception e) {
                    log.error("exception", e);
                }
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        executorService.shutdown();
        log.info("count:{}", count);
    }
    private static void add() {
        count++;
    }
}

 

發佈了537 篇原創文章 · 獲贊 2350 · 訪問量 204萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章