Stopping Jmeter test when all users in user.csv are done - jmeter

I am working on a test plan in Jmeter.
I am using a concurrency Thread Group for my test and I am using a user.csv file to pass my users through it. I have set the Recycle on EOF as "False" and Stop thread on EOF as "True" as I want all the users to work only once. I set some time on "Hold Target Rate Time".
My problem is sometimes all the users are done and leaves but test doesnt stop and keeps going on as the time I have set on "Hold Target Rate Time" is more then the actual time all users took and I keep getting the this message in log file "INFO o.a.j.t.JMeterThread: Stop Thread seen for thread bzm - Concurrency Thread Group 1-8480, reason: org.apache.jorphan.util.JMeterStopThreadException: End of file:user.csv detected for CSV DataSet:users configured with stopThread:true, recycle:false
".
I want to stop the test as soon as all users are done. How should I do it.
I am using a Beanshell sampler with this code:
if(${__threadNum} == 0)
{
ctx.getEngine().askThreadsToStop()
}
But its not helping.
Any suggestion?

Configure your CSV Data Set to be like:
Add If Controller to your Test Plan and put the following expression into "Condition" area:
${__groovy(vars.get('foo').equals('<EOF>'),)}
Add JSR223 Sampler as a child of the If Controller and put the following code into "Script" area:
SampleResult.setStopTest(true)

I simply added a JSR223 PreProcessor and added following code.
if(("${LOGIN}" == "END")) //Login is defined in USER.csv
{
long startTime = System.currentTimeMillis(); //fetch starting time
while((System.currentTimeMillis()-startTime)<7200000) //Defining sometime to
wait let threads end gracefully.
{
// Waiting for the Defined time.
}
//println("---Stopping---"); // // Testing Purpose
ctx.getEngine().askThreadsToStop();
}

Related

How to add timer in jmeter script, which we can start at first call, poll the status & stop once the first request is completed & add assertions

I am doing load testing on generating report and the requirement is like the report should get generated within 10mins.
It includes one HTTP post request for report generation, and then there is a status check call, which keeps on checking the status of the first request. Once the status of first request changes to complete then the report generation is successful.
Basically I want to start the timer at the begining of the first request and stop the timer once the status is complete and need to add assertion if the time is less than 10 mins then test is pass else fail.
I tried multiple approaches like using Transaction controller, and adding all request under it. But this doesn't give sum but the average response time of all the request under it.
Also, I tried beanshell listener, extracting the response time for every request and adding them all...
var responseTime;
props.put("responseTime", sampleResult.getTime());
log.info(" responseTime :::" + props.get("responseTime"));
log.info("time: "+ sampleResult.getTime());
props.put("responseTime", (sampleResult.getTime()+props.get("responseTime")));
log.info("new responseTime :::" + props.get("responseTime"));
However, I am not interested in adding the response time of these requests, instead I need to just know what is the time elapsed from when the report is triggered and till it gives status as complete.
All the jmeter timers are adding delays, I dnt wish to add delay instead I need it as a timer.
Any help is highly appreciated.
Thank you
Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting mainly due to performance reasons so I'll provide one of possible solutions in Grovy
Add JSR223 PostProcessor as a child of the HTTP Request which kicks off the report generation and put the following code there:
vars.putObject('start', System.currentTimeMillis())
Add JSR223 Sampler after checking the status and put the following code there:
def now = System.currentTimeMillis()
def start = vars.getObject('start')
def elapsed = now - start
if (elapsed >= 600000) {
SampleResult.setSuccessful(false)
SampleResult.setResponseMessage('Report generation took: ' + (elapsed / 1000 / 60) + ' minutes instead of 10')
}
Example setup:

Jmeter: Failing to execute next component after while controller(using CSV data config also)

I have a jmeter test plan with while controller, which loops over URLs from a CSV(CSV contains only URLs):
However the next component 'HTTP Request' never gets executed. in log viewer, I can see this:
2021-02-24 20:23:29,317 INFO o.a.j.t.JMeterThread: Stop Thread seen
for thread Thread Group 1-1, reason:
org.apache.jorphan.util.JMeterStopThreadException: End of
file:C:/Users/Administrator/Downloads/jmeter/urls.csv detected for
CSVDataSet:CSV Data Set Config configured with stopThread:true,
recycle:false
If I set stopThread to False, then this controller never stops. Is there any solution to this?
While controller is configured as follows:
You need to set both "Recycle on EOF" and "Stop thread on EOF" to false because:
if you will be recycling on EOF - the while controller will never stop
if you stop thread on EOF - no elements after the While Controller will be executed
So
Configure your CSV Data Set Config like:
Put your "OWA Inbox Access" sampler under the If Controller and use the following __jexl3() function as the condition:
${__jexl3("${myVar}" != "<EOF>",)}
More information: Using the While Controller in JMeter, see Reading all Values from the CSV and Continue chapter

JMeter - Disable/Ignore assertion only for first request

I have a Thread Group with 10+ requests in same hierarchy.
I added Duration Assertion for all and it's working fine, except in 1 case:
If server is upload before test, first request failed due long duration caused by a server startup delay.
How can I ignore assertion in first request on the first execution?
You can add a JSR223 Listener to your Test Plan and use the code like:
if (prev.getSampleLabel().equals('First Sampler') && vars.getIteration() == 1) {
prev.setSuccessful(true)
}
It will mark the sampler with label of First Sampler as successful if it fails during first Thread Group iteration.
prev stands for SampleResult class instance, check out JavaDoc for available functions and properties. For example you might be interested in getAssertionResults()

How to stop Jmeter test after specified number of threads failed

I have one Transaction controller which has one http request in my Jmeter test plan. Transaction name and url comes from CSV file. At the end total execution is divided into 5 different transactions.
Testplan:
Testplan
-Thread Group
- User defined variable
Total sample execution will be 8000-10000. Now what i want, if total sample failures reached to 100, my JMeter test should stop test execution.
I have added User defined variable name "thread" and with value "0". I have added below code in Beanshell Post-Processor
int count= Integer.parseInt(vars.get("thread"));
if (prev.getErrorCount()==1){
count++;
System.out.println(count);
vars.put("thread",Integer.toString(count));
}
if (count==100){
System.out.println("Reached to max number of errors in load test, stopping test");
log.info ("Reached to max number of errors in load test, stopping test");
prev.setStopTestNow(true);
}
Somehow code is not working as expected. When error count reach to 100, Jmeter test is not getting stopped. Test is stopped when error count reached to 130. I am not sure who to fix above code.
Can someone please let me know what is issue in above code?
Variables are specific to 1 thread while Properties are shared by all threads.
See:
https://jmeter.apache.org/api/org/apache/jmeter/util/JMeterUtils.html#getProperty(java.lang.String)
https://jmeter.apache.org/api/org/apache/jmeter/util/JMeterUtils.html#setProperty(java.lang.String,%20java.lang.String)
Ensure you synchronize access
Another option is to use a custom java class as a Singleton and increment its value.
Here an example implementation using Beanshell (Prefer JSR223 + Groovy for performances):
setupThreadGroup that resets the counter on test start:
BeanshellPostProcessor that updates counter:
Note that as you call setStopTestNow, test threads are interrupted but do not stop exactly at this time unless you have some timer (which is usually the case)
prev.setStopTestNow(true);

Beanshell script launched once (start and end of test plan) in JMeter

Good afternoon !
I will try to explain you clearly my problem.
The context
I have a JMeter TestPlan which send HTTP requests to a server. I have a Beanshell script to assert each different case of error returned.
302 response code -> OK
200 response code -> ?
In each error 200, I check the response data string to see if it is an error or a correct case. (User error like User don't have correct rights is OK, but Server is unavailable is ERROR and both have 200 as response code.)
Here is my test plan :
The goal
As I have several errors returned by only one assertion script, I am not able to differenciate each error, except by uncollaspe the assertion in a ViewResultTree. But I disable it when launching my test, and I will launch my TestPlan remotely.
I had the idea to manually count each error. All my samples goes in my Assertion script, and goes to the correct if block according to their content. I increment some variables (JMeter.properties in fact) in each block.
int test = Integer.parseInt(props.getProperty("302"));
test++;
props.setProperty("302", ""+test);
I want to display all those variables in a JFrame at the end of my testplan like this :
The problem
My problem is that I don't know how to launch a Beanshell script before and after the TestPlan.
I want a first script to be started before any sample is send, just to initialize all my properties variables to 0 (else, they keep the value of the last TestPlan).
And, I want a second one to display my Frame with all the variables after the test plan is finished. (Currently it is a JFrame but it will not stay like this.)
Tested solutions
1) For my first script, I set a Counter (JMeter > Config Element > Counter) in the beginning of my test plan to 0.
I use it to check if my test already started of not with an If Controller :
I have a Pre-Processor Beanshell with props.set("302","0"); where "302" is my property to count all 302 response code.
It correctly works but I want to know if there is a proper way to do this.
2) Then, for my second script, I tried to use ${JMeterThread.last_sample_ok} in an If Controller aswell but it doesn't work like I expected. If I put it after my sample, it start after all OK assertion, and if I put it at the end of the test plan, it is never called.
How can I run my beanshell script once, after all my threads are stopped (i.e. all sample finished) ?
Thank you in advance, I hope you understood everything !
JMeter SetUp thread group and TearDown thread group are meant for exactly this.
Add your beanshell component to the setUp thread group to do some setup activities before your actual test starts. Similarly the tearDown thread group runs after your test execution is complete.

Resources