I have 2 type script in every request now. Is it possible to make A function and B function and decide what script will use in script?
Now:
Request1
|_ ScriptA
Request2
|_ ScriptB
Request3
|_ ScriptA
I want
Common Script A
Common Script B
Request1
|_ Call Common Script A
Request2
|_ Call Common Script B
Request3
|_ Call Common Script A
If you add a Post/Pre processor it'll be executed after/before each request:
Pre/Post Processor Script
Request1
Request2
Request3
Post-Processors are applied after samplers. Note that they are applied to all the samplers in the same scope,
If you use JSR223 script you can use same file name so you will write code once (DRY principle)
Script File Name of a file to be used as a JSR223 script
Depending on what you're trying to achieve:
Beanshell PreProcessor will be executed before request
Other Beanshell Test Elements (Post-Processor, Assertion, Listener) will be executed after the request
All the aforementioned Test Elements obey JMeter Scoping Rules
If you want the "function" to act as a separate request and record the execution time into .jtl results file - go for Beanshell Sampler, you can put it under a Test Fragment and reference it in Module Controller to avoid code duplication
Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting so it makes sense considering migration
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 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
In jmeter, I'm using nested loop controllers, along with some JSR223 postprocessors inside each loop
Here's the order the script is written:
Loop Controller
Loop Controller
http request
Endloop
JSR223 postprocessor
Endloop
I want the script to first run the http request (multiple times), then after that looping is complete, run the JSR223 postprocessor, then repeat all that.
Instead, what is happening is it enters into the first Loop Controller, then runs the JSR223 postprocessor, then runs the second nested Loop Controller.
Why? How do I get it to run the script in the order in which it's written from top to bottom and nested?
Change from using postprocessor , which is executed for every request in scope, to sampler which executed once
Some elements in the test trees are strictly hierarchical (Listeners, Config Elements, Post-Processors, Pre-Processors, Assertions, Timers), and some are primarily ordered (controllers, samplers).
Another option is to add it under Sampler which will execute it omce after sampler
JSR223 PostProcessor is being executed after each Sampler in its Scope, in your case after each iteration of the HTTP Request sampler.
If you want to run it only once - either put Flow Control Action sampler at the place of the JSR223 PostProcessor and make the JSR223 PostProcessor a child of the Flow Control Action sampler.
Otherwise you can use JSR223 Sampler instead of the JSR223 PostProcessor, if you don't want the JSR223 Sampler to appear in test results - put SampleResult.setIgnore() function somewhere in your script.
Here is my scenario:
I created a test fragment for a sampler which is being used in many thread groups present in different jmx scripts. I sometimes would like to extract few values of this sampler result using few post processors.
Question:
How do I group and make these post processors reusable? I do not want to include as part of the test fragment itself as I don't need/want to execute post processor action every time.
Here is what I have tried:
I am able to save those post processors as a separate test fragment and include it in my test script right after the test fragment with the sampler whenever I want to execute them. I can save the sampler result to a jmeter variable and use it inside my post processor test fragment.
Is this a good approach? Please guide me.
Having Post-Procesors at the same level as all other Samplers is not a very good idea as they will be executed for each Sampler in their scope
Saving response data into a variable each time is also an overhead as according to your question you need the value sometimes
I would recommend using JSR223 Sampler to copy previous sampler response data and apply necessary Post-Processor(s) to it as child(ren).
The relevant code to copy the previous sampler response data would be as simple as:
SampleResult.setResponseData(ctx.getPreviousResult().getResponseData())
Where:
SampleResult - stands for current SampleResult
ctx - stands for JMeterContext
Check out Apache Groovy - Why and How You Should Use It article to learn more about Groovy scripting in JMeter conctept
The JSR223 Sampler can be saved as a Test Fragment as well.
Adding to #Dmitri T answer, in JSR PostProcessor you can save similar code in script file and reuse it
Script file A file containing the script to run, if a relative file path is used, then it will be relative to directory referenced by "user.dir" System property
Use the same script file in several post processors for re-usability:
I have a BSF sampler that runs a groovy script.
I want to create multiple sample results out of this single script in jmeter.
Is it possible?
...my groovy script will run a junit suite and i want to report each junit test separately. Or my groovy script will make 100 http get and i want to display 100 sampler results.
To achieve this you have to organize loop - e.g. put your BSF Sampler under any suitable logic controller: Loop Controller), While Controller, ForEach Controller, or use possibilities of Thread Group (Loop Count, Number of Threads fields).
This scenario will re-use single instance of your BSF Sampler for each new iteration / thread and produce separate sampler result for each execution.
As well you possibly have to provide different entry data / params to BSF Sampler on each iteration - to parametrize it. In this case you can look at least into CSV Dataset Config (to read from file) or any of postprocessors/extractors (go get data from responses).
Suppose you also look into JSR223 Sampler to use along with groovy scripts for better performance.