爲 java 開發者設計的性能測試框架,用於壓測+測試報告生成

拓展閱讀

junit5 系列教程

基於 junit5 實現 junitperf 源碼分析

Auto generate mock data for java test.(便於 Java 測試自動生成對象信息)

Junit performance rely on junit5 and jdk8+.(java 性能測試框架。壓測+測試報告生成。)

junitperf

junitperf 是一款爲 java 開發者設計的性能測試框架。

爲什麼使用?

  • 可以和 Junit5 完美契合。

  • 使用簡單,便於項目開發過程中的測試實用。

  • 提供拓展,用戶可進行自定義開發。

特性

  • 支持 I18N

  • 支持多種報告生成方式,支持自定義

  • Junt5 完美支持,便於 Java 開發者使用

快速開始

項目依賴

  • jdk1.8 及其以上版本

  • Junit5 及其以上版本

maven 導入

<dependency>
    <groupId>com.github.houbb</groupId>
    <artifactId>junitperf</artifactId>
    <version>2.0.7</version>
</dependency>

入門案例

入門案例地址

  • 使用例子
public class HelloWorldTest {

    @JunitPerfConfig(duration = 1000)
    public void helloTest() throws InterruptedException {
        Thread.sleep(100);
        System.out.println("Hello Junit5");
    }

}

配置說明

測試註解指定

@JunitPerfConfig

指定測試時的屬性配置。(必填項)

屬性 說明 類型 默認值 備註
threads 執行時使用多少線程執行 int 1
warmUp 準備時間 long 0 單位:毫秒
duration 執行時間 long 60_000(1分鐘) 單位:毫秒
latencyStatistics 統計實現 StatisticsCalculator DefaultStatisticsCalculator
reporter 報告實現 Reporter ConsoleReporter

使用如下:

public class JunitPerfConfigTest {

    /**
     * 2個線程運行。
     * 準備時間:1000ms
     * 運行時間: 2000ms
     * @throws InterruptedException if any
     */
    @JunitPerfConfig(threads = 2, warmUp = 1000, duration = 2000)
    public void junitPerfConfigTest() throws InterruptedException {
        System.out.println("junitPerfConfigTest");
        Thread.sleep(200);
    }

}

各種報告的實現

這裏主要是對於性能測試統計的輸出方式
支持以下方式:

方式 案例
默認方式 DefaultReporterTest
命令行 ConsoleReporterTest
HTML HtmlReporterTest
組合方式 MultiReporterTest
自定義方式 DefineReporterTest

@JunitPerfRequire

指定測試時需要達到的要求。(選填項)

屬性 說明 類型 默認值 備註
min 最佳的運行耗時 float -1 最快的運行耗時如果高於這個值,則視爲失敗。單位:毫秒
max 平均的運行耗時 float -1 最壞的運行耗時如果高於這個值,則視爲失敗。單位:毫秒
average 平均的運行耗時 float -1 平均的運行耗時如果高於這個值,則視爲失敗。單位:毫秒
timesPerSecond 每秒的最小執行次數 int 0 如果低於這個最小執行次數,則視爲失敗。
percentiles 對於執行耗時的限定 String[] {} percentiles={"20:220", "30:250"}。20% 的數據執行耗時不得超過 220ms;30% 的數據執行耗時不得超過 250ms;

使用如下:

public class JunitPerfRequireTest {
    /**
     * 配置:2個線程運行。準備時間:1000ms。運行時間: 2000ms。
     * 要求:最快不可低於 210ms, 最慢不得低於 250ms, 平均不得低於 225ms, 每秒運行次數不得低於 4 次。
     * 20% 的數據不低於 220ms, 50% 的數據不得低於 230ms;
     *
     * @throws InterruptedException if any
     */
    @JunitPerfConfig(threads = 2, warmUp = 1000, duration = 2000)
    @JunitPerfRequire(min = 210, max = 250, average = 225, timesPerSecond = 4, percentiles = {"20:220", "50:230"})
    public void junitPerfConfigTest() throws InterruptedException {
        System.out.println("junitPerfConfigTest");
        Thread.sleep(200);
    }

}

報告方式

命令行方式

大致如下:

[INFO] [2020-06-16 20:05:53.618] [c.g.h.j.e.HelloWorldTest.helloTest] - Started at:  2020-06-16 20:05:52.512
[INFO] [2020-06-16 20:05:53.619] [c.g.h.j.e.HelloWorldTest.helloTest] - Invocations:  9
[INFO] [2020-06-16 20:05:53.620] [c.g.h.j.e.HelloWorldTest.helloTest] - Success:  9
[INFO] [2020-06-16 20:05:53.620] [c.g.h.j.e.HelloWorldTest.helloTest] - Errors:  0
[INFO] [2020-06-16 20:05:53.621] [c.g.h.j.e.HelloWorldTest.helloTest] - Thread Count:  1
[INFO] [2020-06-16 20:05:53.623] [c.g.h.j.e.HelloWorldTest.helloTest] - Warm up:  0ms
[INFO] [2020-06-16 20:05:53.623] [c.g.h.j.e.HelloWorldTest.helloTest] - Execution time:  1000ms
[INFO] [2020-06-16 20:05:53.624] [c.g.h.j.e.HelloWorldTest.helloTest] - Throughput:  9/s (Required: -1/s) - PASSED
[INFO] [2020-06-16 20:05:53.625] [c.g.h.j.e.HelloWorldTest.helloTest] - Memory cost:  16byte
[INFO] [2020-06-16 20:05:53.635] [c.g.h.j.e.HelloWorldTest.helloTest] - Min latency:  100.191414ms (Required: -1.0ms) - PASSED
[INFO] [2020-06-16 20:05:53.635] [c.g.h.j.e.HelloWorldTest.helloTest] - Max latency:  105.2382ms (Required: -1.0ms) - PASSED
[INFO] [2020-06-16 20:05:53.636] [c.g.h.j.e.HelloWorldTest.helloTest] - Avg latency:  101.43268ms (Required: -1.0ms) - PASSED

HTML 方式

頁面如下:

後期會進行樣式調整。

HTML 形式

指定方法執行順序

說明

方法的執行順序會影響到最終的報告顯示順序。

如果你想嚴格指定同一個類方法的執行順序,推薦使用 Test Execution Order 的方式。

@TestMethodOrder 需要 junit5 在 5.4 及其以後版本

示例代碼

參考 OrderedHtmlTest

對於 junit4 的支持

引入 jar

<dependency>
    <groupId>com.github.houbb</groupId>
    <artifactId>junitperf</artifactId>
    <version>1.0.3</version>
</dependency>

相關文檔

junit4 相關使用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章