Jmeter/Groovy: getting iteration number without reference to Beanshell - performance

Basically this in a slightly (but meaningfully!) different context. From within a Groovy script being run in jMeter (those are all the details I have at the moment), I need functionality to tell the script what iteration of the test it is currently on.

There are 2 options:
Use vars.getIteration() function
Since JMeter 4.0 you can also get current Thread Group iteration as ${__jm__Thread Group__idx}. From the Groovy script it would be vars.get('__jm__Thread Group__idx)`

Related

Thread is passing same Request Body multiple times. JSR223 PreProcessor is used to generate random request body every time

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.

Jmeter - Unable to move to next iteration when an ERROR occurs

I am using the following code in BeanShell postprocessor to stop the current iteration on an error and move to the next iteration. But in my case, I am handling the duration programmatically(infinite while loop and time functions) and not using the thread group loop/duration. So loop count is set to default one at thread group level.
My jmeter script would be -> 3 transaction + whilecontroller(10 transactions).
My error would occur at 5th transaction inside while loop. so i need to stop at that 5th transaction level and start the thread again.
ctx.setTestLogicalAction(org.apache.jmeter.threads.JMeterContext.TestLogicalAction.START_NEXT_ITERATION_OF_CURRENT_LOOP);
So in this case where am not using thread group loop count/duration and using the above code in a Beanshell Postprocessor, it is not moving to next iteration(like stop the current wihle loop run and start from initial). Am I missing something, could someone suggest?
It won't, if the error occurs at 5th iteration - "your" code will proceed to 6th iteration without executing any Samplers which are below the Beanshell PostProcessor. According to interpretation of your description with my very limited level of English I believe you need to use START_NEXT_ITERATION_OF_THREAD action instead.
It's recommended to use JMeter's built-in test elements and avoid scripting where possible. Particular your case can be handled using If Controller and Flow Control Action Sampler combination
If you prefer or have to go for scripting - consider using the most performing language which is Groovy, it's recommended to use JSR223 Test Elements and Groovy language for scripting since JMeter 3.1

How to run a JSR223 PreProcessor script once per loop

I currently have a loop that executes a bunch of queries via JDBC request samplers. They should all share a random ID that changes on every loop.
I tried using the beanshell script and JSR223 PreProcessor. But the PreProcessor gets executed before every single JDBC reuqest sampler, not once per loop. I feel like there is an obvious fix to my problem that I am missing.
I also tried putting the JSR223 script into an "Only Once Controller". But then the random variable I inject with vars.put() is not visible to the JDBC sampler. Also, as far as i understand the Only Once Controller, it would only execute on the first loop iteration. Which is not what I want.
JSR223 PreProcessor obeys JMeter Scoping Rules so if you put it to be a child of i.e. q_insert1 sampler - it will be executed only once per iteration.
I would also recommend re-considering language selection, since JMeter 3.1 it's recommended to use Groovy for scripting

JMeter: thread switching between variables for preset time periods throughout a test run

I have a script with a once only loop where a thread uses reg expression extractor to get a variable into an array. After the once only loop, the thread selects a random instance of that variable and then continues with that instance of the variable for the entire test run.
It would be more realistic for the script to do the following:
Jmeter thread uses variable_1 for x seconds, switch to variable_2 for x seconds, switch to variable_3 for x seconds
Variables_1,2,3,x coming from the reg ex array should be selected randomly
x seconds could be 300, 600, 1200 and selected randomly
Is this something that can be accomplished with JMeter controllers/functions or would it require something along the lines of custom beanshell code?
It can be done using ForEach Controller & Runtime Controller.
For Each controller is to iterate the variables one by one.
Under Runtime controller, you keep all your samplers to be executed for the duration.
Test Plan:
For Each Controller config:
Runtime Controller config:
I would recommend reconsidering your approach as this way you test won't be repeatable. The whole idea of testing is that when you run the test 2 or more times - you should get the same results, elsewise if you discover a product issue you won't be able to reproduce it to ensure that it is fixed.
Coming back to your question: yes it can be done via JMeter Test Elements without having to go into scripting. Take a look at __chooseRandom() function available via JMeter Plugins project. The easiest way of installing JMeter Plugins and keeping them up-to-date is using JMeter Plugins Manager

jMeter - Beanshell bsh.shared hashmap data in init file?

I have a jMeter project I'm working on which has thrown up all sorts of problems. Mainly due to storing and scoping of variable data throughout the run.
I now have things pretty much working and I'm using a Beanshell shared hashmap to store data through the run. I don't need to worry about thread safety due to the way I'm doing it.
It works, but it re-initialises itself each time a thread group runs. Despite putting the initialisation step outside all thread groups.
So, as I understand it, the solution is to put all the startup data into an initialisation file which is only run once on startup. But I can't figure out how I'm supposed to do that? I've copied the code from the Beanshell Preprocessor I was using previously into a ".bshrc" file and updated the jMeter properties file with the location of my ".bshrc" file, but it doesn't seem to work. It doesn't actually seem to have done anything. When I run the test, no values are present and everything fails.
I've tried using:
beanshell.init.file=../bin/data.bshrc
and
beanshell.preprocessor.init=../bin/data.bshrc
I've tried to find some sort of idiots guide to setting up an init file, but I can't find anything helpful. This is the first time I've had to make much serious use of Beanshell and my Java knowledge is VERY limited at best!
At the moment, I'm getting round it by running my test once with the original Beanshell pre-processor enabled. This sets up the hashmaps and they stay resident in memory from there on. I stop this run, disable the pre-processor, and all subsequent runs work just fine.
Anyone?
I would suggest using setUp Thread Group which is being executed prior to any other Thread Groups and define your test data there with a Beanshell Sampler like
bsh.shared.myMap = new java.util.HashMap();
bsh.shared.myMap.put("foo","bar");
// any other operations
After that in your main Thread Group(s) you can access myMap values in any Beanshell-enabled test element (Sampler, Pre/Post Processor, Assertion) as
log.info("foo = " + bsh.shared.myMap.get("foo"));
2014/07/22 10:06:48 INFO - jmeter.util.BeanShellTestElement: foo = bar
See How to use BeanShell: JMeter's favorite built-in component guide for more details on Beanshell scripting in Apache JMeter and a kind of Beanshell cookbook.
If you use Beanshell for "heavy" operations I would recommend considering switching to JSR223 Sampler and Groovy language as in that case you'll get performance comparable to native Java code.

Resources