1、前言
在軟件開發的生命週期中,API
文檔的自動生成以及接口的性能測試是提升開發效率和確保產品質量的關鍵步驟。隨着smart-doc
工具在3.0.1
版本後新增了生成JMeter
性能壓測腳本的能力,開發者可以更加便捷地完成這兩項任務。本文將介紹如何利用smart-doc
和JMeter
進行有效的性能壓測。
2、Smart-doc生成JMeter
生成 JMeter 腳本
使用 Smart-doc
生成 JMeter
性能壓測腳本,可以大大減少性能測試腳本的編寫時間,提升測試效率。Smart-doc
自動生成的 JMeter
腳本,可以直接在 JMeter
中運行,無需經過複雜的配置和調試,讓性能測試變得更加簡單快捷。
首先,確保你的項目中已經包含了smart-doc
插件。然後,在項目的pom.xml
文件中配置smart-doc
插件的相關參數,例如:
<plugin>
<groupId>com.ly.smart-doc</groupId>
<artifactId>smart-doc-maven-plugin</artifactId>
<version>3.0.3</version>
<configuration>
<configFile>./src/main/resources/smart-doc.json</configFile>
<projectName>${project.description}</projectName>
</configuration>
<executions>
<execution>
<!--如果不需要在執行編譯時啓動smart-doc,則將phase註釋掉-->
<phase>compile</phase>
<goals>
<!--smart-doc提供了html、openapi、markdown等goal,可按需配置-->
<goal>jmeter</goal>
</goals>
</execution>
</executions>
</plugin>
運行mvn -Dfile.encoding=UTF-8 smart-doc:jmeter
命令,smart-doc
將掃描項目源代碼,提取註釋信息,並自動創建相應的JMeter
性能壓測腳本。
不熟悉使用可以查閱smart-doc
官方文檔https://smart-doc-group.github.io/#/zh-cn/integrated/jmeter
導入JMeter 腳本
打開 JMeter
,點擊 "File" -> "Open",選擇第一步生成的 JMeter
腳本文件,點擊 "Start" 按鈕,JMeter
就會開始按照腳本進行性能壓測。
配置prometheus監控
Prometheus
是一款開源的監控和警告工具,用於處理時間序列數據。通過結合這兩款工具,我們可以實現對 JMeter
測試過程的實時監控,從而提高性能測試的可觀測性。
以下是實現步驟:
- 安裝 JMeter Prometheus 插件
首先,你需要在 JMeter
的 lib/ext
目錄下安裝 Prometheus
插件。你可以從 JMeter Plugins Manager
或者官方網站 https://jmeter-plugins.org/ 下載插件。也可以從Github
下載,https://github.com/johrstrom/jmeter-prometheus-plugin/releases 。本次選擇從Github下載最新的0.7.1
注意: JMeter
默認監聽的IP地址是127.0.0.1
,默認會導致Prometheus
無法連接到 JMeter Prometheus
的監聽端口,因此需要注意在jmeter.properties
里加個配置prometheus.ip=0.0.0.0
- 在 JMeter 中配置 Prometheus 插件
打開 JMeter
,添加Prometheus Listener
到你的測試計劃中。這可以通過右鍵點擊"測試計劃" -> "添加" -> "監聽器"-> Prometheus Listener
來完成。
監聽器的配置可以參考官方的配置,參考如下(smart-doc
支持配置添加生成Prometheus Listener
):
- 配置 Prometheus
在 Prometheus
的配置文件(prometheus.yml
)中,添加一個新的 scrape_config
來從 JMeter Prometheus
插件中抓取數據。例如:
scrape_configs:
- job_name: 'jmeter'
static_configs:
- targets: ['<Your JMeter machine IP>:9270']
其中,<Your JMeter machine IP> 是運行 JMeter
測試的機器的 IP
地址,9270
是 JMeter Prometheus
插件的默認監聽端口。
- 啓動JMeter
本文中爲了方便驗證,壓測時線程組設置爲“無限循環” ,可根據實際需求去調整。
啓動成功後, JMeter Prometheus
會默認在本地創建一個端口爲9270
的服務。 訪問網址:http://localhost:9270/metrics 看到如下內容就說明成功了
- 啓動 Prometheus
啓動 Prometheus
,Prometheus
將開始從 JMeter Prometheus
插件中抓取數據。如下圖配置和啓動成功後能在Prometheus
中看到設置的target
- 配置Grafana
到Grafana
官網,查找官網提供的prometheus-jmeter
監控面板。這裏選用ID
爲14927
的模版導入到Grafana
中。
點擊Load之後選擇Promethues
數據源。
注意: 測試中發現14927
原始模版存在一些錯誤,本文編寫過程中做了修復,需要的同學可以通過下面鏈接下載修復後的模版導入: https://github.com/smart-doc-group/smart-doc-demo/blob/master/jmeter/grafana-template/jmeter-prometheus-14972.json
模版導入成功後我們就可以在Grafana
中看到整個性能壓測的監控數據了。
爲了大家可以快速的感受整個的壓測流程,smart-doc
社區也整理提供一個採用docker-compose
一鍵啓動體驗的模版。如果想通過k8s
部署來體驗,也可以藉助一些國內大模型AI工具直接把docker-compose
的模版轉化成k8s
部署模版。
體驗項目中也包含使用說明
相關代碼地址:https://github.com/smart-doc-group/smart-doc-demo
3、Smart-doc在性能壓測實踐中的優點
使用 Smart-doc
與 JMeter
的組合進行性能壓測,有以下幾個優點:
自動化: Smart-doc
可以自動從源碼中提取 API
信息,自動生成 JMeter
性能壓測腳本,無需手動編寫,大大提高了效率。
精確: Smart-doc
自動生成的JMeter
性能壓測腳本,與源碼中的 API
定義完全保持一致,避免了手動編寫腳本時可能出現的錯誤。
靈活: Smart-doc
支持多種配置選項,可以根據測試需求定製生成的 JMeter
腳本。
後續smart-doc也會持續完善對JMeter的支持優化。請持續關注smart-doc開源項目: https://github.com/TongchengOpenSource/smart-doc
4、總結
通過結合smart-doc
和JMeter
,我們不僅可以自動化生成API
文檔,還能快速創建性能測試腳本並進行壓測。這種集成的工作流程顯著提高了開發和測試效率,同時幫助團隊更容易地維護和優化軟件系統的性能。希望本文爲你提供了實用的參考,以便在日常工作中更加高效地應用這兩個強大的工具。 我們也歡迎大家持續關注和支持smart-doc
開源社區,未來我們也在探索更多語言的支持,爲更多開發者賦能提效。