JMeter 是一個(gè)不錯(cuò)的負(fù)載和性能測(cè)試工具,我們也用來(lái)做 HTTP API 接口測(cè)試。我們的 API 返回結(jié)果為 JSON 數(shù)據(jù)格式。JSON 簡(jiǎn)介,JSON 教程。
JSON 已經(jīng)成為數(shù)據(jù)交換格式的事實(shí)標(biāo)準(zhǔn):
1. Ajax 調(diào)用時(shí)的返回?cái)?shù)據(jù);
2. 在移動(dòng)應(yīng)用中,HTTP 調(diào)用時(shí)的返回?cái)?shù)據(jù);
3. REST Web Service 交換格式;
4. MongoDB 等 NO-SQL 數(shù)據(jù)庫(kù)的數(shù)據(jù)存儲(chǔ)格式;
5. ...
一定要安裝 Json Path 插件?
- JSON 數(shù)據(jù)結(jié)構(gòu)簡(jiǎn)單的情況
在JSON 數(shù)據(jù)結(jié)構(gòu)簡(jiǎn)單的情況下,使用原生內(nèi)置的 Regular Expression Extractor(正則表達(dá)式)后置處理器來(lái)提取數(shù)據(jù)是可以自如應(yīng)對(duì)的,不一定非要安裝 Json Path 相關(guān)插件; - 使用正則表達(dá)式的一個(gè)優(yōu)勢(shì)在于 JMeter 安裝時(shí)就內(nèi)置了,不用額外安裝;
- 創(chuàng)建和閱讀正則表達(dá)式并不容易,隨著 JSON 數(shù)據(jù)結(jié)構(gòu)復(fù)雜度上升,正則表達(dá)式是難以理解的,其可維護(hù)性自然大打折扣;
安裝 Json Path 相關(guān)的插件就是一個(gè)較好的解決 JSON 數(shù)據(jù)結(jié)構(gòu)復(fù)雜度的方法。
了解 Json Path 語(yǔ)法
對(duì)于熟悉 XPath 語(yǔ)法的人來(lái)說(shuō)(XML 數(shù)值提取語(yǔ)法),使用 Json Path 解析 JSON 格式的響應(yīng)數(shù)據(jù)比較方便;不過(guò),即使你不熟悉 XPath 也沒(méi)關(guān)系,來(lái),馬上了解 Json Path 語(yǔ)法就可以了。
了解 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 頁(yè)面里面是原有風(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 這個(gè)插件集,目前最新 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 對(duì)應(yīng)目錄即可;
- 重啟 JMeter;
目前在使用中還沒(méi)發(fā)現(xiàn)什么問(wèn)題。
使用 Json Path Assertion 檢查返回值是否符合預(yù)期


使用 Post Processor:JSON Path Extractor 抽取變量值,供后續(xù)使用


- 從 JSON response 中提取數(shù)據(jù)(每個(gè) Extractor 只能抽取一個(gè)變量);
- 存放到 passcode 變量中為后續(xù)使用;
- 如果沒(méi)有找到,則變量 passcode 中會(huì)包含 “NO DEFAULT VALUE”(Default Value);
- 我們可以使用 ${passcode} 在后續(xù)元件中注入數(shù)據(jù);
對(duì)照:使用 Regular Expression Extractor 一次提取多個(gè)數(shù)據(jù)
在一個(gè)請(qǐng)求下加一個(gè) Regular Expression Extractor;
-
進(jìn)行配置:
Reference Name: myVar
Regular Expression: {"succ":"(.+?)","passcode":"(.+?)"}
Template: $1$$2$
1)提取兩個(gè)變量,使用 ${myVar_g1} 和 ${myVar_g2} 訪問(wèn),加個(gè) Debug Sampler 可以在 View Results Tree 查看;
2)即使使用 Regular Expression Extractor,也建議每個(gè) Extractor 只提取一個(gè)變量值,直觀有語(yǔ)義;
Regular Expression Extractor 的配置 -
通過(guò) View Results Tree 查看 Debug Sampler 的結(jié)果
通過(guò) View Results Tree 查看 Debug Sampler 的結(jié)果 -
通過(guò) View Results Tree listener 內(nèi)置的 Regex Tester 對(duì)正則表達(dá)式進(jìn)行即時(shí)測(cè)試
通過(guò) [View Results Tree] listener 內(nèi)置的 Regex Tester 對(duì)正則表達(dá)式進(jìn)行即時(shí)測(cè)試
使用 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


