51Testing软件测试论坛

标题: ContiPerf接口性能测试 [打印本页]

作者: 小文0111    时间: 2019-3-19 16:49
标题: ContiPerf接口性能测试
ContiPerf

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

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


具体使用方法添加依赖
  1.     <!--性能测试-->
  2.             <dependency>
  3.                 <groupId>org.databene</groupId>
  4.                 <artifactId>contiperf</artifactId>
  5.                 <version>2.3.4</version>
  6.                 <scope>test</scope>
  7.             </dependency>
复制代码
在junit中使用
  1. /**
  2.      * 激活性能测试
  3.      * 否则@PerfTest 无法生效
  4.      */
  5.     @Rule
  6.     public ContiPerfRule rule = new ContiPerfRule();



  7.     @Test
  8.     @PerfTest(invocations = 100000,threads = 1000)
  9.     public void selectDailyReadingUserAvatarByDailyIdTest(){
  10.         long id = (long) (Math.random()*600);
  11.         dailyService.selectDailyReadingUserAvatarByDailyId(id);
  12.     }
复制代码
@PerfTest相关参数
  1. @Documented
  2. @Target({ METHOD, TYPE })
  3. @Retention(RUNTIME)
  4. public @interface PerfTest {

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

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

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

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

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

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

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

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

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

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

  47. }
复制代码
在上面我们一般只要配置invocations和threads参数即可

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


执行

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

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

  1. samples: 100000
  2. max:     23764
  3. average: 2153.67736
  4. median:  1478
复制代码

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


图形化结果查看

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

[attach]123043[/attach]


补充junit的使用方式配置使用
  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(locations = {"classpath:META-INF/application.xml"})
复制代码
  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest
复制代码

配置文件加载顺序

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

外部配置替换内部配置

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




作者: 海海豚    时间: 2019-3-20 11:25
谢谢分享




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2