性能工具之代码级性能测试工具ContiPerf

ContiPerf 简介

ContiPerf 是一个轻量级的测试工具,基于JUnit 4 开发,可用于效率测试等。可以指定在线程数量和执行次数,通过限制最大时间和平均执行时间来进行性能测试。

官网地址:https://sourceforge.net/p/contiperf/wiki/Home/

ContiPerf 使用

 <!-- ContiPerf是一个轻量级的性能测试工具 -->
        <dependency>
            <groupId>org.databene</groupId>
            <artifactId>contiperf</artifactId>
            <version>2.3.4</version>
            <scope>test</scope>
        </dependency>
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisServiceImplTest {

    //引入 ContiPerf 进行性能测试
    @Rule
    public ContiPerfRule rule = new ContiPerfRule();

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //@PerfTest(threads = 10, invocations = 100)// 10个线程并发执行,每个线程执行10次,总共执行100次
    //@PerfTest(threads = 10, duration = 300)// 10个线程并发执行,在300毫秒内反复执行
    @PerfTest(invocations = 10, threads = 10, timer = RandomTimer.class, timerParams = {30, 80})
// 每执行完一次会等待30~80ms然后才会执行下一次调用
    @Test
    public void testPing() throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            String ping = ping();
            System.out.println(ping);
        }
        Thread.sleep(100000);
    }

    /**
     * redis ping 测试
     *
     * @return
     */
    public String ping() {
        return stringRedisTemplate.execute(new RedisCallback<String>() {
            @Override
            public String doInRedis(RedisConnection connection) throws DataAccessException {
                return connection.ping();
            }
        });
    }
}

@Rule 是J unit 提供的一个扩展接口注解,其接口类为:org.junit.rules.MethodRule,注意在 Junit5 中,已经被 TestRule 所替代了。
也可以通过对类指定 @PerfTest 和 @Required,表示类中方法的默认设置。

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

@Required注解:
@Required(throughput = 20):要求每秒至少执行20个测试;
@Required(average = 50):要求平均执行时间不超过50ms;
@Required(median = 45):要求所有执行的50%不超过45ms;
@Required(max = 2000):所有执行的执行时间没有超过2s;
@Required(totalTime = 5000):要求总的执行时间不超过5s;
@Required(percentile90 = 3000):要求90%的测试不超过3s;
@Required(percentile95 = 5000):要求95%的测试不超过5s;
@Required(percentile99 = 10000):要求99%的测试不超过10s;
@Required(percentiles = “66:200,96:500”):要求66%的测试不超过200ms,96%的测试不超过500ms。

同时访问:target/contiperf-report/index.html,会生成图表:

在这里插入图片描述

图表中的指标:
Execution time: 执行时间
Throughput: TPS
Min. latency: 最小响应时间
Average latency: 平均响应时间
Median: 响应时间中位数
90%: 90%响应时间范围
Max latency: 最大响应时间

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值