并发模拟工具

@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++;
    }
}
发布了533 篇原创文章 · 获赞 898 · 访问量 111万+

猜你喜欢

转载自blog.csdn.net/qq_40794973/article/details/104156286