• Java 性能测试框架工具-JunitPerf 快速上手


    一、背景

    最近写文章需要了解和对比一些函数的性能差异,因此在网上找到了一个简单易用的 Java 性能测试框架 junitperf

    官方介绍它的优势是:

    • 可以和 Junit5 完美契合。
    • 使用简单,便于项目开发过程中的测试实用。
    • 提供拓展,用户可进行自定义开发。

    二、范例

    2.1 依赖

    <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.6.0</version>
        <scope>test</scope>
    </dependency>
    
    <!-- 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

    图片描述

    大家可以使用本人在剖析《阿里巴巴 Java 开发手册》专栏中讲到的“先猜想后验证” 的学习方法,这样印象更深刻,学习效果更好。

    通过上图,我们可以猜测,该框架使用 freemarkder 生成 HTML 报告,使用 commons-math3 进行性能计算,使用 junit-jupiter-engine 支持 Junit 5的特性等。

    感兴趣大家可以 clone 项目深入研究。

    大家还可以通过查看单元测试的方式快速掌握用法:

    图片描述

    2.2 示例

    使用很简单,建议直接进入 JunitPerfConfig 直接中看注释即可明白每个属性的含义。

    希望大家平时学习新技术时,也可以尝试通过这种方式,快速了解用法。

    /**
     * 执行接口
     * 对于每一个测试方法的条件配置
     * @author bbhou
     * @version 1.0.0
     * @since 1.0.0
     */
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD})
    @Documented
    @API(status = API.Status.MAINTAINED, since = VersionConstant.V2_0_0)
    @ExtendWith(PerfConfigProvider.class)
    @TestTemplate
    public @interface JunitPerfConfig {
    
        /**
         * 执行时使用多少线程执行
         * @return int val
         */
        int threads() default 1;
    
        /**
         * 准备时间(单位:毫秒)
         * @return time in mills
         */
        long warmUp() default 0L;
    
        /**
         * 执行时间。(单位:毫秒)
         * 默认值:默认为 1min
         * 这里的执行时间不包含准备时间。
         * @return time in mills
         */
        long duration() default 60_000L;
    
        /**
         * 存放统计信息工具
         * @return 统计实现类
         */
        Class<? extends StatisticsCalculator> statistics() default DefaultStatisticsCalculator.class;
    
        /**
         * 存放报告信息类
         * @return 报告信息
         */
        Class<? extends Reporter>[] reporter() default {ConsoleReporter.class};
    
    }
    

    因此随手写个示例:

    import com.github.houbb.junitperf.core.annotation.JunitPerfConfig;
    import com.github.houbb.junitperf.core.report.impl.ConsoleReporter;
    import com.github.houbb.junitperf.core.report.impl.HtmlReporter;
    import lombok.extern.slf4j.Slf4j;
    
    @Slf4j
    public class PerfTest {
    
        @JunitPerfConfig(threads = 10, warmUp = 1_000, duration = 30_000
                , reporter = {HtmlReporter.class, ConsoleReporter.class})
        public void newStrTestStringBuilder() {
            StringBuilder stringBuilder = new StringBuilder("demo");
            for (int i = 0; i < 100; ++i) {
                stringBuilder.append(i);
            }
            log.info(stringBuilder.toString());
        }
    }
    

    通过 reporter 可以配置 HTML 方式和 控制台方式的报告,下图为 HTML 方式(在 target 目录的 junitperf 文件夹对应的包名下):

    图片描述

    三、总结

    该框架虽然在国内不是特别出名,但是非常简单易用,对于要求不高的朋友来说已经非常不错了。
    掌握学习的能力,快速上手一个新技术的能力非常重要,希望本文能够给你带来一些启发。

    更多详细内容参见 github 项目说明,更多高级用法可以下载源码去看下单元测试,也可以根据源码自行探索。


    作者:明明如月
    链接:https://www.imooc.com/article/302303
    来源:慕课网
    本文首次发布于慕课网 ,转载请注明出处,谢谢合作

  • 相关阅读:
    centos golang 环境配置
    运行安全审计 npm audit
    Oracle ——UTL_SMTP包发送Email
    UML学习入门就这一篇文章
    UML ——类图和对象图
    SQL SERVER 行列转换(转自别人)
    Oracle行列转换小结
    同步调用/异步调用(摘自百度)
    C#中Invoke的用法(Winform编程)
    udpclient之异步编程
  • 原文地址:https://www.cnblogs.com/zgq123456/p/12907772.html
Copyright © 2020-2023  润新知