How to fetch Map from jmeter global properties in different thread - jmeter

I am using Jmeter 5.0 where i have piece of java code written inside a JSR223 PostProcessor in a single thread group. The values are getting stored inside the maps and i can view it in the Debug Sampler. The code is as follows -
import java.util.Map;
import java.util.HashMap;
Map gamePlayHistoryMap = new HashMap();
gamePlayHistoryMap.put(vars.get("playerId"), vars.get("GameplayHistoryId"));
props.put("GamePlayHistoryMap", gamePlayHistoryMap);
Map payLevelDetailsMap = new HashMap();
payLevelDetailsMap.put(vars.get("playerId"), vars.get("PayLevelDetails"));
props.put("PayLevelDetailsMap", payLevelDetailsMap);
Now i want to access the values of these 2 maps in different thread group. How to do that ? I have tried using JSR223 PreProcessor where i have written code as follows -
import java.util.Map;
import java.util.HashMap;
Map gameTemplateIdMap = props.get("GamePlayHistoryMap");
Map payLevelDetailsMap = props.get("PayLevelDetailsMap");
I am unable to get the values as stored in previous thread.Can someone help me in pointing where i might have gone wrong ?
1st Thread Debug Sampler -
GamePlayHistoryMap={107=3387} HTTPResponse.parsers=htmlParser
wmlParser cssParser
PayLevelDetailsMap={107={"prizeQuantity":0,"prizeType":{"prizeTypeId":2,"prizeName":"Cash","description":"Promotional
Cash","listofErrors":[],"isValid":true},"isValid":true,"description":"$0.3","externalPrizeID":null,"prizeTypeID":2,"gameTemplateID":0,"isNotifySocial":false,"prizeValue":0.3,"payMethodID":1,"winProbability":17.5,"celebrationLevel":null,"payMethod":{"payMethodID":1,"name":"CMS","description":"Account","listofErrors":[],"isValid":true},"listofErrors":[],"payLevelTemplateID":41170,"isNotifySignage":true,"position":3,"celebrationLevelID":1}}
2nd Thread Debug Sampler -
GamePlayHistoryMap={107=} HTTPResponse.parsers=htmlParser wmlParser
cssParser PayLevelDetailsMap={107=}

Are you sure that your 2nd Thread Group is being executed after 1st Thread Group? The only failure reason I can think of is that you're trying to read the value from map at the time when it has not yet been defined.
Check out jmeter.log file for any suspicious entries. If I'm right and this is the case you can:
Tick Run Thread Groups consecutively at Test Plan level
Or use Inter-Thread Communication Plugin in order to block 2nd Thread Group until the value of map is set in the 1st Thread Group

I did this using the bsh.shared property of BeanShell Sampler. 1st Thread group code which populates the map is as follows -
import java.util.Map;
import java.util.HashMap;
Map gamePlayHistoryMap = new HashMap();
Map payLevelDetailsMap = new HashMap();
gamePlayHistoryMap.put(vars.get("playerId"), vars.get("GameplayHistoryId"));
bsh.shared.gphMap = gamePlayHistoryMap;
payLevelDetailsMap.put(vars.get("playerId"), vars.get("PayLevelDetails"));
bsh.shared.pldMap = payLevelDetailsMap;
2nd Thread Group code which fetches the map is as follows -
import java.util.Map;
import java.util.HashMap;
Map myMap1 = bsh.shared.gphMap;
vars.put("GamePlayHistoryId", myMap1.get(vars.get("playerId")));
log.info(myMap1.get(vars.get("playerId")));
Map myMap2 = bsh.shared.pldMap;
vars.put("PayLevelDetails", myMap2.get(vars.get("playerId")));
log.info(myMap2.get(vars.get("playerId")));
Of course i think this can also be done using JSR223 elements, just need to figure out how. I also need to figure out how to declare the map in the 1st thread group globally so that each time the thread runs the values of the map gets appended. As of now each time the map gets newly initialized whenever the thread runs more than once. Any pointers how to achieve this would be greatly appreciated.
Thank You

Related

Jmeter Mongo Db insert script is not adding iterations from csv data set

CSV set as below
enter image description here
Jmeter insert mongo DB script is as below
import com.mongodb.*
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.MongoClientSettings;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import java.util.Arrays;
try {
MongoCollection<Document> collection = vars.getObject("collection");
Document document = new Document("_id", "${_id}")
.append("has_mortgages",false)
.append("data", new Document ("etag":"${_etag}")
.append("links", new Document ("charges","/company/${_id}/charges"))
);
collection.insertOne(document);
}
catch (Exception e) {
SampleResult.setSuccessful(false);
SampleResult.setResponseCode("500");
SampleResult.setResponseMessage("Exception: " + e);
}
Thread group - no.of threads is 1 and loop count is 3
While running the script the first iteration is picking the value of _id as PT000001 and inserting the record. While
second iteration is picking up the _id correctly from the csv which is PT000002 but the
collection.insertOne(document); is still adding the _id PT000001
Can some one please tell me what is wrong with my script??
Take a look at JSR223 Sampler documentation:
The JSR223 test elements have a feature (compilation) that can significantly increase performance. To benefit from this feature:
Use Script files instead of inlining them. This will make JMeter compile them if this feature is available on ScriptEngine and cache them.
Or Use Script Text and check Cache compiled script if available property.
When using this feature, ensure your script code does not use JMeter variables or JMeter function calls directly in script code as caching would only cache first replacement. Instead use script parameters.
so you need to change "${_id}" to vars.get('_id') and "${_etag}" to vars.get('_etag') and your script will start working as expected.
vars stands for JMeterVariables class instance, see the JavaDoc for all available functions and Top 8 JMeter Java Classes You Should Be Using with Groovy for more information on this and other JMeter API shorthands available for the JSR223 Test Elements.
Resolved after unchecking the below check from sampler
Cache compiled script if available

Problem in JSR223 script, JSR223 PostProcessor : javax.script.ScriptException

I am using Jmeter 5.0 where i have piece of java code written inside a JSR223 PostProcessor. The code is as follows -
import java.util.Map;
import java.util.HashMap;
Map gamePlayHistoryMap = new HashMap();
gamePlayHistoryMap.put(${playerId}, ${GameplayHistoryId});
props.put("GamePlayHistoryMap", gamePlayHistoryMap);
Map payLevelDetailsMap = new HashMap();
payLevelDetailsMap.put(${playerId}, ${PayLevelDetails});
props.put("PayLevelDetailsMap", payLevelDetailsMap);
However when i execute the test plan, in the console i get the following error -
javax.script.ScriptException: In file: inline evaluation of: import java.util.Map; import java.util.HashMap; Map gamePlayHistoryMap = new H . . . '' Encountered "( 107 , )" at line 6, column 23.
in inline evaluation of:import java.util.Map; import java.util.HashMap; Map gamePlayHistoryMap = new H . . . '' at line number 6
Can someone help me in pointing where i might have gone wrong ?
Don't use ${} in JSR223 scripts, use instead vars.get("") to get varibles
gamePlayHistoryMap.put(vars.get("playerId"), vars.get("GameplayHistoryId"));
It seems that GameplayHistoryId is empty, in such case add default value in JSONExtractor or fail test
See JMeter's best practices for JSR223 scripting:
In this case, ensure the script does not use any variable using ${varName} as caching would take only first value of ${varName}. Instead use :
vars.get("varName")
Since JMeter 3.1 you should be using groovy language for scripting, looking into your exception details it appears you're using java which is not real Java, it's Beanshell interpreter which has worse performance comparing to Groovy and you have to stick to Java 5 syntax.
Don't inline JMeter Functions and/or Variables into scripts as they might be resolved into something causing script failures and in case of Groovy they conflict with GString templates and compilation caching feature. Use vars shorthand for JMeterVariables class to read existing variables values and create new ones, i.e. replace this line:
gamePlayHistoryMap.put(${playerId}, ${GameplayHistoryId});
with this one:
gamePlayHistoryMap.put(vars.get('playerId'), vars.get('GameplayHistoryId'));
You are missing the Map key/value definition.
Map <String, String> gamePlayHistoryMap = new HashMap<>();
gamePlayHistoryMap.put(${playerId}, ${GameplayHistoryId});
Not sure about the answer of:
Don't use ${} in JSR223 scripts, use instead vars.get("")
not sure it has anything to do with it.

How to get the name given to transaction controller using beanshell preprocesssor in jmeter

I want to get the name given to transaction controller using BeanShell preprocessor in JMeter.
Which I want to use to connect and display in dynaTrace later using header manager.
I tried something like this using BeanShell listener
String test = sampleResult.getSampleLabel();
log.info(test);
but I want to use the preprocessor.
log.info(sampler.getName());
This is used to get the name of sampler, in the similar way I want to get the name of transaction controller.
Specifically, I want to use BeanShell preprocessor .
Can somebody help me in this?
You cannot walk further than Previous Result or Previous Sampler so I would state that it is not something you can implement easily. Looks like your test is not very well designed as normally people do not require knowing the name of the parent sampler controller.
Nevertheless you can get access to JMeter Test Plan Tree and figure out information from there. The example code will look something like:
import org.apache.jmeter.control.TransactionController;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.SearchByClass;
import java.lang.reflect.Field;
import java.util.Collection;
StandardJMeterEngine engine = ctx.getEngine();
Field test = engine.getClass().getDeclaredField("test");
test.setAccessible(true);
HashTree testPlanTree = (HashTree) test.get(engine);
SearchByClass txnCtrlSearch = new SearchByClass(TransactionController.class);
testPlanTree.traverse(txnCtrlSearch);
Collection txnControllers = txnCtrlSearch.getSearchResults();
for (Object txnController : txnControllers) {
log.info(((TransactionController) txnController).getName());
}
Demo:
Some information on using JMeter API from Beanshell scripts: How to Use BeanShell: JMeter's Favorite Built-in Component

How can I navigate the JMeter test element tree from within a sampler script

From a JSR223 Sampler, I can get access to the current test element using the sampler variable.
From there, how can I navigate the tree of TestElement objects? For example, how can I get access to the parent test element (and then it’s parent, etc) or how can I get access to the TestPlan test element?
Background:
I want to dynamically create a JDBC Connection Configuration element from a JSR223 Sampler using Groovy.
From other questions (e.g., here) and web searches (e.g., here), I know how to create test plan elements from the top down (e.g., how to create a test plan and build the tree down from there). So I know how to do the new DataSourceElement() which is a TestElement but I don’t know how to add that new element to the test plan. In the sampler script I have access to the sampler (Sampler) and the ctx (JMeterContext) variables but I don’t know how to navigate the test element tree.
I tried just using sampler.addTestElement but a config element isn’t really valid under a sampler element. Still, I did try but the config element was not found when I tried to use it in a JDBC Request (error: "No pool found named: 'myDatabaseThreadPool', ensure Variable Name matches Variable Name of JDBC Connection Configuration").
I’m hoping that if I can get the TestPlan element and add the config element to that, then it would work.
FWIW, my test plan looks like this:
Test Plan
Thread Group 1 (could be a setup thread group)
JSR223 Sampler (this is where I want to create the dynamic config)
Thread Group 2 (multiple threads)
JDBC Request (uses the pool variable name specified in the dynamic config)
View Results Tree
I can go into further detail about why I want to dynamically create the JDBC Connection Configuration, but if there’s an easy answer about how to navigate the test element tree from inside my sampler script I’d like to know that anyway.
As you have mentioned you have access to JMeterContext via ctx shorthand. Hence you have access to StandardJMeterEngine class instance via ctx.getEngine(); method.
Looking into StandardJMeterEngine source you can see that test plan is being stored as HashTree structure:
private HashTree test;
So the choices are in:
change access modifier to public and recompile JMeter from sources
use Trail - Java Reflection API in order to access test value
Reference code:
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.SearchByClass;
import java.lang.reflect.Field;
import java.util.Collection;
StandardJMeterEngine engine = ctx.getEngine();
Field test = engine.getClass().getDeclaredField("test");
test.setAccessible(true);
HashTree testPlanTree = (HashTree) test.get(engine);
SearchByClass testPlans = new SearchByClass(TestPlan.class);
testPlanTree.traverse(testPlans);
Collection testPlansRes = testPlans.getSearchResults();
TestPlan testPlan = (TestPlan)testPlansRes.toArray()[0];
//do what you need with "testPlanTree" and/or "testPlan"
Check out How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information using JMeter and Java API from scripting test elements.

How to capture thread specific different data form drop down list in jmeter?

My application consist the Select Title drop down list contains values as Mr, Miss, Dr & Mrs.
I want to capture the different title (Random but from above 4) for different thread. please suggest how it is possible.
This is my script i have pass the title parameter as ${randomTitle}
Value pass to database as,
Post request as,
Out of interest, is it vital to use enum there?
Try amending your code as follows:
import java.util.Random;
String[] frm_titles = {"Mr", "Miss", "Dr", "Mrs"};
Random randGenerator = new Random();
int randInt = randGenerator.nextInt(frm_titles.length);
vars.put("randomTitle",frm_titles[randInt]);
It should work this way. Enum must not be local to Beanshell interpreter, if you need to use enum structure - compile it as .jar and place it to JMeter classpath.
See How to use BeanShell: JMeter's favorite built-in component guide for more details on Beanshell scripting in Apache JMeter.
You can use Beanshell Preprocessor:
import java.util.Random;
public enum frm_titles {"Mr", "Miss", "Dr", "Mrs"};
Random randGenerator = new Random();
int randInt = randGenerator.nextInt(frm_titles.values().length);
vars.put("randomTitle",frm_titles.values()[randInt].toString());
Then, in your test plan change the post params and add ${randomTitle} instead your title param.
Check this links for more info:
http://www.beanshell.org/manual/quickstart.html
http://jmeter.apache.org/usermanual/component_reference.html#BeanShell_Sampler
http://jmeter.apache.org/usermanual/functions.html
http://testeverythingqtp.blogspot.com.es/2013/01/jmeter-bean-shell-script-create-file.html
http://jmeter-kh.blogspot.com.es/2009/07/how-to-make-beanshell-work-in-jmeter.html
I think 2 ways are possible,
you can try beanshell processor
you can try regular expression extractor and counter
first approach is best explained above, for 2nd approach you can try,
Random product selection using Jmeter

Resources