How to call same request in a loop in gatling - gatling

Background:
I want to perform a test where I need to check time taken to transfer data from one api to another api.
for ex:
/API_1 is sending data
/API_2 gets the data from API_1. here not all data is received at once it is coming in chunks and taking time.
So i want to record this delay.
I am trying this
val ExeScn= scenario("CheckDelay")
.exec(http("XX-Post")
.post("src/v1/req")
.header("Authorization", "ABCD")
.body(StringBody(postBody))
.asJSON
.check(jsonPath("$.data.name[*].address[*].property").findAll.is("Completed")
)
How do I apply a loop here?
I want to run scenario("CheckDelay") till below condition
.check(jsonPath("$.data.name.address.property").find.is("Completed")

loop: asAlongAs
condition for the loop: : isUndefined()
check: simple find with a notExists

Related

How to use tryMax in Gatling for a blank attribute, logout and restart the iteration?

I know I can use tryMax to restart a scenario and use .exists() to validate if an attribute exists. How do I incorporate the two together?
If you have checks in an exec inside the tryMax and that check fails, it will try it again from the beginning of the tryMax block.
EX:
val TestScenarioThatRestartsOnFail: ScenarioBuilder = scenario("ScenarioNameHere")
.tryMax(2) {
exec(
http("zzzz")
.get("zzz")
.check(xxxx)
)
.exec(
http("zzzz")
.get("zzz")
.check(xxxx)
)
}
So basically any check that fails in the entire tryMax{} block, will make the entire block run again.
You can also chain multiple tryMax blocks if you need to retry different number of times different requests
val TestScenarioThatRestartsOnFail: ScenarioBuilder = scenario("ScenarioNameHere")
.tryMax(2) {
exec (
http ("zzzz")
.get ("zzz")
.check (xxxx)
)
}.tryMax(2) {
exec(
http("zzzz")
.get("zzz")
.check(xxxx)
)
}
If you encounter checks that are "optional" hence they might sometimes fail, but you don't want to trigger the tryMax for those checks, use the optional method like :
.check(regex("/admin-ng/login.[^\"]*.css").find.optional.saveAs("login_css"))
This will not fail the check if it is not fulfilled, in this example, the element we search for is not in the response.

JMeter looping with indexes and property update

I'm attempting to create a test plan when a certain value is reached, then some functionality happens. The test plan consists of multiple threads running with a loop, and when some condition is reached I'd like to fire an HTTP request .
I'll drill down to the guts of it:
In my test I have logic in a looping way with multiple threads, and when a condition is met (the condition is met every 10 seconds) then I need to iterate through a value that it's value should be saved from the previous iteration - that value which I defined is a property (inside user.properties) - startIndex = 0 (initialized to 0).
So I've made a While Controller which it's condition is like this:
${__javaScript(${__P(startIndex,)}<=${currBulk},)}
And I expect the HTTP request, which depends on startIndex value inside the while to be executed when startIndex<=currBulk variable.
Inside the While Controller the HTTP request should to be fired until all indexes are covered, and I've written it like this inside BeanShell PostProcessor:
int startIndexIncInt = Integer.parseInt(props.getProperty("startIndex")); //get the initiated index of the loop
startIndexIncInt = startIndexIncInt + 1; //increment it and see if needed to fire the request again, by the original While condition
vars.put("startIndexIncIntVar", String.valueOf(startIndexIncInt));
props.put("startIndex",vars.get("startIndexIncIntVar")); //the property incremental and update
So, I designed it like in order that in the next time (after 10 more seconds) I'll have an updated startIndex that will be compared to the new currBulk (which is always updated by my test plan).
And I just cant have it done . I keep receiving errors like:
startIndexIncInt = Integer.parseInt(props.ge . . . '' : Typed variable declaration : Method Invocation Integer.parseInt
Needless to say that also the var startIndexIncIntVar I defined isn't setted (I checked via debug sampler).
Also, my problem isn't with the time entering the while, my problems are basically with the variable that I should increment and use inside my HTTP request (the while condition, and beanshell post processor script)
Just for more info on it, if I'd written it as pseudo code it would look like this:
startInc = 0
----Test plan loop----
------ test logic, currBulk incremented through the test-----
if(time condition to enter while){
while (startIndex <= currBulk){
Send HTTP request (the request depends on startIndex value)
startIndex++
}
}
Please assist
It appears to be a problem with your startIndex property as I fail to see any Beanshell script error, the code is good so my expectation is that startIndex property is unset or cannot be cast to the integer. You can get a way more information regarding the problem in your Beanshell script in 2 ways:
Add debug() command to the beginning of your script - you will see a lot of debugging output in the console window.
Put your code inside try block like:
try {
int startIndexIncInt = Integer.parseInt(props.getProperty("startIndex")); //get the initiated index of the loop
startIndexIncInt = startIndexIncInt + 1; //increment it and see if needed to fire the request again, by the original While condition
vars.put("startIndexIncIntVar", String.valueOf(startIndexIncInt));
props.put("startIndex", vars.get("startIndexIncIntVar")); //the property incremental and update
} catch (Throwable ex) {
log.error("Beanshell script failure", ex);
throw ex;
}
this way you will be able to see the cause of the problem in jmeter.log file
Actually it appears that you are overscripting as incrementing a variable can be done using built-in components like Counter test element or __counter() function. See How to Use a Counter in a JMeter Test article for more information on the domain.

How to handle delay in jmeter based on response code

My requirement was that whenever there was an error in the Jmeter execution, there should be a delay before starting the next iteration.
"Start Next Thread Loop" is selected at the Thread Group level for Sampler error. Due to this Jmeter will start a new Iteration if there is any error during the execution.
For this purpose I have used Beanshell Timer at the start of the Iteration. Following code is added for delay if the response code is anything other than "200"
String code = prev.getResponseCode();
if(code!="200"){
log.info("::::::::::::::::::::::::::::Response Code = "+code);
log.info("sleep for 10sec");
return 10000;
}
else
return 0;
Please let me know if there are any better ways of doing this.
I believe the prev.getResponseCode() can also be used to do any kind of cleanup task, in case there is any error.
Like for example, if a user logs into the application and got an error before doing logout. We can test at the start of the iteration if the previous response code is in error, if so, we can make the user logout of the application.
You can do this using:
If Controller that will check if last response was in error or not using
${__jexl2("${JMeterThread.last_sample_ok}" == "false",)}
Test Action that will be used to Start Next Thread Loop
Test plan would have the following layout:

Loop a Volley request

I want to repeat a volley request more than one time.
Something like:
For (int i=0;i<10;i++){
RequestA{
TextView.setText(TextView.getText + JsonArray[i].toString);
}
}
When i run the program the FOR loop 10 times but the Volley request just one time with i=9, so in the TextView print just the content of the JsonArray[i=9].
So my question is: why also the Volley Request doesn't loop 10 times?
I don't know how you use volley. But, i believe the volley does loop 10 times. The reason why you only see the JsonArray[i=9] is because you are overriding the value in textView. Instead of using textView, try using log to get the value and read from logcat.

AppEngine - Optimize read/write count on POST request

I need to optimize the read/write count for a POST request that I'm using.
Some info about the request:
The user sends a JSON array of ~100 items
The servlet needs to check if any of the received items is newer then its counterpart in the datastore using a single long attribute
I'm using JDO
what i currently do is (pseudo code):
foreach(item : json.items) {
storedItem = persistenceManager.getObjectById(item.key);
if(item.long > storedItem.long) {
// Update storedItem
}
}
Which obviously results in ~100 read requests per request.
What is the best way to reduce the read count for this logic? Using JDO Query? I read that using "IN"-Queries simply results in multiple queries executed after another, so I don't think that would help me :(
There also is PersistenceManager.getObjectsById(Collection). Does that help in any way? Can't find any documentation of how many requests this will issue.
I think you can use below call to do a batch get:
Query q = pm.newQuery("select from " + Content.class.getName() + " where contentKey == :contentKeys");
Something like above query would return all objects you need.
And you can handle all the rest from here.
Best bet is
pm.getObjectsById(ids);
since that is intended for getting multiple objects in a call (particularly since you have the ids, hence keys). Certainly current code (2.0.1 and later) ought to do a single datastore call for getEntities(). See this issue

Resources