Authorization request run every x minutes, while load test continues - jmeter

I have an API that needs the authorization header - auth expires after 10 minutes
API to be load tested just needs to run the auth header request every 10 minutes - and pass that auth token. I have the variable set up correctly, but am unable to get the auth to re run at 10 minutes
This is very similar but is different enough that I am asking my question
This is the logic in the JSR223 Post Processor
def start_Time = vars.get("startTime")
log.info "start_Time>>>>>>>"+start_Time
long currentTime = ${__time(,)}
log.info "currentTime>>>>>>>"+currentTime;
long diff=currentTime.toLong()-start_Time.toLong();
log.info "diff>>>>>>>"+diff
if(diff>=100){
start_Time = ${__time(,)};
vars.put("startTime",start_Time.toString());
}
vars.put("flag",diff.toString());
log.info "FlagValue>>>>>>>"+vars.get("flag")
which doesn't seem to be correct, I have seen some threads on this, but haven't been able to find the problem with the above.
Logic in the If Controller
${__groovy(vars.get("flag").toInteger() >= 100)}

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.
Your __time() function call returns the same value each time, you need to replace it with System.currentTimeMillis() function
So I believe your code needs to be changed to look like:
def start_time = vars.getObject('startTime') ?: vars.get('TESTSTART.MS') as long
def currentTime = System.currentTimeMillis()
def diff = currentTime - (start_time as long)
if (diff >= 100) {
vars.putObject('startTime', System.currentTimeMillis())
}
vars.putObject('flag', diff)
More information on Groovy scripting in JMeter: Apache Groovy: What Is Groovy Used For?

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 - How can I pass 2 condition in a while loop on Jmeter

How can I pass 2 condition in a while loop on Jmeter. The conditions are
The request should run in loop till "Pass" response comes.
While loop should run only for 1 minute.
Condition 1 is working fine. However condition 2 is unable to implement.
I have tried running the While Loop inside a Runtime Controller. But the issue is, if the response "Pass" comes before 1 min, the rest of the test stops.
Tried other way round (Runtime inside While Loop) leading to numerous execution of the request, even after receiving "Pass" response.
Will appreciate any leads on this. Thanks
Add a JSR223 Sampler just before the While Controller and store the current time into a JMeter Variable using the following code:
SampleResult.setIgnore()
vars.putObject('whileLoopStart', System.currentTimeMillis())
Use the following __groovy() function as the While Controller's condition:
${__groovy(!vars.get('your_variable').equals('Pass') && ((System.currentTimeMillis() - vars.getObject('whileLoopStart')) < 60000),)}
This way the While Controller will run until:
either your_variable is not equal to Pass
or 60 seconds pass
whatever comes the first
More information on Groovy scripting in JMeter: Apache Groovy - Why and How You Should Use It
This could be another solution.
You can achieve the desired outcome with the following components.
Runtime Controller
If Controller
Flow Control Action
Set the Runtime (duration) in the Runtime Controller
Set the first condition you already have in While Controller in the If Controller
Click the Break Current Loop to exist from the Run Time controller

How to log the current time in JMeter Webdriver sampler

I want log the current time to the JMeter log from the JMeter Webdriver sampler.
I am using the below code in the Webdriver sampler which is logging the time in milliseconds but the problem is my Webdriver sampler has a waiting time of 2 minutes. The getStartTime() and getEndTime() functions are considering the sampler waiting time also.
Is it any other way to get the current time from the Webdriver sampler? I want to measure the time taken between two actions in the browser.
WDS.log.info(WDS.sampleResult.getStartTime())
WDS.log.info('WDS.sampleResult.getEndTime())
You can record start and end time using Date.prototype.getTime() function like:
var before = new Date().getTime()
// here is the code which duration you would like to measure
var after = new Date().getTime()
WDS.log.info('Time taken = ' + (after - before) + ' ms')
Demo:
See The WebDriver Sampler: Your Top 10 Questions Answered for more WebDriver sampler tips and tricks
Exclude the wait time by covering only the required code block with the start time and end time function.
Ex:-
WDS.log.info(WDS.sampleResult.getStartTime())
---some code
WDS.log.info('WDS.sampleResult.getEndTime())
Wait for two minutes..wait(2)
WDS.log.info(WDS.sampleResult.getStartTime())
--some code
WDS.log.info('WDS.sampleResult.getEndTime())
In this way, wait time is excluded from the calculation.
This is not the best solution but one which I can think of.

Can you track how long controller was running? jmeter

I'm doing some testing with while controller.
In my test it checks every 3 seconds for a correct response text and if condition is not met, it repeats.
I would like to track how long that while controller was running, repeating itself until the condition was correct.
Is there any way to do that?
You do it via JMeter Functions, to wit:
__time() - to get the current timestamp before entering the While Controller
__longSum() - to calculate the delta between 2 timestamps
Something like:
Before the While Controller starts put the following expression somewhere in your script:
${__time(,before)}
It will store the current timestamp into ${before} JMeter Variable
After the While Controller ends put the following expression somewhere in your script:
${__longSum(${__time(,)},-${before},)}
It will get the new current timestamp and calculate the delta by substracting ${before} variable value from it
Demo:
See How to Use JMeter Functions posts series for more information on using JMeter Functions.
you can use Counter and Bean Shell Post Processor.
Keep Counter Config Element inside the While Controller. The value of Count will be incremented in each iteration.
So, in if condition, you can add BeanShell Post Processor to the sampler inside If controller, and calculate the total time taked using the following formula:
count value * 3 = total seconds waited to reach If Condition.
Following is the placement of elements in the TestPlan:
While Controller
...Counter
...If Controller
......HTTP Sampler
.........Bean Shell Post Processor
Image reference:
Counter Configuration screenshot:
Bean Shell Post Processor code:
int count = Integer.parseInt(vars.get("count"));
int totalTime = count*3;
log.info("total time waited : " + totalTime);
vars.put("totalTime", totalTime);
Screenshot:
Note: the value is saved in totalTime variable, so you can refer the value in subsequent requests using ${totalTime}

Jmeter - how to get SLA metric

Does exists any way to calculate count of requests under SLA in jmeter from UI? For example, count of requests that response time < 400 ms?
I had a similar problem a while ago and wrote a little tool - see https://github.com/sgoeschl/jmeter-sla-report
Simplest solution is to use Simple Data Writer to save Label, Elapsed Time and / or Latency to a CSV file, which will generate raw output like this:
elapsed,label
423,sampler1
452,sampler2
958,sampler1
152,sampler1
And from here you can take it to any other tool (awk, Excel, etc.) to filter results you want.
Another option is to use BeanShell Listener to generate such report on the fly. Something like this:
long responseTime = sampleResult.getEndTime() - sampleResult.getStartTime();
if(responseTime < 400) {
FileOutputStream f = new FileOutputStream("myreport.csv", true);
PrintStream p = new PrintStream(f);
this.interpreter.setOut(p);
print(sampleResult.getSampleLabel() + "," + responseTime);
f.close();
}
This method, though, may not be performant enough if you are planning to run a stress test with many (more than 200-300) users and many operations that "fit" the filter.
JMeter provides OOTB a Web Report that provides tons of informations regarding your load test using standard metrics like APDEX, Percentiles ...
See this:
http://jmeter.apache.org/usermanual/generating-dashboard.html
If you still want this, do the following:
Add as a child of your request add a Duration Assertion:
All response below it will be marked as failing.
And in the report, you'll have the count of successful requests meeting this SLA criterion.

Resources