1

I'm trying to create 24 appointments per day and after the 24th appointment has been created, I want my script to switch to the next day and then continue adding appointments hour by hour and so on.

1 appointment = 1 hour

I'm using the following BeanShell PreProcessor piece of code for hour and day iteration inside the request:

cal.add(Calendar.DAY_OF_YEAR,1);
cal.add(Calendar.HOUR_OF_DAY,1);

I used User Defined Variables to set initial hour and date in the following format (HH:mm) (MM/dd/yyyy)

The script is working but it is only adding 1 appointment per day and then switches to the next day.

I can also use a loop controller and put the +1hour BeanShell PreProcessor request under the loop controller but then I got no clue how to switch the day after the 24 loops of the loop controller have been reached. Is there a way to iterate the days by 1 from the User Defined Variables on each thread execution?

This is the code that I used in the BeanShell inside my add appointment request:

import java.text.SimpleDateFormat; 
import java.util.Calendar;
import java.util.Date;

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); 
Date date1 = sdf.parse(vars.get("HOUR")); 


Calendar cal = Calendar.getInstance(); 
cal.setTime(date1); 
cal.add(Calendar.HOUR_OF_DAY,1);
date1 = cal.getTime(); 
vars.put("HOUR",sdf.format(date1)); 


log.info("HOUR=" + vars.get("HOUR"));

Mainly, I'm having a thread group with 3 requests which together are representing a flow: Login -> Create a patient -> Add appointment for the above patient

Add appointment is a POST request with a bunch of parameters. The ones I'm interested in iterating are: Appointment.StartDateDay and Appointment.StartTime.

please help ^^

Klaus
  • 25
  • 5
  • You don't actually need to switch the day, if you keep on adding hours the day will switch automatically. So removing `cal.add(Calendar.DAY_OF_YEAR,1);` should fix the issue. – Parth Mehta Dec 13 '19 at 14:38
  • Thanks for your reply Parth! I already tried what you just mentioned but the thing is my add appoointment request contains both HOUR and DAY parameters. Therefore, if I loop my request 48 times on the same thread, it will still add the 48 appointments on the same day – Klaus Dec 14 '19 at 17:24
  • Please can you paste your code full code in your question? It should give me better context. – Parth Mehta Dec 14 '19 at 17:31
  • Yes of course. Hope it helps – Klaus Dec 14 '19 at 18:06

2 Answers2

1
  1. Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting so consider migrating from Beanshell to Groovy as soon as possible
  2. In general it is recommended to avoid scripting and use JMeter built-in components where possible, for your time increment JMeter's __timeShift() function should work just fine:

     ${__timeShift(HH:mm,now,PT1H,,)}
    

Demo:

enter image description here

More information: Creating Dates in JMeter Using the TimeShift Function

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
0

I think the issue is at Date date1 = sdf.parse(vars.get("HOUR")); where you are trying to parse the date like this the day, month and year information gets lost and only hours and minutes get preserved. So if you change it as follows then I think it should work for you once you add start time as user defined variable TIME_IN_MS (e.g. 1576586156).

import java.text.SimpleDateFormat; 
import java.util.Calendar;
import java.util.Date;

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); 
Date date1 = new Date(Long.parseLong(vars.get("TIME_IN_MS"))); 


Calendar cal = Calendar.getInstance(); 
cal.setTime(date1); 
cal.add(Calendar.HOUR_OF_DAY,1);
date1 = cal.getTime(); 
vars.put("TIME_IN_MS",String.valueOf(date1.getTime())); 


log.info("TIME_IN_MS=" + vars.get("TIME_IN_MS"));
Parth Mehta
  • 1,869
  • 5
  • 15
  • It's not working, unfortunately. "HOUR" is the start hour format which is set in the User Defined Variables in the format HH:mm. More specifically, in my context HOUR=00:00 which means that my first appointment will be added at 00:00 and then it will start iterating by +1 until it reaches 24:00. Am I missing something? – Klaus Dec 17 '19 at 09:37
  • I'm really eager to understand how to iterate the DAY after iterating the HOUR 24 times if the DAY value is a fixed parameter inside my add appointment http request. Until then, I'll keep trying to get it right. Thanks Parth! – Klaus Dec 17 '19 at 09:56
  • Beanshell code is ok. The problem might be elsewhere. Please share output of `log.info("TIME_IN_MS=" + vars.get("TIME_IN_MS"));` – Parth Mehta Dec 17 '19 at 11:01
  • 17 14:11:15,320 WARN o.a.j.m.BeanShellPreProcessor: Problem in BeanShell script. org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import java.text.SimpleDateFormat; import java.util.Calendar; import java.util. . . . '' : Error in method invocation: Method put( java.lang.String, long ) not found in class'org.apache.jmeter.threads.JMeterVariables' In other words, TIME_IN_MS variable is no longer getting iterated by using the above piece of code. – Klaus Dec 17 '19 at 12:15
  • Please see revised code in the answer, it should work for you now. – Parth Mehta Dec 17 '19 at 12:39