Java并发测试与Apache ab测试

测试是开发过程中必不可少的,而并发测试也会越来越多,这里简单地进行一下100个并发量的测试,主要是使用CyclicBarrier来控制并发,CyclicBarrier是栅栏式的,线程相互等待直到个数达到设定的参数值,当然也可以通过 CountDownLatch来控制,代码如下。
    @Resource
    TestService testService;

    @Test
    public void test() {
        int count = 100;//设置并发数量
        CyclicBarrier cyclicBarrier = new CyclicBarrier(count);
        ExecutorService executorService = Executors.newFixedThreadPool(count);
        String parameter = "杂七杂八的各种参数";
        for (int i = 0; i < count; i++) {
            executorService.execute(new Task(cyclicBarrier, parameter));
        }
        executorService.shutdown();
        while (!executorService.isTerminated()) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }


    public class Task implements Runnable {

        private CyclicBarrier cyclicBarrier;
        private String parameter;

        public Task(CyclicBarrier cyclicBarrier, String parameter) {
            this.cyclicBarrier = cyclicBarrier;
            this.parameter = parameter;
        }

        @Override
        public void run() {
            if (testService == null) {
                System.out.println("error");
            }
            try {
                cyclicBarrier.await();
                ResultDO resultDO = testService.invoke(parameter);//要测试的方法
                assert resultDO != null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

ab是Apache自带的压力测试工具,安装完Apache之后就可以在bin里使用ab。

模拟1个请求,100个并发  

ab -n 100 -c 1 http://localhost:8080/test

其中-n请求数,-c并发数


返回结果

This is ApacheBench, Version 2.3 <$Revision: 1757674 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient).....done

Server Software:        Apache-Coyote/1.1
Server Hostname:        localhost
Server Port:            8080

Document Path:          /test
Document Length:        73 bytes      ##响应的正文长度

Concurrency Level:      100      ##并发数
Time taken for tests:   4.744 seconds      ##总耗时
Complete requests:      100      ##完成请求数
Failed requests:        0      ##失败请求数
Total transferred:      27100 bytes      ##总传输量
HTML transferred:       7300 bytes      ##HTML传输量
Requests per second:    21.08 [#/sec] (mean)      ##每秒请求数,吞吐量,是重要数据
Time per request:       4744.271 [ms] (mean)      ##服务器响应耗时
Time per request:       47.443 [ms] (mean, across all concurrent requests)      ##平均请求耗时
Transfer rate:          5.58 [Kbytes/sec] received      ##每秒网络上的流量

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.3      0       1
Processing:   958 3287 1358.8   3511    4738
Waiting:      902 3284 1359.9   3510    4737
Total:        958 3288 1358.8   3511    4738

Percentage of the requests served within a certain time (ms)
  50%   3511      ##50%的请求耗时在3511ms以内,下同
  66%   4598
  75%   4677
  80%   4686
  90%   4692
  95%   4696
  98%   4716
  99%   4738
 100%   4738 (longest request)




你可能感兴趣的:(Java)