ContiPerf 是一個輕量級的測試工具,基于JUnit 4 開發(fā)囱桨,可用于性能測試等仓犬。可以指定在線程數量和執(zhí)行次數舍肠,通過限制最大時間和平均執(zhí)行時間來進行性能測試搀继。
使用步驟:
1. maven 引入
org.databene
contiperf
2.1.0
test
2. 單元測試類里面使用 @Rule 注解激活 ContiPerf
@Rule
public ContiPerfRule i = new ContiPerfRule();
3. 在具體測試方法上使用 @PerfTest 指定調用次數/線程數窘面,使用 @Required 指定每次執(zhí)行的最長時間/平均時間/總時間等
@Test
@PerfTest(invocations = 30000, threads = 20)
@Required(max = 1200, average = 250, totalTime = 60000)
public void test1() throws Exception {
snowflakeSequence.nextId();
}
注:
1)PerfTest參數
@PerfTest(invocations = 300):執(zhí)行300次,和線程數量無關律歼,默認值為1民镜,表示執(zhí)行1次;
@PerfTest(threads=30):并發(fā)執(zhí)行30個線程险毁,默認值為1個線程制圈;
@PerfTest(duration = 20000):重復地執(zhí)行測試至少執(zhí)行20s。
2)Required參數
@Required(throughput = 20):要求每秒至少執(zhí)行20個測試畔况;
@Required(average = 50):要求平均執(zhí)行時間不超過50ms鲸鹦;
@Required(median = 45):要求所有執(zhí)行的50%不超過45ms;
@Required(max = 2000):要求沒有測試超過2s跷跪;
@Required(totalTime = 5000):要求總的執(zhí)行時間不超過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。
3)@PerfTest @Required 可以加在類上面橡羞,表示所有方法的默認配置
4. 在測完之后眯停,在本地的target下生成contiperf-report目錄,里面有個index.html文件卿泽,即為測試結果莺债。