Increment Date for every Thread in Jmeter - jmeter

I have a requirement to book an appointment, The value should not overlap.So I have to increment start time and end time for every thread and it should consider time from 9:00 am to 6:00 pm.I have written below Groovy code to increment the date. But for every thread I am getting the same value.Below is the log viewer ,where we can see that ,for all 4 threads the value didnt change. Any help would be appreciated.
def now = new Date()
log.info('Before: ' + now.format('HH:mm'))
use(groovy.time.TimeCategory) {
def nowPlus60Mins = now + 60.minutes
def nowPlus15Mins = nowPlus60Mins + 15.minutes
props.put('After',nowPlus60Mins.format('HH:mm'));
props.put('End',nowPlus15Mins.format('HH:mm'));
log.info('After: ' + props.get('After'))
log.info('End: ' + props.get('End'))
}

As per JMeter Documentation:
Properties are not the same as variables. Variables are local to a thread; properties are common to all threads
So what you're doing is overwriting the value hence all the threads are using the same (last) one.
Consider switching from props to vars and you should get expected result.
Moreover, JMeter provides __timeShift() function so you don't even need any custom Groovy scripting

Related

Change the number of threads values in JMeter according to the Loop Count value

I would like to change the number of threads values in JMeter according to the Loop Count value.
Number of Threads values are 1 when Loop Count values are 1,
Number of Threads values ​​when Loop Count value is 2,
When the Loop Count value is n, I would like to change the Number of Threads value to n.
How do we do that?
Now JMeter is using 5.3.
You can use same (or different) property for threads and loop count, e.g.
On Thread Group settings put in number of threads and loop count get property method
${__P(count)}
And send the property in command line:
jmeter -n -Jcount=4 -t your.jmx
For count 4 flow will execute 16 times (4 threads * 4 loops)
The nature of your request is not very clear, given you know the number of loops you can use the same JMeter Variable or JMeter Property as the number of Threads
Whatever. You can change the number of threads for every Thread Group in your Test Plan to be equal to the number of loops of that Thread Group by:
Adding setUp Thread Group to your Test Plan
Adding JSR223 Sampler to the setUp Thread Group
Putting the following code into "Script" area
SampleResult.setIgnore()
def engine = engine = ctx.getEngine()
def testPlan = engine.test
def threadGroupSearch = new org.apache.jorphan.collections.SearchByClass<>(org.apache.jmeter.threads.ThreadGroup.class)
testPlan.traverse(threadGroupSearch)
threadGroupSearch.getSearchResults().each { threadGroup ->
def loops = threadGroup.getSamplerController().getPropertyAsInt('LoopController.loops')
threadGroup.setNumThreads(loops)
}

in jmeter variable value is not incrementing correctly in http request

in jmeter> bean shell preprocessor i wrote code to increment the date and this date variable i have passed to a http request. But while debugging only first date is fetching .incremented date is not passing to the variable
Calendar cal = Calendar.getInstance();
for (int i = 0; i < 100; i++) {
cal.add(Calendar.SECOND, 1);
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd
kk:mm:ss"); //dd/MM/yyyy
Date now = new Date();
String strDate = sdfDate.format(cal.getTime());
vars.put("timer", strDate);
log.info("DATE" + strDate);
}
this 'timer' variable is not incrementing to http request for 'n' request'
Your code always returns one future timestamp (100 seconds ahead of current time) and saves it into ${timer} variable.
Depending on what you are trying to achieve, there are several options:
Change vars.put("timer", strDate); line to vars.put("timer_" + i, strDate);, this way you will get the following variables:
timer_0=current time + 1 second
timer_1=current time + 2 seconds
...
timer_99=current time + 100 seconds
Remove loop as it is not really required as you are overwriting ${timer} variable 100 times
Also be aware that using Beanshell is not the best scripting option, according to JMeter Best Practices you should avoid scripting where possible. For instance in particular your case you can achieve the same functionality using __time() and __longSum() functions combination. Check out Apache JMeter Functions - An Introduction to get familiarized with JMeter Functions concept.
If you have to go for scripting consider using the most performing language which is Groovy (you can use it from JSR223 Test Elements)

how to use variable in jmeter like counter

I'm using loop controller inside the jmeter script, and I'm not able to fetch previous variable value in pre-processor beanshell.
Example:
var temp = 1; log.info("before : "+temp.toString()); temp++; prev.put("t",temp.toString());
Thanks in advance
To save values b/w iterations:
Following is one of the ways to store and retrieve the values b/w iterations:
log.info("temp prev value " + vars.get("temp")); // first iteration returns null
vars.put("temp","something"); // store or override the value, so it will be available in next iterations.
To know iteration number:
If your need is to know the iteration number, then use Counter:
In beanshell preprocessor, access using reference name (counter) as shown below:

Relative time calculation

I have an application implementing RESTful API. I have two methods create_order and order_status. The first method creates order and persists it with the current time in the order.time field:
order.time = Time.now
The second method responds with a hardcoded value:
:eta => 20.minutes.from_now.to_i
Instead of returning the hardcoded 20 minutes, how can I return the relative value that decreases with elapsed time (depending on the time when status request was made)?
At the beginning of the order, they are the same (20.minutes.from_now.to_i), but if the request is made after 5 mins, it should be 15.minutes.from_now.to_i.
I would save some other attribute along with order.time
For example : order.eta = Time.now + 1200
Or else : order.processing_time = 1200 and then order.eta could be calculated.
I like the second solution better, enabling different processing times for different orders.

How do I create an object that will have a number at the end that will increment each time it is used?

I am trying to make a variable that I can increment each time after I use it.
The $companyLevel os the variable that I need to increment.
count = 20
# Variables (20)
while count > 0
$levelName = ""; 8.times{$levelName << (65 + rand(25)).chr}
$companyLevel = "CLev5"
browser2.button(:id, "addCompanyLevel").click
sleep 2
browser2.text_field(:id, $companyLevel).set $levelName
$companyLevel += 1
count -= 1
end
How do I create a variable that will have a number at the end that will increment each time it is used?
Thanks.
Since you already have a count, why does this need to be a variable? why not just do simple string concatenation to create the value you want on the fly
companyLevel = "CLev" + count.to_s
Unless you need to perhaps read up on what an 'array' is?
I'd suggest you purchase and read the book "Everyday Scripting with Ruby" it's a great way to lean the basics of the ruby language and geared towards testers.
This is achieved by creating an object with a property that increments not by creating a variable that increments.

Resources