How do I call a Custom Function in JMeter - jmeter

I've created a custom function in JMeter that returns a n length string with random characters. Where should I define the function, and how do I call it?

As long as your function is in the jmeter classpath, you should be able to call it just like any standard function with ${...} syntax, as described here.
Do make sure your jar file containing your function is in lib/ext, or added to the jmeter classpath via search_path's input option

You can do this in a beanshell script or via a javascript call. See the manual on
functions and on the beanshell preprocessor.

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 use StringFromFile function within a JSR223 Sampler?

I can't find any good docs/examples on how to use the StringFromFile function to get multiple records from a source data file from within a JSR223 Sampler. Could somebody please help me?
My code which doesn't work:
String filename = "filename.csv";
String record = StringFromFile(filename,"id");
You shouldn't be using it directly in the scripts, 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.
So the solution would be using File.readLines() function which reads the file into a List of strings
More information: The Groovy Templates Cheat Sheet for JMeter

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.

Jmeter JSR223 Function calls from several threads

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

Resources