如何 return BeanShell PostProcessor 中的 jsonarray 用于 jmeter 中的下一个测试用例
How to return an jsonarray in BeanShell PostProcessor for next testcase in jmeter
在我得到这样的列表之后:
[{
"id": "55af0d89ba411ff8b8000680",
},{
"id": "55af0d89ba411ff8b80006bc",
"object": {
"id": "55af0d89ba411ff8b80006bd",
}
}]
在 BeanShell PostProcessor 中,我有代码 return 列表 ID 如下:
import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;
import java.util.*;
String response = prev.getResponseDataAsString();
JSONParser parser = new JSONParser();
JSONArray GroupList = (JSONArray) parser.parse(response);
Iterator i = GroupList.iterator;
JSONArray GroupIds = new JSONArray();
while (i.hasNext())
{
JSONObject objGroup = (JSONObject) i.next();
GroupIds.push(objGroup.get("id"));
}
vars.putObject("GroupIds",GroupIds);
对于下一个测试元素,我创建了 foreach 循环控制器来循环 GroupIds
,但我在日志中看到,它有这样的错误:
jmeter.util.BeanShellInterpreter: Error invoking bsh method:
eval Sourced file: inline evaluation of: ``import
org.json.simple.JSONObject; import org.json.simple.JSONArray; import
org. . . . '' : Typed variable declaration : Cannot access field:
iterator, on object:[{
"id": "55af0d89ba411ff8b8000680",
"name": "Companies",
"description": "Contacts of companies",
"type": "Group",
"created_at": "2015-07-22T03:27:05Z",
"updated_at": "2015-07-23T02:45:02Z",
"dominant_gender": "unknown",
"average_age": "?" }, {
"id": "55af0d89ba411ff8b80006bc",
"name": "Subscription",
"description": "Subscription",
"type": "Subscriptions::SubscriptionList",
"average_age": "?",
"subscription": {
"id": "55af0d89ba411ff8b80006bd",
"name": "Subcription 1",
"form_ids": ["5526430cba411ff8c30001cd"]
} }]
jmeter.extractor.BeanShellPostProcessor: Problem in BeanShell script
org.apache.jorphan.util.JMeterException: Error invoking bsh method:
eval Sourced file: inline evaluation of: ``import
org.json.simple.JSONObject; import org.json.simple.JSONArray; import
org. . . . '' : Typed variable declaration : Cannot access field:
iterator, on object:[{
"id": "55af0d89ba411ff8b8000680",
"name": "Companies",
"description": "Contacts of companies",
"type": "Group",
"created_at": "2015-07-22T03:27:05Z",
"updated_at": "2015-07-23T02:45:02Z",
"dominant_gender": "unknown",
"average_age": "?" }, {
"id": "55af0d89ba411ff8b80006bc",
"name": "Subscription",
"description": "Subscription",
"type": "Subscriptions::SubscriptionList",
"average_age": "?",
"subscription": {
"id": "55af0d89ba411ff8b80006bd",
"name": "Subcription 1",
"form_ids": ["5526430cba411ff8c30001cd"]
} }]
有什么问题?
您的代码有几个问题:
Iterator i = GroupList.iterator;
- 括号是必需的,因为 iterator
是一种方法,而不是字段。
GroupIds.push(objGroup.get("id"));
- 将push
改为add
,没有push
方法
完整的工作代码:
String response = prev.getResponseDataAsString();
JSONParser parser = new JSONParser();
JSONArray GroupList = (JSONArray) parser.parse(response);
Iterator i = GroupList.iterator();
JSONArray GroupIds = new JSONArray();
while (i.hasNext()) {
JSONObject objGroup = (JSONObject) i.next();
GroupIds.add(objGroup.get("id"));
}
vars.putObject("GroupIds", GroupIds);
顺便说一句,不推荐在高负载下使用Beanshell,因为它效率不高。对于一个线程来说还可以,但是当涉及到巨大的负载时 - 它可能会成为性能瓶颈并可能破坏您的测试。如果是这种情况 - 请考虑改用 JSR223 PostProcessor 和 "groovy" 语言。
需要很少的改动:
- 添加 groovy-all.jar 到 JMeter 的 /lib 文件夹
- 将所有 Beanshell 测试元素替换为带有 "groovy" 引擎的 JSR223
请参阅 Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! 文章了解详细的安装说明、脚本提示和技巧以及不同的脚本引擎性能基准。
UPDATE - 为 ForEach 控制器生成变量:
import org.json.simple.parser.JSONParser;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
String response = prev.getResponseDataAsString();
JSONParser parser = new JSONParser();
JSONArray GroupList = (JSONArray) parser.parse(response);
for (int i = 0; i < GroupList.size(); i++) {
JSONObject objGroup = (JSONObject) GroupList.get(i);
vars.put("Group_" + i, objGroup.get("id"));
}
然后添加一个ForEach Controller,配置如下:
- 输入变量前缀:
Group
- 起始索引:
-1
- 输出变量名:
CURRENT_GROUP
- 在数字前添加“_” - 勾选方框
在需要的地方使用${CURRENT_GROUP}
在我得到这样的列表之后:
[{
"id": "55af0d89ba411ff8b8000680",
},{
"id": "55af0d89ba411ff8b80006bc",
"object": {
"id": "55af0d89ba411ff8b80006bd",
}
}]
在 BeanShell PostProcessor 中,我有代码 return 列表 ID 如下:
import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;
import java.util.*;
String response = prev.getResponseDataAsString();
JSONParser parser = new JSONParser();
JSONArray GroupList = (JSONArray) parser.parse(response);
Iterator i = GroupList.iterator;
JSONArray GroupIds = new JSONArray();
while (i.hasNext())
{
JSONObject objGroup = (JSONObject) i.next();
GroupIds.push(objGroup.get("id"));
}
vars.putObject("GroupIds",GroupIds);
对于下一个测试元素,我创建了 foreach 循环控制器来循环 GroupIds
,但我在日志中看到,它有这样的错误:
jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import org.json.simple.JSONObject; import org.json.simple.JSONArray; import org. . . . '' : Typed variable declaration : Cannot access field: iterator, on object:[{ "id": "55af0d89ba411ff8b8000680", "name": "Companies", "description": "Contacts of companies", "type": "Group", "created_at": "2015-07-22T03:27:05Z", "updated_at": "2015-07-23T02:45:02Z", "dominant_gender": "unknown", "average_age": "?" }, { "id": "55af0d89ba411ff8b80006bc", "name": "Subscription", "description": "Subscription", "type": "Subscriptions::SubscriptionList", "average_age": "?", "subscription": { "id": "55af0d89ba411ff8b80006bd", "name": "Subcription 1", "form_ids": ["5526430cba411ff8c30001cd"] } }]
jmeter.extractor.BeanShellPostProcessor: Problem in BeanShell script org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import org.json.simple.JSONObject; import org.json.simple.JSONArray; import org. . . . '' : Typed variable declaration : Cannot access field: iterator, on object:[{ "id": "55af0d89ba411ff8b8000680", "name": "Companies", "description": "Contacts of companies", "type": "Group", "created_at": "2015-07-22T03:27:05Z", "updated_at": "2015-07-23T02:45:02Z", "dominant_gender": "unknown", "average_age": "?" }, { "id": "55af0d89ba411ff8b80006bc", "name": "Subscription", "description": "Subscription", "type": "Subscriptions::SubscriptionList", "average_age": "?", "subscription": { "id": "55af0d89ba411ff8b80006bd", "name": "Subcription 1", "form_ids": ["5526430cba411ff8c30001cd"] } }]
有什么问题?
您的代码有几个问题:
Iterator i = GroupList.iterator;
- 括号是必需的,因为iterator
是一种方法,而不是字段。GroupIds.push(objGroup.get("id"));
- 将push
改为add
,没有push
方法
完整的工作代码:
String response = prev.getResponseDataAsString();
JSONParser parser = new JSONParser();
JSONArray GroupList = (JSONArray) parser.parse(response);
Iterator i = GroupList.iterator();
JSONArray GroupIds = new JSONArray();
while (i.hasNext()) {
JSONObject objGroup = (JSONObject) i.next();
GroupIds.add(objGroup.get("id"));
}
vars.putObject("GroupIds", GroupIds);
顺便说一句,不推荐在高负载下使用Beanshell,因为它效率不高。对于一个线程来说还可以,但是当涉及到巨大的负载时 - 它可能会成为性能瓶颈并可能破坏您的测试。如果是这种情况 - 请考虑改用 JSR223 PostProcessor 和 "groovy" 语言。
需要很少的改动:
- 添加 groovy-all.jar 到 JMeter 的 /lib 文件夹
- 将所有 Beanshell 测试元素替换为带有 "groovy" 引擎的 JSR223
请参阅 Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! 文章了解详细的安装说明、脚本提示和技巧以及不同的脚本引擎性能基准。
UPDATE - 为 ForEach 控制器生成变量:
import org.json.simple.parser.JSONParser;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
String response = prev.getResponseDataAsString();
JSONParser parser = new JSONParser();
JSONArray GroupList = (JSONArray) parser.parse(response);
for (int i = 0; i < GroupList.size(); i++) {
JSONObject objGroup = (JSONObject) GroupList.get(i);
vars.put("Group_" + i, objGroup.get("id"));
}
然后添加一个ForEach Controller,配置如下:
- 输入变量前缀:
Group
- 起始索引:
-1
- 输出变量名:
CURRENT_GROUP
- 在数字前添加“_” - 勾选方框
在需要的地方使用${CURRENT_GROUP}