ContiPerf接口性能测试

ContiPerf

ContiPerf是一个轻量级的测试工具,基于JUnit 4 开发,可用于接口级的性能测试

可以指定在线程数量和执行次数,通过限制最大时间和平均执行时间来进行效率测试

具体使用方法

添加依赖

            <!--性能测试-->
            <dependency>
                <groupId>org.databene</groupId>
                <artifactId>contiperf</artifactId>
                <version>2.3.4</version>
                <scope>test</scope>
            </dependency>

在junit中使用

    /**
     * 激活性能测试
     * 否则@PerfTest 无法生效
     */
    @Rule
    public ContiPerfRule rule = new ContiPerfRule();



    @Test
    @PerfTest(invocations = 100000,threads = 1000)
    public void selectDailyReadingUserAvatarByDailyIdTest(){
        long id = (long) (Math.random()*600);
        dailyService.selectDailyReadingUserAvatarByDailyId(id);
    }

@PerfTest相关参数


@Documented
@Target({ METHOD, TYPE })
@Retention(RUNTIME)
public @interface PerfTest {

	/**
   * 常用的就是这个参数 执行次数  与线程无关
	 * The total number of invocations to perform - use this alternatively to {@link #duration()}.
	 * The default value is one. @see #duration()
	 */
	int invocations() default  1;

	/**
   * 间隔时间  可以暂时不用  因为性能测试主要是测试并发
	 * The number of milliseconds to run and repeat the test with the full number of configured threads -
	 * use this alternatively to {@link #invocations()}. When using a {@link #rampUp()}, the ramp-up times
	 * add to the duration.
	 * @see #duration()
	 */
	int duration() default -1;

	/**
  * 线程数 这个比较好理解
  * The number of threads which concurrently invoke the test. The default value is 1.
  */
	int threads() default  1;

	/**
	 * The number of milliseconds to wait before each thread is added to the currently active threads.
	 * On {@link #duration()}-based tests, the total ramp-up time of rampUp * (threads - 1) is added to the
	 * configured duration.
	 */
	int rampUp() default  0;

	/** The number of milliseconds to wait before the actual measurement and requirements monitoring is activated.
	 *  Use this to exclude ramp-up times from measurement or wait some minutes before dynamic optimizations are
	 *  applied (like code optimization or cache population). */
	int warmUp() default  0;

	/** Set this to true, if execution should stop with a failure message as soon as a configured {@link Required#max()}
	 * value is violated. Set it to false, if you are interested in performing a full measurement to get percentiles,
	 * throughput and more. The default value is false. */
	boolean cancelOnViolation() default false;

	/** The class of a {@link WaitTimer} implementation by which a wait time can be incurred between test invocations */
	Class<? extends WaitTimer> timer() default None.class;

	/** The parameters to initialize the {@link WaitTimer}.
	 * The meaning of the values is individual for the WaitTimer implementation. */
	double[] timerParams() default { };

	/** One ore more {@link Clock} classes to use for time measurement.
	 * The first one specified is the one relevant for requirements verification. */
	Class<? extends Clock>[] clocks() default { };

	// TODO v2.x int timeout()       default -1;

}

在上面我们一般只要配置invocations和threads参数即可

其他具体的感兴趣可以直接看源码,注释比较清楚

执行

和正常的junit使用一样,直接执行即可

只不过在执行完会有相应的结果在命令行中输入

samples: 100000
max:     23764
average: 2153.67736
median:  1478

其他的输出结果和junit的结果一样

图形化结果查看

在junit执行完毕,会在target/contiperf-report中有相关的执行结果,可以使用浏览器打开查看

输入图片说明

补充junit的使用方式

配置使用

  1. spring项目
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:META-INF/application.xml"})
  1. spring boot项目
@RunWith(SpringRunner.class)
@SpringBootTest

配置文件加载顺序

在Spring中只要记住一个原则,最近原则就好

外部配置替换内部配置

test中的配置优先加载,如果test中没有的配置向外查找加载

猜你喜欢

转载自my.oschina.net/kipeng/blog/1810841
今日推荐