How to share the list of lists variable from setupthreadgroup to a normal threadgroup in jmeter?
for instance if I have list1=[[1,2],[3,4],[5,6]] in setupthreadgroup and same I want to use in subsequent thread group. How can we achieve this?
I am trying to save this using props.put('listOfData',list1) in setupThreadGroup and in normal threadGroup I am trying to access it using props.get(listOfData[1][1])
You need to change this one:
props.get(listOfData[1][1])
to this one:
props.get('listOfData')[1][1]
props is just a shorthand for java.util.Properties, see the JavaDoc for all available functions and Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information on this and other JMeter API shorthands available for the JSR223 test elements
Related
My test has 2 API requests.
The parameters passed in 2nd API request body are to be unique every time. So I used JSR223 PreProcessor with groovy to generate that using RandomUtils.
The Thread group is set to have 3 threads with 15 sec Ramp up time and used Loop Controller with Loop count as 10. 1st API is in thread group and 2nd API is in Loop Controller as it needs to run multiple times.
But during test execution, for 2nd API one thread is passing same request body with same parameters multiple times. Because of which the test fails. How is that possible?
It's impossible to state "how is that possible" without seeing your Groovy code.
The most common mistake is using JMeter Functions or Variables in Groovy scripts. As per 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.
If this is your case - refactor your code to use vars shorthand for JMeterVariables class instance instead of JMeter Functions or Variables syntax and it should resolve your issue.
I am currently trying to extract 10 different ids(each request will generate a different id) then i am trying to use those 10 different ids in the next thread group.
The way i am doing this is by first using a HTTP request inside a simple controller then i am using a thread group and using a module controller.
First i extract the id from the response and creating a variable like this:
Then i am setting property name like:
${__setProperty(loginassistant_${__threadNum},${loginassistant},)}
Then i am using thread group and inside a thread group i am using a module controller with 10 threads
then in the next request i am reading it similarly:
${__P(loginassistant_${__threadNum},)}
But unfortunately in all my requests(10) it is using the same id generated from the first thread group. However, i would to see 10 different ids being used to each 10 of the other requests
How many threads does your Thread Group which executes Journey Steps have? If its only one - you will have only one ID generated as JMeter properties are global.
Since JMeter 3.1 you're supposed to be using JSR223 Test Elements and Groovy language for scripting so consider migrating to JSR223 Assertion and the following Groovy code (it's equivalent of your functions calls)
props.put('loginassistant_' + (ctx.getThreadNum() + 1), vars.get('loginassistant'))
where:
props is a shorthand for JMeter Properties
vars stands for JMeterVariables
ctx is for JMeterContext
See Top 8 JMeter Java Classes You Should Be Using with Groovy article for more details.
Here is the evidence of the approach working:
Any reason for not going for Inter-Thread Communication Plugin?
Simply this is what I want help with,
Using Jmeter -
Set a variable in the JSR223 Assertion in the 1st thread
Access that value in a different threads User Defined Variables
Thanks in advance
JMeter Variables are local to the current thread only therefore you won't be able to access the value outside the current thread (virtual user) context.
As per JMeter Documentation:
Properties are not the same as variables. Variables are local to a thread; properties are common to all threads
So if you need to make some variable "global" you can convert it into a property like:
props.put('foo', 'bar')
in the JSR223 Assertion
Once done you should be able to access the value using __P() function as ${__P(foo,)} where required
You can also use __setProperty() function instead of setting the value in the JSR223 Assertion.
More information on JMeter Functions concept: Apache JMeter Functions - An Introduction
You can use below method:
props.put("var","value"); // set property in 1st thread group
props.get("var"); // get property in 2nd thread group
Alternatively you can user "Inter-Thread Communication" plugins in JMeter.
I have csv file with 10,000 entries. for each iteration, i want to use few 100's of entries. i have loaded into a java list in JSR223 sampler under only once controller. so that, i can load once and use this master list to retrieve chunk of records every time in other JSR223 samplers. i am not able to access variables "totalrows", "lines" defined in one JSR223 sampler from another JSR223 sampler in different thread group. Any other way apart from using vars.get() to get global scope for variables defined to JSR223 sampler
import org.apache.commons.io.FileUtils;
List lines = FileUtils.readLines(new File("skuinfo.csv"));
int totalrows = lines.size();
Variables are thread related, to be able to share you need to use properties (props in jsr223 test elements).
But I would advise you not to use this method as it will impact performances of JMeter particularly if your code is not correctly developed.
You also need to handle synchronization as objects will be shared among threads.
So it’s better to use built-in mechanism of JMete, ie CSV DataSet and prepare data before test.
I need to read file once and its result to be processed further in sampler.
My strategy is ThreadGroup--> BeanShell Preprocessor + BeanShell Sampler
My preprocessor should read file once for whole Thread Group and result to be used in Sampler for specific no. of thread.(i do not want to read file for each thread)
I wrote file reader code in preprocessor, now need to use the result in sampler.
Don't use the Beanshell PreProcessor as it will be executed by each thread. Use a separate Thread Group with 1 thread to read the values.
Don't use Beanshell Samplers to create the actual load, in case of more or less severe load it will become a bottleneck.
Use JSR223 Test Elements and "groovy" language for scripting - this way you'll be able to get maximum performance from your code.
Now answers:
int number = ctx.getThread().getThreadNum(); // get current thread number
props.put("value_for_thread_" + number, "foo"); // store some value specific for the specific thread
String value = props.get("value_for_thread_5"); // get value for thread 5
Where:
ctx - is a shorthand for JMeterContext
props - stands for JMeter Properties, an instance of java.util.Properties deriving all the methods and fields, global for the whole JVM instance.
See JavaDocs for aforementioned objects to see what else can be done and Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! guide for instructions on installing groovy scripting engine, scripting best practices and benchmark of Beanshell, JSR223+groovy and Java code.
Use Jmeter variables to store the values you've read, and then use them in subsequent steps. Please note that the pre-processor will run every time your thread loop is executed.
In your beanshell preprocessor, you can store a variable like this:
vars.put("name","value")
and then access it later either as
vars.get("name")
in beanshell, or as ${name} in fields of any other sampler.
Please note that if your preprocessor is part of your main thread group, it will be run every time your thread loops. If this is an expensive operation or values do not change during the run, you might want to use a setup thread group.