JMeter provides a simple HTTP server, the HTTP Mirror Server (https://jmeter.apache.org/usermanual/component_reference.html#HTTP_Mirror_Server), which causes JMeter to simply mirror back any request that is sent to it.
Instead of mirroring a given request, is there a way for JMeter to accept a request and then, based on the request, execute a series of actions?
You can use i.e. a Beanshell Sampler which allows execution of arbitrary Beanshell (or Java) code so you should be able to develop logic parsing incoming request and conditionally switch to this or that actions branch.
Test plan outline
Thread Group
Beanshell Sampler
If Controller (condition 1)
action 1
action 2
If Controller (condition 2)
action 3
action 4
In Beanshell Sampler place custom code which will listen to incoming HTTP connections and set a JMeter variable basing on outcome via vars.put method
In the If Controller you can put a condition checking variable value like
In "If Controller (condition 1)" - "${myVariable}"=="foo"
In "If Controller (condition 2)" - "${myVariable}"=="bar"
See How to use BeanShell: JMeter's favorite built-in component for detailed information on Beanshell scripting and pre-defined variables and If Controller documentation entry for information on setting correct conditions.
Related
I am new to jMeter so hope you can help me out.
I have a thread group with several HTTP request samplers. I am sending an email every time any sampler fails. The thing is I would like to collect the names + response bodies of the several failed samplers to send in the email.
Can anybody please give an example of how to do it?
The best solution would be using Simple Data Writer listener configured to save only failed requests, there you can choose whatever metrics you need to store:
Another option is using JSR223 test elements and Groovy language to extract the "interesting" fields from the failed requests:
in the above example the sampler gets the label and the body from the previous result and stores them into ${requestName} and ${responseData} JMeter Variables which you can use wherever you want. More information on these prev, vars and other JMeter API shorthands: Top 8 JMeter Java Classes You Should Be Using with Groovy
USing WS plugin -- For single request ,server returns multiple responses in my application.
This is a gaming application. For game play request, it sends multiple response in return based on game logic.The value of attributes in response (EX: {"server":{"event":"broadcast","broadcastaction":"gamevents" --attribute broadcastaction changes)changes according to game flow. sometimes ,broadcastaction attribute is not there in response.
Multiple conditions to be checked in my response.
So i have to capture all response or i have to check whether the specific value is present in response using multiple if conditions in jsr223 (java) sampler.
Below is a example for response which changes as mentioned below based on game flow
Ex:
1.{"server":{"event":"***","tr":"0"}
2.{"server":{"csh":0.0,"id":"3","action":"$$$$","value":"normal######"}
3."server":{"csh":0.0,"id":"0","action":"******","score":"-*","count":"1"},{"csh":0.0,"id":"0","action":"###","value":"***"}]}}
4."server":{"csh":0.0,"id":"0","action":"******","score":"-*","count":"1"},{"csh":0.0,"id":"0","action":"###","value":"***"}]}}
I think you're looking for a JSR223 Assertion and Groovy language as:
Groovy has built-in JSON support
It has handy shorthands to JMeter API allowing access to response data and result manipulation so you can conditionally mark sampler as passed or failed
It's the most performing scripting option comparing to other available languages
More information: Scripting JMeter Assertions in Groovy - A Tutorial
I have the while controller (as long as a flag is false) which has multiple IFs like:
I need to set the flag once certain conditions are satisfied at various places. Where do I need to place the stand alone JSR223 sampler to reset the flag? The locations I have placed it as highlighted is throwing error ('Method getCookieManager() not found in class...' ) as my JSR223 sampler doesn't have an instance of the HTTP Cookie Manager because I have brought the cookies from the one-off setUp thread group to main thread group (payment - via BS PreProcessor).
If you're using Beanshell PreProcessor and Beanshell PostProcessor for passing cookies between setup and "normal" thread groups - you need to change their location, to wit:
Put Beanshell PostProcessor as a child of the 02 LOGIN sampler
Put Beanshell PreProcessor as a child of the 04 GET PAYT sampler
as soon as you do this all getCookieManager() not found in class errors will go away
Consider migrating from the Beanshell Pre and PostProcessor to the JSR223 equivalents as they may become performance bottleneck when it comes to high loads, see Apache Groovy - Why and How You Should Use It article for more details
I am creating a JMeter test plan with multiple HTTP requests.
To track request which failed, got answer here about using JMeterThread.last_sample_ok.
As there are multiple requests I need to add this Beanshell sampler after each of the HTTP request to flag off failed request.
Is there a way to set a flag if any of the HTTP request fails in a given thread?
You can add BeanShell Listener or JSR223 Listener and add your code for failure under the if statement:
if (vars.get("JMeterThread.last_sample_ok") == "false") {
....
}
JSR223 Listener will execute code after each Sampler and check if failed.
You don't need the Sampler, you need the Listener. Listeners are obeying Scoping Rules therefore if you put a listener at the same level as all your requests (or higher) it will be applied to all of them.
You don't need Beanshell, it is a kind of performance anti-pattern, since JMeter 3.1 users are encouraged to use JSR223 Test Elements and Groovy for any scripting tasks. Check out Apache Groovy - Why and How You Should Use It article for more information, benchmarks, Groovy best practices, etc.
Example code you could use would be something like:
if (!prev.isSuccessful()) {
log.info(sampler.getThreadName() + ' ' + sampler.getName() + ' has failed')
}
Demo:
My script structure looks like that:
Transaction Controller SEARCHING OFFERS Transaction
http request offers
http request offers details
Beanshell PostProcessor
Which BeanShell command let me to get name, response code, response time, test result and test time for whole transaction?
Where I should attach BeanShell PostProcessor?
Which code I should use, but in my opinion
String name = sampler.getName();
doesn't work correctly for me.
IMO you should be using BeanShell Listener instead of Beanshell PostProcessor. Listener can be at the same place where your current Beanshell PostProcessor is. That object has access to sampleResult, which would contain response code / response time / etc, for example:
sampleResult.getSampleLabel(); // the name, e.g. 'SEARCHING OFFERS Transaction'
Full list of SampleResult functions is here
If you only want to process transactions in this listener, you could filter them (e.g. by name).