Jmeter result configuration to use grafana - jmeter

I have a transaction controller and in that 5 requests are there. Now when i run a load test and in my script "Generate parent sampler" is checked. So here what is happening is whenever a failure occurs in any of the requests within this sampler, the transaction controller shows a message like "None" or "Number of sampler" etc.
I am using Grafana to monitor my test results, so how can i show the failed response message of a failed request to a transaction controller first and then show the message in grafana

JMeter's Backend Listener doesn't send response message to Grafana, you can see the possible values in the Metrics Exposed chapter.
The only way to send the response message to Grafana is changing Transaction Controller label using JSR223 Test Elements and Groovy language.
Example code for the JSR223 Sampler:
SampleResult.setIgnore()
def failure = prev.getParent().getSubResults().find { subResullt -> !subResullt.isSuccessful() }
if (failure != null) {
failure.getParent().setSampleLabel(failure.getParent().getSampleLabel() + ': ' + failure.getResponseMessage())
}
Demo:

Related

missing items in jmeter result tree

here is structure of my test.
recording controller
transaction controller
homepage (transaction controller)
HTTP Request 1
Response Assertion (response code = 200)
HTTP Request 2
Response Assertion (response code = 200)
HTTP Request 3
Response Assertion (response code = 200)
....
login (transaction controller)
HTTP Request 1
Response Assertion (response code = 200)
....
logout (transaction controller)
HTTP Request 1
Response Assertion (response code = 200)
....
View Results Tree
Test is placed inside Ultimate Thread Group (up to 20 threads at once). I placed into each HTTP Request Response Assertion.
When run the the test and after that look in the result tree everything is green, so OK.
But when I clicked through result tree I recognised, that sometimes some items are
missing, but not every time.
see: for example:
transaction controller
homepage
login
(logout is missing !!!)
Question is why?
With Ultimate Thread Group you don't have fixed number of loops/iterations, so if you specify some "Hold load for" time span and a thread (virtual user) will be shut down "in the middle" of test execution, i.e. somewhere at homepage transaction - it will simply not be able to finish all the remaining requests.
Like
if you set Hold Load For to 1 second - only 1 request will be executed
if you set Hold Load For to 5 seconds - a couple of requests will be executed
if you set it to 5 minutes - most probably all the requests will be executed at least once, but the number of the requests will mostly depend on your application response time
So if you want the whole sequence to be executed - switch to "normal" Thread Group and specify the desired number of iterations
Also using JMeter GUI mode and View Results Tree listener for executing load tests is not the best idea, you should be running your JMeter tests in command-line non-GUI mode and use HTML Reporting Dashboard for results analysis.

How to run the request based on previous request response data

I have test plan with following request in sequence with 2 loops.
ApiHttp request
Mqtt request
I need to run 1st loop & verify API reaponse is true or false:
If it's true, run Mqtt request , else start the 2nd loop and same verify the API response, if it's true run Mqtt else stop.
In above scenarios, I put beanshell to read the API response:
vars.put("response", new String(data));
But don't know how to verify of its true or false & execute the Mqtt. Any help pls.
Add If Controller as the very first test element under the Thread Group and use the __groovy() function as the condition:
${__groovy(vars.getIteration() <= 2,)}
Put ApiHttp request as a child of the If Controller
Add another If Controller after the ApiHttp request and use the following __groovy() function as the condition:
${__groovy(ctx.getPreviousResult().getResponseDataAsString().contains('true'))}
Add Mqtt request as a child of the If Controller
Add Flow Control Action sampler after the Mqtt request and configure it like:
First switch from Beanshell to JSR223 Test elements and use Groovy.
Use If Controller and just put in it:
${response}
To stop the test based on iteration number, be aware that a Loop Controller exposes iteration as a variable based on name of variable, for example if your LoopController is named LC, then you can access iteration as:
vars["__jm__LC__idx"].toInteger()
And to stop a test from Groovy using JSR223 Assertion you can throw:
throw new org.apache.jorphan.util.JMeterStopTestException();
See:
https://jmeter.apache.org/api/org/apache/jorphan/util/JMeterStopTestException.html

How to check the response assertion is passed or failed in Jmeter using beanshell

I want to save the request and response of the sample into a text file, if response assertion is failed.
Here I can save the request and response it to a text file. But I am struck where,
how to check the whether response assertion is passed or failed for the respective sample.
How to validate the is current "Http Request" sample is passed or failed?
Thanks In advance.
Add Beanshell Listener as a child of your HTTP Request sampler which has Assertion(s)
Put the following code into "Script" area:
import org.apache.jmeter.assertions.AssertionResult;
for (AssertionResult assertionResult : sampleResult.getAssertionResults()) {
if (assertionResult.isFailure()) {
//do what you need here
}
}
As Listeners are executed after assertions the listener will have information regarding all assertion results therefore you will be able to conditionally store request/response details into a file.
Be aware that starting from JMeter version 3.0 the Groovy scripting engine is being shipped with JMeter so consider switching to JSR223 Listener and replacing all your Beanshell Test Elements with the JSR223 equivalents, performancewise it would be much better.

JMeter- Sending one email for multiple http responses

I am using csv for http requests and sending emails in case of failure. Can I send only one email at the end of http requests from csv file. Like if 10 http request failed then I want to send a single email containing urls of all pages for which response was 404 or a failure response returned by server. Image Attached
Add JSR223 Listener to the main Thread Group
Put the following code into "Script" area:
if (!prev.isSuccessful()) {
StringBuilder builder = new StringBuilder()
def failures = props.get("failures")
if (failures != null) {
builder.append(failures)
}
builder.append(prev.getUrlAsString())
builder.append(System.getProperty("line.separator"))
props.put("failures", builder.toString());
}
Add tearDown Thread Group with 1 virtual user and 1 loop to your Test Plan
Add If Controller to this tearDown Thread Group
Use ${__groovy(props.get("failures") != null,)} as the If Controller's condition
Put your SMTP Sampler as a child of the if controller
Use ${__P(failures,)} in the mail body - it will hold failed samplers URLs.
Assuming above configuration JMeter will send only one email with the list of all failed samplers URLs and only if any sampler has failed. Feel free to amend the code as required.
References:
__P() function
__groovy() function
Groovy is the New Black
I made it simple and moved SMTP Sample under tearDown ThreadGroup and attached Aggregate Report with Email which contains all information which I want to send in email.
Also In Aggregate Report I selected option that show only Errors which made this report more concise.
Thanks for Answers. #Dmitri T
If anyone having issue feel free to consult.

How do I resend a sampler upon a failed assertion on JMeter?

I am designing a load test in JMeter.
With the current application that we have whenever a HTTP request is sent, the web server will very occasionally send back a page with a message. To get around this we just have to reload the page. This page could come up for literally any HTTP request.
Is there any way to design a test in JMeter where when a sampler fails, the sampler simply retries?
I'm not sure how I can get a Beanshell sampler to resend a HTTP request.
It is possible via additional Beanshell Assertion
You can re-run arbitrary sampler from the Beanshell Assertion as simple as
ctx.getCurrentSampler().sample(null);
Add a Beanshell Assertion after all other assertions. It matters as assertions are being executed upside-down.
Put the following code into Beanshell Assertion's "Script" area (just change "message" to what your server returns on error.
import org.apache.jmeter.samplers.SampleResult;
if (new String(ResponseData).equals("message")) {
SampleResult result = ctx.getCurrentSampler().sample(null);
if (result.getResponseDataAsString().equals("message")) {
Failure = true;
} else {
SampleResult.setSuccessful(true);
}
}
You'll have only one result recorded.
If assertion passes 1st time - it'll be successful
If assertion fails 1st time and passes 2nd time - it'll be successful
If assertion fails 2 times - it'll be recorded as failed.
For extended information on Beanshell scripting check out How to use BeanShell: JMeter's favorite built-in component guide.
Create such hierarchy:
Thread Group (1 user, 1 second ramp-up, forever)
-While Controller (empty condition = forever)
--Counter (start – 1, increment – 1, reference name – counter)
--HTTP request
---Timer (I prefer constant Timer, responseble for pause betwee retrying)
---BeanShell Post Processor
BeanShell Post Processor should contains(pseudo code):
if(Integer.parseInt(vars.get("counter")>5)
{
prev.setSuccessful(false);
prev.setStopTestNow(true);
}
if(successCondition)
{
prev.setStopTest(true);
}
There is no direct way to achieve it, but I think you can use While controller in conjuction with Regex extractor to resend the failed requests.
Logical flow could be,
1. HTTP request
2. Regex extractor post processor - check response contains failure extract value in msg variable, default is success
3. While controller - run till msg=failure, default value of msg is success
Example screenshot,
Let me know if this works.

Resources