How to get current loop number of ThreadGroup in jmeter - jmeter

I am using jmeter. I set Loop Count to 5 in ThreadGroup and number of Thread to 1. How can I get the current loop number so I can perform some operation in beanshell postprocessor?

You can do it like ${__BeanShell(vars.getIteration();,)} where:
__Beanshell - JMeter function allowing execution of arbitrary Beanshell script
vars - shorthand to JMeterVariables class which holds current JMeter variables
getIteration() method returns what you're looking for
See How to use BeanShell: JMeter's favorite built-in component guide for more information on Beanshell scripting in JMeter.

Related

Jmeter share data from setupthreadgroup to normal threadgroup

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

Difference between a JSR223 Sampler and JSR223 PreProcessor

I'm new to Jmeter and vigorously learning.
I wanted to know how JSR223 Sampler and JSR223 PreProcessor are different, can a sampler be used the same way as the preprocessor?
General difference is that PreProcessor won't be executed unless it has a sampler in its scope which it will be triggered (per sampler)
Pre-Processor element is defined to alter the settings of Samplers in their scope. It will always execute before the actual sampler request.
Specific difference is that JSR223 PreProcessor doesn't have SampleResult available, so for example you can't execute the following example:
SampleResult.setStopTest(true);
The SampleResult ResponseData is set from the return value of the script. If the script returns null, it can set the response directly, by using the method SampleResult.setResponseData(data), where data is either a String or a byte array. The data type defaults to "text", but can be set to binary by using the method SampleResult.setDataType(SampleResult.BINARY).
The SampleResult variable gives the script full access to all the fields and methods in the SampleResult. For example, the script has access to the methods setStopThread(boolean) and setStopTest(boolean).
JSR223 Sampler is a Sampler, so it will generate a SampleResult which will appear in the Test Results (unless you call SampleResult.setIgnore() method)
JSR223 PreProcessor cannot be executed per se, you need to connect it to one (or many) Samplers according to JMeter Scoping Rules so it will be executed before one (or many) samplers. PreProcessors execution time is not reflected in the test results (unless you use a Transaction Controller configured to include it)
Both can run an arbitrary code (it's recommended to stick to Groovy) so which one is to use mostly depends on your use case, if you need to conduct the load and measure the time - go for the Sampler, if you need to set up some data - go for the PreProcessor, etc.

How to pass data to BeanShell sampler using BeanShell preprocessor using JMeter?

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.

Jmeter increment a variable after loop

I have a test that loops as suggested here:
Is it possible to loop a test in JMeter?
I have a Thread Group with 100 users and a loop count of 5.
A Runtime Controller to run for 30 seconds.
Now when the Runtime Controller finishes I would like to increment a variable that I can read inside my BeanShell sampler in my test. At the end of the test this variable should be equal to the loop count.
Ok figured it out!
I used a Counter element and set it to "Track counter independently for each user".
The variable increments only after each loop.
Also very important the Counter has to be under Thread Group but not inside the Runtime Controller.
Since you use Beanshell you can access current loop number as simple as
vars.getIteration();
See JMeterVariables class JavaDoc to see what else could be done using it, How to use BeanShell: JMeter's favorite built-in component guide for advanced information on Beanshell scripting in Apache JMeter and remember that the method will work only on Thread Group level, the value won't increment inside Loop Controller

How to call a function defined in JSR223 Sampler in Jmeter

I have the function defined in JSR223 sampler in Jmeter.
I would like to call it in the different sampler in the same thread.
This function is create a new time value.
Thanks,
Pdo
I can see 2 options:
Switch from JSR223 Sampler to Beanshell Sampler. In that case you will be able to use bsh.shared namespace to reuse variables, properties, and functions.
If your request can be narrowed down to creating a new time value there is __time() function capable of returning current date in different formats. If you need time in the future/past - you can combine it with __longSum function. For better information on aforementioned and other functions refer to How to Use JMeter Functions posts series

Resources