JMeter 是一個不錯的負(fù)載和性能測試工具,我們也用來做 HTTP API 接口測試。我們的 API 返回結(jié)果為 JSON 數(shù)據(jù)格式。JSON 簡介,JSON 教程。
JSON 已經(jīng)成為數(shù)據(jù)交換格式的事實(shí)標(biāo)準(zhǔn):
1. Ajax 調(diào)用時的返回?cái)?shù)據(jù);
2. 在移動應(yīng)用中,HTTP 調(diào)用時的返回?cái)?shù)據(jù);
3. REST Web Service 交換格式;
4. MongoDB 等 NO-SQL 數(shù)據(jù)庫的數(shù)據(jù)存儲格式;
5. ...
一定要安裝 Json Path 插件?
- JSON 數(shù)據(jù)結(jié)構(gòu)簡單的情況
在JSON 數(shù)據(jù)結(jié)構(gòu)簡單的情況下,使用原生內(nèi)置的 Regular Expression Extractor(正則表達(dá)式)后置處理器來提取數(shù)據(jù)是可以自如應(yīng)對的,不一定非要安裝 Json Path 相關(guān)插件; - 使用正則表達(dá)式的一個優(yōu)勢在于 JMeter 安裝時就內(nèi)置了,不用額外安裝;
- 創(chuàng)建和閱讀正則表達(dá)式并不容易,隨著 JSON 數(shù)據(jù)結(jié)構(gòu)復(fù)雜度上升,正則表達(dá)式是難以理解的,其可維護(hù)性自然大打折扣;
安裝 Json Path 相關(guān)的插件就是一個較好的解決 JSON 數(shù)據(jù)結(jié)構(gòu)復(fù)雜度的方法。
了解 Json Path 語法
對于熟悉 XPath 語法的人來說(XML 數(shù)值提取語法),使用 Json Path 解析 JSON 格式的響應(yīng)數(shù)據(jù)比較方便;不過,即使你不熟悉 XPath 也沒關(guān)系,來,馬上了解 Json Path 語法就可以了。
了解 jmeter-plugins.org
This project is an independent set of plugins for Apache JMeter, the popular Open-Source load and performance testing tool.
為 JMeter 定制插件,提供獨(dú)立的插件集。完整的插件集列表 有六大類:
1. Standard Set
2. Extras Set
3. Extras with Libs Set
4. WebDriver Set
5. XMPP Set
6. Hadoop Set
本次我們要安裝的 Json Path 就包含在 Extras with Libs Set 里面。
注:從官網(wǎng)看似乎推薦使用 JMeter Plugins Manager(目前 0.11 版),Old-Style Releases 頁面里面是原有風(fēng)格的下載包;
安裝 Extras With Libs Set
Installation and Usage
Just copy the JAR file into JMeter's lib/ext directory.
Then you can start JMeter and add additional items to your Test Plan.
Java version 1.6 and JMeter 2.4 are required.
Extras With Libs Set 這個插件集,目前最新 JMeterPlugins-ExtrasLibs-1.4.0.zip。
- JMeterPlugins-ExtrasLibs-1.4.0.zip 包
│ LICENSE
│ README
│
└─lib
│ asm-5.0.3.jar
│ commons-beanutils-1.8.3.jar
│ commons-lang-2.6.jar
│ commons-pool-1.6.jar
│ ezmorph-1.0.6.jar
│ jedis-2.2.1.jar
│ json-lib-2.4-jdk15.jar
│ json-path-2.1.0.jar
│ json-smart-2.2.jar
│ qpid-client-0.20.jar
│ qpid-common-0.20.jar
│ spring-core-2.5.6.jar
│ spring-jms-2.5.6.jar
│ xom-1.2.10.jar
│
└─ext
JMeterPlugins-ExtrasLibs.jar
- JMeter 基本目錄
├─bin
├─docs
├─extras
├─lib
│ ├─ext
│ └─junit
├─licenses
└─printable_docs
- 將 Extras with Libs Set 解壓到 JMeter 對應(yīng)目錄即可;
- 重啟 JMeter;
目前在使用中還沒發(fā)現(xiàn)什么問題。
使用 Json Path Assertion 檢查返回值是否符合預(yù)期
使用 Post Processor:JSON Path Extractor 抽取變量值,供后續(xù)使用
- 從 JSON response 中提取數(shù)據(jù)(每個 Extractor 只能抽取一個變量);
- 存放到 passcode 變量中為后續(xù)使用;
- 如果沒有找到,則變量 passcode 中會包含 “NO DEFAULT VALUE”(Default Value);
- 我們可以使用 ${passcode} 在后續(xù)元件中注入數(shù)據(jù);
對照:使用 Regular Expression Extractor 一次提取多個數(shù)據(jù)
在一個請求下加一個 Regular Expression Extractor;
-
進(jìn)行配置:
Reference Name: myVar
Regular Expression: {"succ":"(.+?)","passcode":"(.+?)"}
Template: $1$$2$
1)提取兩個變量,使用 ${myVar_g1} 和 ${myVar_g2} 訪問,加個 Debug Sampler 可以在 View Results Tree 查看;
2)即使使用 Regular Expression Extractor,也建議每個 Extractor 只提取一個變量值,直觀有語義;
Regular Expression Extractor 的配置 -
通過 View Results Tree 查看 Debug Sampler 的結(jié)果
通過 View Results Tree 查看 Debug Sampler 的結(jié)果 -
通過 View Results Tree listener 內(nèi)置的 Regex Tester 對正則表達(dá)式進(jìn)行即時測試
通過 [View Results Tree] listener 內(nèi)置的 Regex Tester 對正則表達(dá)式進(jìn)行即時測試
使用 Listener:Assertion Results 以觀察 Assertion 結(jié)果
備注
- jmeter-plugins.org 插件集下載;
- Jayway JsonPath Syntax in GitHub
- goessner: JSONPath expressions
- Parsing JSON responses with JMeter
- JSON path extractor for multiple values