jmeter内对response值的处理

本文涉及的产品
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
全局流量管理 GTM,标准版 1个月
云解析 DNS,旗舰版 1个月
简介: jmeter内对response值的处理1. 正则表达式提取器在取样器上右键,选择正则表达式提取器,如图:image接下来看一下正则表达式提取器的界面:image在这里对页面各字段进行一下说明,从上往下:1. Apply to,是指你提取的变量要应用的范围,需要注意的是,Jmeter Variable是指应用到全局,也就是跨线程的。

jmeter内对response值的处理

1. 正则表达式提取器

在取样器上右键,选择正则表达式提取器,如图:

img_ddfe20d2fb9b7019f2b512aef0869a4f.jpe
image

接下来看一下正则表达式提取器的界面:

img_92e0c56e75dc42bc2f3b5f2e656a3533.jpe
image

在这里对页面各字段进行一下说明,从上往下:

1. Apply to,是指你提取的变量要应用的范围,需要注意的是,Jmeter Variable是指应用到全局,也就是跨线程的。

2. 要检查的响应字段,是指你的正则表达式的提取范围,主体应该是整个response,单选body就是指在body里找,其它选项根据字面意思,就不再赘述。

3. 引用名称,是我们自定义的一个变量名称,也就是提取出来的值的变量名称,例如我给它定义为“var”,那么正则提取出来的值就存在var里,之后如果要用,就使用el表达式,也就是${var(刚刚定义的名称)}的方式来使用。

4. 正则表达式,这里写提取变量用的正则表达式

5. 模板,这里是指你提取多少个变量(如果在正则表达式中有多个提取表达式),一般我们提取一个就够了,模板采用$N$的方式,N代表数量,例如,我们设立一个只提取1个值的模板,就写作$1$。

6. 匹配数字,也就是提取匹配到的第几个值,例如写1就是从匹配到的值里选取第一个。

7. 缺省值,也就是如果提取不到,那么默认给一个值替换提取结果。

在这里举一个例子如下:

现在有一段response值,

{"errorCode":"","success":true,"message":"","data":{"scoreId":"98","templateId":"","title":"新建权重评分表-auto-test","type":"1","state":"2","containers":[{"id":"692","text":"维度1","components":[{"id":"2196","text":"第一个指标","percent":"25","type":"1","options":[{"id":"6978","text":"好","score":"100","sort_num":1},{"id":"6979","text":"较好","score":"80","sort_num":2},{"id":"6980","text":"一般","score":"60","sort_num":3},{"id":"6981","text":"较差","score":"40","sort_num":4},{"id":"6982","text":"差","score":"0","sort_num":5}]},{"id":"2197","text":"第二个指标","percent":"25","type":"2","options":[{"id":"6983","text":"评分标准1","score":"100","sort_num":1}]}]},{"id":"693","text":"维度2","components":[{"id":"2198","text":"第三个指标","percent":"25","type":"2","options":[{"id":"6984","text":"评分标准2","score":"100","sort_num":1}]},{"id":"2199","text":"第四个指标","percent":"25","type":"1","options":[{"id":"6985","text":"好","score":"100","sort_num":1},{"id":"6986","text":"较好","score":"80","sort_num":2},{"id":"6987","text":"一般","score":"60","sort_num":3},{"id":"6988","text":"较差","score":"40","sort_num":4},{"id":"6989","text":"差","score":"0","sort_num":5}]}]}]}}

我们需要从中把维度1对应的id提取出来,那么可以按照如下参数(."id":"(.+?)","text":"维度1"*)提取:

img_1661f7871c46d0bb16ed335df632ab97.jpe
image

附上正则表达式的图:

img_052a2cb5dd42669cbb6e03b51bb8a143.png
image

2. 使用beanshell提取

在取样器上右键,选择beanshell postprocessor,如图:

img_4277b39b3f6dfd8d96bf1fce86b30426.jpe
image

接下来看一下beanshell的界面:

img_78b3a134bb372f86454688e952823e89.jpe
image

beanshell就是一个运行Java的微环境(大概可以这么理解),在Script框里我们写java代码,jmeter执行到这里的时候就会执行里面的java代码,beanshell里面会有一些jmeter内置的变量,用于jmeter和你写的Java代码交互,常用的大约有这么几个:

1. log:写入信息到jmeber.log文件,使用方法:log. info(“This is log info!”);

2. vars - (JMeterVariables):操作jmeter变量,这个变量实际引用了JMeter线程中的局部变量容器(本质上是Map),它是测试用例与BeanShell交互的桥梁,常用方法:

  • vars.get(String key):从jmeter中获得变量值
  • vars.put(String key,String value):数据存到jmeter变量中

3. prev - (SampleResult):获取前面的sample返回的信息,常用方法:

  • getResponseDataAsString():获取响应信息
  • getResponseCode() :获取响应code

在这里根据我在工作里遇到的一个实战来说明

首先说明下需求,现在存在一个接口是post类型,http协议的,他需要若干个参数用以传参,其中一个参数“scoreData”是json类型的数据,数据结构如下:

{
"645(题目的id)":{"id":"2546(选项的id)","percent":"25(分值,固定值)"},
"646(同上)":{"id":"2551(同上)","percent":"24.75(同上)","value(该参数只有在题目的type=2的时候才会出现)":"99(分值,固定值)"}
}

说明一下这个数据结构,我们的问卷可能会存在多个题目,上面case里的645,646就代表是两道题目,题目的类型分为选择题和填分题,选择题的type是1,我们只取第一个选项的id(也就是说传参代表勾选了第一项);填分题的type是2,我们这里固定填99分,也就是value是99;percent这个字段是根据权重计算出来的值,这里不多做说明,就当做它的值也是一个固定的值。

数据全部来源于前一个接口的返回值,返回值如下:

{"success":true,"message":"","errorCode":"","data":[{"submit_state":"0","score_id":"f5","travel_id":"5","travel_title":"auto-test-zyj1502356486388","inspect_time":"2033-08-23","category_codes":"2266","suggest_content":"","my_score":"0","categoryName":"商业包装设计与施工","scoreData":{"fbScoreId":"f5","templateId":"","title":"新建权重评分表-auto-test-zyj","type":1,"containers":[{"id":"8","text":"维度1","components":[{"id":"14","text":"第一个指标","percent":"25","type":"1","options":[{"id":"38","text":"好","score":"100","sort_num":1},{"id":"39","text":"较好","score":"80","sort_num":2},{"id":"40","text":"一般","score":"60","sort_num":3},{"id":"41","text":"较差","score":"40","sort_num":4},{"id":"42","text":"差","score":"0","sort_num":5}],"select":""},{"id":"15","text":"第二个指标","percent":"25","type":"2","options":[{"id":"43","text":"评分标准1","score":"100","sort_num":1}],"select":""}]},{"id":"9","text":"维度2","components":[{"id":"16","text":"第三个指标","percent":"25","type":"2","options":[{"id":"44","text":"评分标准2","score":"100","sort_num":1}],"select":""},{"id":"17","text":"第四个指标","percent":"25","type":"1","options":[{"id":"45","text":"好","score":"100","sort_num":1},{"id":"46","text":"较好","score":"80","sort_num":2},{"id":"47","text":"一般","score":"60","sort_num":3},{"id":"48","text":"较差","score":"40","sort_num":4},{"id":"49","text":"差","score":"0","sort_num":5}],"select":""}]}]},"travelDetail":{"travel_id":"5","score_id":"f5","travel_title":"auto-test-zyj1502356486388","company_name":"上海东方雨虹防水技术有限责任公司","supplier_type":"2","tax_qualify":"2","organizer_id":"195104","inspect_time":"2033-08-23","scoreName":"新建权重评分表-auto-test-zyj","supplierTypeName":"代理商","taxQualifyName":"小规模纳税人","organizerName":"125","Teams":"zyj账号01、zyj账号02","Locations":[{"name":"办公室","remark":"备注1"},{"name":"地点一","remark":"备注2"}],"QualifyFiles":[{"name":"营业执照","remark":"备注3"},{"name":"地点二","remark":"备注4"}]},"travelPhoto":[],"photoDescription":{"1":[{"address":"地点一"},{"address":"办公室"}],"2":[{"address":"地点二"},{"address":"营业执照"}]},"photo_count":0,"score_item_total_count":"4"},{"submit_state":"0","score_id":"f4","travel_id":"4","travel_title":"auto-test-zyj1502351094326","inspect_time":"2033-08-23","category_codes":"2266","suggest_content":"","my_score":"0","categoryName":"商业包装设计与施工","scoreData":{"fbScoreId":"f4","templateId":"","title":"新建权重评分表-auto-test-zyj","type":1,"containers":[{"id":"6","text":"维度1","components":[{"id":"10","text":"第一个指标","percent":"25","type":"1","options":[{"id":"26","text":"好","score":"100","sort_num":1},{"id":"27","text":"较好","score":"80","sort_num":2},{"id":"28","text":"一般","score":"60","sort_num":3},{"id":"29","text":"较差","score":"40","sort_num":4},{"id":"30","text":"差","score":"0","sort_num":5}],"select":""},{"id":"11","text":"第二个指标","percent":"25","type":"2","options":[{"id":"31","text":"评分标准1","score":"100","sort_num":1}],"select":""}]},{"id":"7","text":"维度2","components":[{"id":"12","text":"第三个指标","percent":"25","type":"2","options":[{"id":"32","text":"评分标准2","score":"100","sort_num":1}],"select":""},{"id":"13","text":"第四个指标","percent":"25","type":"1","options":[{"id":"33","text":"好","score":"100","sort_num":1},{"id":"34","text":"较好","score":"80","sort_num":2},{"id":"35","text":"一般","score":"60","sort_num":3},{"id":"36","text":"较差","score":"40","sort_num":4},{"id":"37","text":"差","score":"0","sort_num":5}],"select":""}]}]},"travelDetail":{"travel_id":"4","score_id":"f4","travel_title":"auto-test-zyj1502351094326","company_name":"上海东方雨虹防水技术有限责任公司","supplier_type":"2","tax_qualify":"2","organizer_id":"195104","inspect_time":"2033-08-23","scoreName":"新建权重评分表-auto-test-zyj","supplierTypeName":"代理商","taxQualifyName":"小规模纳税人","organizerName":"125","Teams":"zyj账号01、zyj账号02","Locations":[{"name":"办公室","remark":"备注1"},{"name":"地点一","remark":"备注2"}],"QualifyFiles":[{"name":"营业执照","remark":"备注3"},{"name":"地点二","remark":"备注4"}]},"travelPhoto":[],"photoDescription":{"1":[{"address":"地点一"},{"address":"办公室"}],"2":[{"address":"地点二"},{"address":"营业执照"}]},"photo_count":0,"score_item_total_count":"4"}]}

要提取数据,尤其是这么大的json数据,首先我们要把这个json的数据结构分析清楚,我推荐大家把这个json放到json解析网站里解析一下结构,这样会更清楚,例如bejson,解析出来如下(部分要点截图):

img_3c7ff3b103debb54e45dd2caf3d16cab.jpe
image

我们现在需要做两件事:

  1. 取值,具体是取每个components下面id的值(也就是题目的id),以及每个options下面第一个id的值(也就是选项的id);
  2. 将取到的值组合成需要的数据结构再输出。

对于json的处理,我们需要用到额外的包,json的官网里有,在java选项下面,第一个JSON-java就是,这个包叫做org.json,我们下载下来之后,把这个包放到jmeter的apache-jmeter\lib\ext下面,这样就可以用了。

简单介绍下org.json,这部分建议看一下教程

JSONObject

是一个无序的键/值对集合。

  • 它的表现形式是一个包裹在花括号的字符串,键和值之间使用冒号隔开,键值和键值之间使用逗号隔开。
  • 内在形式是一个使用get()和opt()方法通过键来访问值,和使用put()方法通过键来添加或者替代值的对象。
  • 值可以是任何这些类型:Boolean,JSONArray,JSONObject,Number和String,或者JOSONObject.NULL对象。

JSONArray

是一个有序的序列值。

  • 它的表现形式是一个包裹在方括号的字符串,值和值之间使用逗号隔开。
  • 内在形式是一个使用get()和opt()方法通过索引来访问值,和使用put()方法来添加或修改值的对象。
  • 值可以是任何这些类型:Boolean,JSONArray,JSONObject,Number,和String,或者JSONObject.NULL对象。

我们大约只用到这两种就可以了

这里直接放出处理的源码:

import java.util.HashMap;
import java.util.Map;

import org.json.*;
    
    System.out.println("test................................................................"); 
    //提取返回值作为变量response_data
    String response_data = prev.getResponseDataAsString();
    //解析json
    JSONObject data_obj = new JSONObject(response_data);        
    JSONArray jsonarray = data_obj.getJSONArray("data");
    JSONObject scoreData = null;
    int id = Integer.parseInt(vars.get("travel_id"));
    System.out.println("id="+id);   
    for (int i = 0; i < jsonarray.length(); i++) { 
        JSONObject jsonobj = jsonarray.getJSONObject(i); 
       System.out.println(jsonarray.optString(i)); 
       System.out.println(jsonobj.getInt("travel_id")); 
       System.out.println(jsonobj.getString("travel_title")); 
       int travle_id = jsonobj.getInt("travel_id");
       if(travle_id==id){   
           scoreData = jsonobj.getJSONObject("scoreData");
            }           
        }   
    JSONArray containers = scoreData.getJSONArray("containers");
    Map data = new HashMap();   
    for (int i = 0; i < containers.length(); i++){
        
        JSONArray component_ary = containers.getJSONObject(i).getJSONArray("components");
         
            for (int j = 0; j < component_ary.length(); j++){
                System.out.println(component_ary.optString(j)); 
             System.out.println(component_ary.getJSONObject(j).get("id")); 
             String data_id = component_ary.getJSONObject(j).get("id").toString();
             JSONArray options = component_ary.getJSONObject(j).getJSONArray("options");    
             String score_id = options.getJSONObject(0).get("id").toString();
             System.out.println(options.getJSONObject(0).get("id")); 
              Map score = new HashMap(); 
             score.put("id", score_id);
             score.put("percent", "25");
             if(component_ary.getJSONObject(j).getInt("type")==2){
                 score.put("value", "99");                  
                }
             System.out.println(score); 
             data.put(data_id, score);
             System.out.println(data); 
                
            }
            
        }
    //将map类型转换为json字符
    JSONObject putdata = new JSONObject(data);
    System.out.println(putdata);
    //将putdata输出到jmeter变量里
    vars.put("putdata", JSONObject.valueToString(putdata));

运行的结果(不是用上面的参数值运行,是我脚本实例的结果,但数据结构是一样的),

putdata的值:putdata={"671":{"id":"2660","percent":"25","value":"99"},"672":{"id":"2661","percent":"25","value":"99"},"673":{"id":"2662","percent":"25"},"670":{"id":"2655","percent":"25"}},这个值被作为变量“putdata”给jmeter使用,我们可以在debugSampler看到这个值:

img_9608261b1b2d18760a4748fb5ce1a394.jpe
image

我们可以在接口里使用这个值了,如图:

img_958141ca4ee7de5b52bc8c8fbe1f3d3f.jpe
image

一些tips

1. 新建处理器的时候,注意选择前置还是后置,前置是指在请求之前进行处理;后置是指在请求之后进行处理。

2. 调试脚本,可以新建一个debugSampler,脚本运行之后,我们可以在查看结果树里查看他的响应数据来观察变量的值;如果是beanshell,可以先在编译器里调试好再复制进去,或者直接打包成jar引用。

引用资料

json教程:http://www.open-open.com/lib/view/open1381566882614.html

beanshell的使用: http://www.cnblogs.com/puresoul/p/4915350.html

目录
相关文章
|
前端开发 测试技术 Apache
JMeter 检查点之响应断言(Response Assertion)
JMeter 检查点之响应断言(Response Assertion)
126 0
jmeter获得Response Headers,Response Body里的值
jmeter获得Response Headers,Response Body里的值
279 0
jmeter获得Response Headers,Response Body里的值
Jmeter中发出请求后的接收的response data乱码
Jmeter中发出请求后的接收的response data乱码
152 0
Jmeter中发出请求后的接收的response data乱码
|
1月前
|
测试技术 持续交付 Apache
Python性能测试新风尚:JMeter遇上Locust,性能分析不再难🧐
【10月更文挑战第1天】Python性能测试新风尚:JMeter遇上Locust,性能分析不再难🧐
130 3
|
2月前
|
测试技术 数据库 UED
Python 性能测试进阶之路:JMeter 与 Locust 的强强联合,解锁性能极限
【9月更文挑战第9天】在数字化时代,确保软件系统在高并发场景下的稳定性至关重要。Python 为此提供了丰富的性能测试工具,如 JMeter 和 Locust。JMeter 可模拟复杂请求场景,而 Locust 则能更灵活地模拟真实用户行为。结合两者优势,可全面评估系统性能并优化瓶颈。例如,在电商网站促销期间,通过 JMeter 模拟大量登录请求并用 Locust 模拟用户浏览和购物行为,可有效识别并解决性能问题,从而提升系统稳定性和用户体验。这种组合为性能测试开辟了新道路,助力应对复杂挑战。
108 2
|
13天前
|
测试技术 持续交付 Apache
Python性能测试新风尚:JMeter遇上Locust,性能分析不再难🧐
Python性能测试新风尚:JMeter遇上Locust,性能分析不再难🧐
39 3
|
12天前
|
缓存 测试技术 Apache
告别卡顿!Python性能测试实战教程,JMeter&Locust带你秒懂性能优化💡
告别卡顿!Python性能测试实战教程,JMeter&Locust带你秒懂性能优化💡
28 1
|
2月前
|
缓存 Java 测试技术
谷粒商城笔记+踩坑(11)——性能压测和调优,JMeter压力测试+jvisualvm监控性能+资源动静分离+修改堆内存
使用JMeter对项目各个接口进行压力测试,并对前端进行动静分离优化,优化三级分类查询接口的性能
谷粒商城笔记+踩坑(11)——性能压测和调优,JMeter压力测试+jvisualvm监控性能+资源动静分离+修改堆内存
|
1月前
|
缓存 测试技术 Apache
告别卡顿!Python性能测试实战教程,JMeter&Locust带你秒懂性能优化💡
【10月更文挑战第1天】告别卡顿!Python性能测试实战教程,JMeter&Locust带你秒懂性能优化💡
60 4
|
2月前
|
移动开发 JSON Java
Jmeter实现WebSocket协议的接口测试方法
WebSocket协议是HTML5的一种新协议,实现了浏览器与服务器之间的全双工通信。通过简单的握手动作,双方可直接传输数据。其优势包括极小的头部开销和服务器推送功能。使用JMeter进行WebSocket接口和性能测试时,需安装特定插件并配置相关参数,如服务器地址、端口号等,还可通过CSV文件实现参数化,以满足不同测试需求。
239 7
Jmeter实现WebSocket协议的接口测试方法