ContiPerf:: 更爲優雅和方便的單元壓力測試工具。

概述

ContiPerf 是一個輕量級的單元測試工具,基於JUnit 4二次開發,使用它基於註解的方式,快速在本地進行單元壓測並提供詳細的報告。html

Example

1. 新建 SpringBoot 工程

核心依賴以下java

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.databene</groupId>
    <artifactId>contiperf</artifactId>
    <version>2.1.0</version>
    <scope>test</scope>
</dependency>
複製代碼

2. 測試接口以及實現

package com.wuwenze.contiperf.service;

import java.util.List;

public interface ContiperfExampleService {

    List<String> findAll();
}
複製代碼
import com.wuwenze.contiperf.service.ContiperfExampleService;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class ContiperfExampleServiceImpl implements ContiperfExampleService {
    private final Random RANDOM = new Random();

    @Override
    public List<String> findAll() {
        try {
            int sleepSecond = RANDOM.nextInt(10);
            log.info("#findAll(): sleep {} seconds..", sleepSecond);
            Thread.sleep(sleepSecond * 1000);
        } catch (InterruptedException e) {
            // ignore
        }
        List<String> resultList = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            resultList.add("string_" + i);
        }
        return resultList;
    }
}
複製代碼

3. 構建單元測試

package com.wuwenze.contiperf.service;

import com.wuwenze.contiperf.ContiperfExamplesApplication;

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(classes = ContiperfExamplesApplication.class)
public class ContiperfExampleServiceTest {
    @Rule
    public ContiPerfRule i = new ContiPerfRule();

    @Autowired
    private ContiperfExampleService contiperfExampleService;

    @Test
    @PerfTest(threads = 1000, duration = 1500)
    public void findAll() {
        contiperfExampleService
                .findAll()
                .forEach(System.out::println);
    }
}
複製代碼

4. 最終執行效果

image.png

查看測試報告: spring

image.png

總結

1)PerfTest參數

@PerfTest(invocations = 300):執行300次,和線程數量無關,默認值爲1,表示執行1次; @PerfTest(threads=30):併發執行30個線程,默認值爲1個線程; @PerfTest(duration = 20000):重複地執行測試至少執行20s。 三個屬性能夠組合使用,其中Threads必須和其餘兩個屬性組合才能生效。當Invocations和Duration都有指定時,以執行次數多的爲準。瀏覽器

  例,@PerfTest(invocations = 300, threads = 2, duration = 100),若是執行方法300次的時候執行時間還沒到100ms,則繼續執行到知足執行時間等於100ms,若是執行到50次的時候已經100ms了,則會繼續執行之100次。多線程

  若是你不想讓測試連續不間斷的跑完,能夠經過註釋設置等待時間,例,@PerfTest(invocations = 1000, threads = 10, timer = RandomTimer.class, timerParams = { 30, 80 }) ,每執行完一次會等待30~80ms而後纔會執行下一次調用。併發

  在開多線程進行併發壓測的時候,若是一會兒達到最大進程數有些系統可能會受不了,ContiPerf還提供了「預熱」功能,例,@PerfTest(threads = 10, duration = 60000, rampUp = 1000) ,啓動時會先起一個線程,而後每一個1000ms起一線程,到9000ms時10個線程同時執行,那麼這個測試實際執行了69s,若是隻想衡量全力壓測的結果,那麼能夠在註釋中加入warmUp,即@PerfTest(threads = 10, duration = 60000, rampUp = 1000, warmUp = 9000) ,那麼統計結果的時候會去掉預熱的9s。dom

2)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。ide

3)測試報告

最終的測試報告位於target/contiperf-report/index.html,使用瀏覽器打開便可。spring-boot

相關文章
相關標籤/搜索