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

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.

Related

Threads are in blocked state when JMeter script is executed [non-gui mode]

I am running the JMeter script with 100 RPS/TPS using Throughput Shaping Timer on Linux VM using Non-GUI mode, as I am not able to reach the desired TPS/RPS with enough RAM and CPU resource available.
I took the ThreadDump and saw that 195 Threads out of 200 Threads are in Blocked State. Thread Dump Analysis is available on:
Thread Dump Analysis
This is the API Script which needs dynamic headers generation before each Request is executed. Dynamic headers are as follows:
contentMD5 - MD5 hash of request body
client - clientTypeAPP
nonce - unique current timestamp
apikey - sha512Hmac of the string generated by concatenating Methods, body, path, md5 etc.
The above headers are generated using the JSR223 Pre Processor. The generated headers are also removed using JSR223 Post Processor after Sampler execution.
The thread dump shows that the problem is with your JSR223 Test Elements, most probably there is an issue with your Groovy code and most probably you're inlining JMeter Functions or Variables there
Make sure to remove all occurrences of calls to functions or variables from the code
Try using script files rather than putting your code into JSR223 test elements
Use a profiler tool to get to the bottom of your code issues
If you hit the limits of the Groovy scripting engine performance consider either moving your custom code logic into a JMeter Plugin or switching to Distributed Testing

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 use _time in consecutive request

i want to create a unique data with time function. below is the code.
Want to use the same unique data in multiple request.
Step1 - JSR223 PostProcessor
long currentTime = ${__time(,)}
Step-2 http request-1
{ "userId": "PerfTesting_${currentTime}"}
Step-3 http request-2
{ "userId": "PerfTesting_${currentTime}"}
But i don't see this is substituting the currenttime variable in requests
Apparently, you have checked the Cache compiled script if available
Solution 1
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.
Hence you need to pass the ${__time(,)} as a parameter
Solution 2
Just uncheck the Cache compiled script if available in your JSR223 Post Processor
Solution 3
You can use User Parameters Pre-Processor to define the time stamp once per iteration
Don't inline JMeter Functions or Variables into 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.
So the options are in:
Move your __time() function to "Parameters" section and refer it as Parameters in the Groovy script like
Use System.currentTimeMillis() instead of __time() function like:
You can avoid Groovy scripting at all and just use __time() function:
http request-1 { "userId": "PerfTesting_${__time(,currentTime)}"}
http request-2 { "userId": "PerfTesting_${currentTime}"}
More information on Groovy scripting in JMeter: Apache Groovy - Why and How You Should Use It

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.

Resources