Reputation: 71
I just recently read that using vars.get("variable")
is much more efficient than using ${variable}
. I had experience using the latter but it results in an error, but I already managed a work around it so that the error will no longer occur (I will no longer discuss it since it is not my issue here). Here is the part of the code:
import groovy.json.JsonSlurper;
String response = prev.getResponseDataAsString();
def jsonSlurper = new JsonSlurper();
def json = jsonSlurper.parseText(response);
if (json.data.target_list) {
Random random = new Random();
String[] strLeadDBIDList = json.data.target_list.keySet();
int idxLeadDBID = random.nextInt(strLeadDBIDList.length);
String strLeadDBID = strLeadDBIDList[idxLeadDBID];
log.info("Leads Dashboard - Customer ID: " + strLeadDBID);
vars.put("strLeadDBID",strLeadDBID);
String strLeadDBModule = json.data.target_list."${strLeadDBID}".parent_type;
log.info("Leads Dashboard - Customer Type: " + strLeadDBModule);
vars.put("strLeadDBModule",strLeadDBModule);
...
So my question, is there a way to use vars.get("strLeadDBID")
instead of "${strLeadDBID}"
in the String strLeadDBModule = json.data.target_list."${strLeadDBID}".parent_type;
code? Or can I use the variable strLeadDBID
instead, and how? Thanks!!!
Upvotes: 1
Views: 1189
Reputation: 168157
My expectation that it would be something like:
String strLeadDBModule = json.data.target_list.get(vars.get("strLeadDBID")).parent_type;
If it will not work do the following:
log.info('Target list class name: ' + json.data.target_list.getClass().getName()))
and look for the relevant line in jmeter.log file. Then check JavaDoc for the given class in Groovy GDK API documentation and look for a suitable function.
Also as per JSR223 Sampler documentation
When using this feature, ensure your script code does not use JMeter variables directly in script code as caching would only cache first replacement. Instead use script parameters.
You could try the below setup:
Check out Apache Groovy - Why and How You Should Use It article for more hints on using Groovy scripting in JMeter tests
Upvotes: 1