[Jmeter] jmeter extract the return value in response, and saves it to a file postprocessor --BeanShell

There is a demand, we need to pressure test environment, create hundreds of thousands of account data, and then create based on the results of the query to certain account information.

Before I follow the practice of directly calling Python API, then the database query;

But recently, access to all database developers is limited, I am no exception. You can not access the database, but there is no query API, and get well.

 

Then I thought of a solution, test a little, feasible. Summary records as follows:

Get data created post API, with Jmeter achieve the following, and then extract the return value, and save it to a local file.

By the way, in fact, implemented in Python, no problem, choose which, in fact, will do.

I made some more before using Python, with Jmeter here to achieve, but also to give you a brief summary records. as follows:

 

Proceed as follows:

A. New TestPlan

 

 

. Add the following two components:

1.TestPlan

 

 

2. Increase setUp Thread Group

 

 

3. Increase HTTP HTTP header Manager

 

 

 配置:Content-Type:application/json

 

4. Increase the HTTP Request

 

 

Specific parameters POST http://haochuangtest.com/api/rest/internal/v1/en/XXXenterprise/$ {} / cloudXXXXX

POST data:
{
"id": "",
"XXXXXNumber": "",
"displayName": "HAO-TEST",
"password": "",
"XXXXXPassword": "",
"autoXXX": 2,
"XXXModel": false,
"smartXXXX": 6,
"expireTime": -1,
"adminUserId": 0,
"adminDisplayName": "null",
"permanent": "true",
"XXXXXNumberType": 4,
"XXXXXId": "null",
"userXXXXXXXDisplayName": "null",
"configs": {
"conXXXX": "2"
}
}

 

 

5.增加 Regular Expression Extractor

 

 

        <hashTree>
          <RegexExtractor guiclass="RegexExtractorGui" testclass="RegexExtractor" testname="Regular Expression Extractor" enabled="true">
            <stringProp name="RegexExtractor.useHeaders">false</stringProp>
            <stringProp name="RegexExtractor.refname">meetingNumber</stringProp>
            <stringProp name="RegexExtractor.regex">&quot;meetingNumber&quot;:&quot;(.+?)&quot;,</stringProp>
            <stringProp name="RegexExtractor.template">$1$</stringProp>
            <stringProp name="RegexExtractor.default"></stringProp>
            <stringProp name="RegexExtractor.match_number">1</stringProp>
          </RegexExtractor>
          <hashTree/>

 

6.增加 BeanShell PostProcessor

 

 

FileWriter fstream = new FileWriter("D:\\WORK_2020\\20200224_HAOCloud\\meetingId.txt",true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(vars.get("meetingNumber")+ "\n");
out.close();
fstream.close();

 

 

7.增加 View Result Tree 察看结果树

 

 

 

3.调试配置并运行;

运行过程,查看结果数的返回结果就行,此处不做截图;

 

4.检查结果:

可见文件已经成功生成 :

   

 

 

 

 

 

注意点:

1.对于返回结果的正则部分要注意,差一个双引号或者.都可能出错,导致获取的结果为null;

2.对于两个引用的组件要注意,不要选错;

3.其中主要的就是这个 BeanShell后置处理器 ,要注意使用;

 


 

 

部分参考:https://www.cnblogs.com/yrxns/p/10912816.html ,但是该文章有点问题错误,看的时候要注意辨别;

当做性能压测时,可能会需要把响应数据的一些字段统计出来。这里简单介绍一下。

1、首先把接口调通,确定需要统计的字段,这里以统计ccmpSeq字段来做例子。

 

2、添加正则表达式提取器,用来提取响应结果中的ccmpSeq

 

 

正则表达式用""包起来,如HTTP请求完成后的响应数据

{
"ccmpSeq":"CBS_queryBalance20171023131825491588",
"respTime":"20180820105000",
"respCode":"0",
"respDesc":"操作成功",
"data":{
"totalAmount":19999991000,
"accountID":"acct1470001090166"
}
}

我们只需要统计ccmpSeq,所以正则表达式为  "ccmpSeq":"(.+?)"

() 表示括起来的部分就是要提取的。

. 表示匹配任何字符串。
+ 表示一次或多次。
?表示不要太贪婪,在找到第一个匹配项后停止。
(3)模板:用$$引用起来,如果在正则表达式中有多个正则表达式,则可以是$2$,$3$等等,表示解析到的第几个值给seq。如:$1$表示解析到的第1个值,我们这里只有一个正则表达式,所以是$1$
(4)匹配数字:0代表随机取值,1代表全部取值,通常情况下填1
(5)缺省值:如果参数没有取得到值,那默认给一个值让它取,通常情况下为空
 
3、在本地新建一个123.csv文件,然后添加一个BeanShell PreProcessor,用于提取结果并将之保存到123.csv文件。

 

4、添加线程数为10,运行后,打开123.csv,查看结果。 

Guess you like

Origin www.cnblogs.com/haochuang/p/12357215.html