springBoot使用Junit的ContiPerf测试

前言:主要是记录下自己走过的坑

一、环境

springBoot 2.2.6.RELEASE
contiperf 2.3.4
junit springboot2.2之后默认是junit5
Intellij IDEA 2019

二、实现

1、使用IDEA创建Spring Initializr项目,记得勾选spring Web
springBoot使用Junit的ContiPerf测试_第1张图片
2、查看pom.xml内容
springboot2.2之后自动增加如下代码,意思就是排除对JUnit4的支持,我没找到ContiPerf支持JUnit5的方式,所以就需要项目对Junit4支持,要删除下方代码片段
springBoot使用Junit的ContiPerf测试_第2张图片

   
       
           org.junit.vintage
           junit-vintage-engine
       
   

我这里是注释了这段代码
springBoot使用Junit的ContiPerf测试_第3张图片
3、引入ContiPerf


    org.databene
    contiperf
    2.3.4
    test

4、创建相关业务类,springBoot使用Junit的ContiPerf测试_第4张图片
5、重点!重点!重点!在测试包下项目自动生成Demo3ApplicationTests测试类,我们可以再这个类中写,也可以自己再创建一个。如果使用自动生成测试类,注意给类增加public修饰
springBoot使用Junit的ContiPerf测试_第5张图片
最终代码如下

package com.example.demo3;

import com.example.demo3.service.HelloService;
import org.databene.contiperf.PerfTest;
import org.databene.contiperf.junit.ContiPerfRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Demo3ApplicationTests {

    @Autowired
    private HelloService helloService;

    @Rule
    public ContiPerfRule contiPerfRule = new ContiPerfRule();

    @Test
    // 100个线程,执行10000次
    @PerfTest(invocations = 10000, threads = 100)
    public void test1() {
        String a = helloService.add();
        System.out.println(a);
    }
}

@PerfTest注解:
threads:线程池数量,并发执行n个线程,和其他两个参数配合使用
invocations:执行次数,与线程数量无关,默认值为1
duration:重复地执行时间,测试至少执行毫秒

6、Run Test 运行
springBoot使用Junit的ContiPerf测试_第6张图片
执行结果在这里,浏览器打开即可springBoot使用Junit的ContiPerf测试_第7张图片
7、结果
springBoot使用Junit的ContiPerf测试_第8张图片

图表中(浏览器翻译):
springBoot使用Junit的ContiPerf测试_第9张图片

你可能感兴趣的:(springBoot使用Junit的ContiPerf测试)