Jmeter JSR223 Function calls from several threads - jmeter

I have written a function in file BeanShellFunction.bshrc called XYZ which I can use it in several of my BeanShell (pre and post) samplers throughout threads and .jmx files and all over the map.
Now I like to do the same in JSR223 (pre and post) and clearly I cant call that XYZ function because thats for pre and post Bean files (or Java). How do I do the same and write a fucntion called ABC for my pre/post JSR223 functions which I can use in any thread and any .jmx file?

If you want to use some custom Groovy code in __groovy() function you need to "tell" JMeter your Groovy file location via a property, i.e. add the next line to user.properties file:
groovy.utilities=/path/to/your/file.groovy
JMeter restart will be required to pick the property up
For other JSR223 friends you can add Script.evaluate() function to the beginning of your JSR223 Test Elements like:
evaluate(new File('/path/to/your/file.groovy'))
After that you will be able to use the functions from your file.
Also be aware that if your class is in compiled form and under JMeter Classpath you don't even need to take any extra actions.
More information: Apache Groovy - Why and How You Should Use It

Related

JMeter loop index always return 0 in Loop Controller

When I use the command ${__jm__myLoopControllerName__idx} in JSR223 Sampler, it always returns 0 as the index.
The sampler is in the Loop Controller. But I can see that the CSV file I'm looping is done for each line because in the listener "View Result Tree" I can see in the Request Headers, that the data is from each line. What am I doing wrong ?
Thanks for your help.
M.
Use vars to get variable in JSR223 script:
String index = vars.get("__jm__myLoopControllerName__idx");
variable inside ${} syntax is getting cached
ensure the script does not use any variable using ${varName} as caching would take only first value of ${varName}. Instead use : vars.get("varName")
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.
So either put your ${__jm__myLoopControllerName__idx} to "Parameters" section and refer it as "Parameters" in your script:
or use vars shorthand for JMeterVariables class instance like:
vars.get('__jm__myLoopControllerName__idx')

Assigning value to the variable based on the request

I have created a jmeter script as below
I am using the user defined variable with function __P() and passing the Url from .sh file
My requirement is, if the url passed is "www.abc.com" then set the value of the variable ${Prefix} to "foo" else set it to "bar"
I tried using JSR223 PostProcessor, but JSR223 PostProcessor has to have a sample associated with it.
Any suggestion how do I achieve it?
Use the following __groovy() function in the HTTP Request sampler directly:
${__groovy(if (vars.get('Url') == 'www.abc.com') {return 'foo'} else {return 'bar'},Prefix)}
For subsequent requests (once the __groovy() function has been run) you will be able to use just ${Prefix}
More information: Apache Groovy - Why and How You Should Use It
If you're not too comfortable with Groovy scripting you can consider using __if() function, it's a part of Custom JMeter Functions bundle, can be installed usign JMeter Plugins Manager

How to create a Java program in Beanshell PostProcessor in Jmeter to merge all the responses?

I have to send JSON requests based on the CSV test data. Suppose there are 90 records - which are basically the request bodies. I put the Thread in a loop to keep sending the request until the last one in the CSV.
Every time I get the response, I need to append them into a single CSV file. Now, since Jmeter Listener does not consolidate all the responses into CSV (I do not want it in xml), I want to know if I can write a Java snippet in BeanShell, capture all responses and write them to a CSV file.
You can use JSR223 Sampler with File.append adding text with , to append to CSV file
This will append to the end of the file.
File file = new File("out.txt")
file.append("hello\n")
If you want the "program"
Add JSR223 Listener to your Test Plan (since JMeter 3.1 it is recommended to use JSR223 Test Elements for scripting)
Put the following code into "Script" area
new File('myfile.csv') << prev.getResponseDataAsString() << System.getProperty('line.separator')
where prev stands for previous SampleResult class instance. Check out Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information on JMeter API shorthands available for the JSR223 Test Elements.
A better option would be saving a response into a JMeter Variable using i.e. Regular Expression Extractor and writing it to a file via sample_variables property

jmeter beanshell call jmeter function

I have a HTTP request sampler configured. In the request body, I call out to a beanshell function I wrote:
${__BeanShell(createHeader("GET"\,"Customer"\,"${__UUID}"\,"${__time(yyyy-MM-dd'T'hh:mm:ss)}"))}
The function just builds some request strings with the passed-in parameters. I want to remove the jmeter function calls (__UUID and __time), and call them directly from within the beanshell function. I could not find a way to do that.
Thanks
Don't inline JMeter Functions or Variables into scripts, in your case you will have to go for code-based equivalents instead, to wit:
__UUID() -> UUID.randomUUID().toString()
__time() -> new java.text.SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss").format(new Date())
Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting, in your case it would be __groovy() function. If you want to re-use createHeader method - you can put it into a separate .groovy file and define groovy.utilities property pointing to this file.
See Apache Groovy - Why and How You Should Use It article for more information on using Groovy scripting in JMeter tests.

how can i pass dynamic values to url in jmeter

I have to give dynamic values to url which takes number of users and their age , which can be selected though web page. but I want to give it in Jmeter using BeanShell PostProcessor.
Help me in this,since I'm new to Jmeter.
This is the path:
/destinations/packages?airports%5B%5D=LGW&units%5B%5D=000577%3ADESTINATION&when=29-05-2016&until=&flexibility=true&flexibleDays=3&noOfAdults=2&noOfSeniors=0&noOfChildren=1&childrenAge=3&duration=7114&first=0&searchType=search&searchRequestType=ins&sp=true&multiSelect=true
from what I've got looks like you can use CSV Data Set Config.
Create .txt file with the data you want to feed your test with;
Place the above .txt file to the folder where your .jmx file lies;
In your Test plan: Under your request sampler - place CSV Data set Config;
Then if you need to use your dynamic values within one threadgroup => refer these data as ${quanity}, ${age} in you url.
If you need to pass these values across the threadgroups => add BeanShell Assertion
Then (in the other Tread group) refer those as ${__property(_quantity)},${__property(_age)}.
Hope, it helps.
First of all you need Beanshell PreProcessor, not Beanshell PostProcessor.
All this parameters are basically name/value pairs which can be defined via HTTPSamplerBase class. HTTPSamplerBase class instance is available to Beanshell PreProcessor as sampler pre-defined variable so if you add the following code into the Beanshell PreProcessor "Script" area
sampler.addEncodedArgument("airports[]","LGW");
sampler.addEncodedArgument("units[]","000577:DESTINATION");
sampler.addEncodedArgument("when","29-05-2016");
sampler.addEncodedArgument("until","");
sampler.addEncodedArgument("flexibility", "true");
sampler.addEncodedArgument("flexibleDays","3");
sampler.addEncodedArgument("noOfAdults","2");
//etc
Your HTTP request will be populated with the values, you set via Beanshell.
JMeter Variables can be accessed via vars shorthand which stands for JMeterVariables class instance.
String airport = vars.get("airport");
sampler.addEncodedArgument("airports[]", airport);
//etc
See How to Use BeanShell: JMeter's Favorite Built-in Component article for comprehensive information on how to use Beanshell Test Elements in Jmeter.
Remember that it is recommended to avoid scripting where possible so if there is another way of implementing your task - go for it.

Resources