一、背景 最近写文章需要了解和对比一些函数的性能差异,因此在网上找到了一个简单易用的 Java 性能测试框架 junitperf。 官方介绍它的优势是: 可以和 Junit5 完美契合。 -- https://mvnrepository.com/artifact/com.github.houbb/junitperf --> <dependency> <groupId>com.github.houbb </groupId> <artifactId>junitperf</artifactId> <version>2.0.3</version> </dependency> 通过依赖就可以大致了解其原理 : https://mvnrepository.com/artifact/com.github.houbb/junitperf/2.0.3 大家可以使用“先猜想后验证” 的学习方法,这样印象更深刻, ; import com.github.houbb.junitperf.core.report.impl.ConsoleReporter; import com.github.houbb.junitperf.core.report.impl.HtmlReporter
五、测试代码 <dependency> <groupId>com.github.houbb</groupId> <artifactId>junitperf</artifactId> <version>2.0.0</version> </dependency> 测试用例一: import com.github.houbb.junitperf.core.annotation.JunitPerfConfig ; import com.github.houbb.junitperf.core.report.impl.HtmlReporter; import org.junit.jupiter.api.BeforeAll ; import com.github.houbb.junitperf.core.report.impl.HtmlReporter; import org.junit.jupiter.api.BeforeAll ; import com.github.houbb.junitperf.core.report.impl.HtmlReporter; import org.junit.jupiter.api.BeforeAll
同时,带大家认识一个非常不错的性能测试工具junitperf。 测试环境 先同步一下测试环境及工具信息: JDK版本:1.8.0_151。 电脑配置:MacBook Pro i7,16G内存。 Java测试工具:junitperf及Junit。 IDE:intellij IDEA。 在测试的过程中电脑中还开了其他很多应用,但基本上都没进行操作。 在单元测试的方法上都有下面的注解: @JunitPerfConfig(duration = 10000, warmUp = 1000, reporter = {HtmlReporter.class}) 该注释为junitperf 最后推荐一下这款用起来还不错的Java性能测试工具,GitHub地址:https://github.com/houbb/junitperf。 上面有详细的使用说明。
测试中使用Mock对象的简单方法 JUnit http://www.junit.org Java的单元测试框架 TestNG http://www.testng.org 一个类似于JUnit的单元测试框架 JUnitPerf http://www.clarkware.com JUnitPerf是一套JUnit Test Decorator,用于帮助测量系统中有JUnit测试的部分的性能和伸缩性 MockObjects http
测试代码 <dependency> <groupId>com.github.houbb</groupId> <artifactId>junitperf</artifactId> <version>2.0.0</version> </dependency> junitperf 测试用例一: import com.github.houbb.junitperf.core.annotation.JunitPerfConfig ; import com.github.houbb.junitperf.core.report.impl.HtmlReporter; import org.junit.jupiter.api.BeforeAll ; import com.github.houbb.junitperf.core.report.impl.HtmlReporter; import org.junit.jupiter.api.BeforeAll ; import com.github.houbb.junitperf.core.report.impl.HtmlReporter; import org.junit.jupiter.api.BeforeAll
性能测试 为了测试性能,写了一个单元测试 package custom; import cn.hutool.core.date.SystemClock; import com.github.houbb.junitperf.core.annotation.JunitPerfConfig