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.
Related
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.
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
My test configuration :
Loop Controller
--> Beanshell Sampler
vars.put("test", "${__CSVRead(*test,0)}");
Add it to an existing array retrieved from vars.getObject
In the above scenario, I am constructing my request payload dynamically in a loop controller. I had to put the CSVRead function in a separate Beanshell sampler under the loop controller since "${__CSVRead(*test,0)}" was reading the sample line if I use it within a for loop inside the beanshell sampler (interpreted mode).
While the above configuration meets my requirement, my *.jtl files are growing in size even for a 30 minute load test since the BeanShell sampler is getting measured all the time. While I am able to filter the required data by using the FilterResults tool, I want to know how to avoid this during the execution itself like the TestActionSampler
Use one of the following Test Elements instead:
BeanShell PreProcessor
BeanShell PostProcessor
Beanshell Timer
By default Timers and Pre/Post Processors execution time is not included into parent sampler elapsed time (unless you use Transaction Controller explicitly configured to do so), using this approach you will be able to exclude the time, required for constructing payload from test results.
I resolved it by using the following configuration.
Loop Controller
--> Test Action Sampler
--> Beanshell timer returning 0 at the end
vars.put("test", "${__CSVRead(*test,0)}");
Add it to an existing array retrieved from vars.getObject
return 0;
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.
I have a test plan in jMeter that requires some parameters that needs to be calculated before running the test. In order to calculate these parameters, I created a JSR223 PreProcessor directly under test plan as seen below.
My problem is PreProcessor seems to run before every request which is not what I want. I need to calculate these parameters only once and use them in testing.
Is there a way to run the JSR223 PreProcessor only once, or should I use another method?
Thanks in advance.
Edit:
As #ubik-load-pack suggested, I tried "setUp Thread Group" as following but variables created in the code was not available under "Thread Group". They were also present neither in the logs (logging is used in the code) nor in the View Results Tree (via Debug PostProcessor)
I also tried "Once Only Controller" which also didn't work, same as above.
For more information here is content of my JSR223 PreProcessor. (Not the whole code, there will be more variables here so using date functions is not a solution for me by the way.)
By design a PreProcessor runs before any Sampler runs.
So if you want to run something only once per user, you can do the following:
Use a Once Only Controller and put in it a JSR223 Sampler that will contain your code.
If you want to do it once for all users, then use a setupThreadGroup
that will contain your JSR223 Sampler and configure it with 1 thread. It will run once before the regular Thread Groups are started.
EDIT after you updated your question:
As I wrote, you cannot use the setupThreadGroup approach if you want to reuse variables in Thread Groups so stick to OnceOnlyController approach for your request
With the Once Only Controller it is not working because you misread my answer, I am suggesting to use a JSR223 Sampler not PreProcessor as a preprocessor will run only if there is a sampler that runs.
If you use a Sampler, and you have multiple ThreadGroups, then you'll have to copy that sampler into each ThreadGroup, since you can't put Samplers outside of ThreadGroups.
you can use jsr223 PreProcessor with:
if(vars.get("init")=="OK") return;
vars.put("init","OK")
//your code
I'm doing a slightly dirty workaround at the top of the preprocessor:
log.info("in PreProcessor. Sampler name: " + sampler.getName())
if (sampler.getName() != "HTTP Request") {
log.info("not running again")
return
}
In my case I cannot add another sampler because it will mess up my timer calculations.