Search code examples
datetimeparametersjmeterperformance-testingepoch

How to set different time slots for a single https requests in jmeter


I have a http request where I need to send random time values in given time frame

Request looks like this: http://domain/api1?&mt=getEmp&punchTime=1590678744

I've shifts pre-defined Morning: 0900 to 1400 Evening: 1400 to 1900 Night: 1900 to 2300 Expectation is: random epoch time value between pre-defined shift time slot should be put as punch-time for each request.

I don't want to separate out the request as per different time shifts.

Could anyone please help me to achieve this with JMeter?


Solution

  • You can calculate the random time stamp in the given range using a suitable JSR223 Test Element and Groovy language.

    Example code which produces random time between 9 and 14 hour of the current day and store it into randomMorningTime JMeter Variable would be something like:

    def calendar = Calendar.getInstance()
    def morningStart = new GregorianCalendar(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 9, 00).getTimeInMillis()
    def morningEnd = new GregorianCalendar(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 14, 00).getTimeInMillis()
    
    
    def randomMorningTime = org.apache.commons.lang3.RandomUtils.nextLong(morningStart, morningEnd)
    def timestamp = ( randomMorningTime / 1000).round() as String
    log.info('Random morning time for current day: ' + new Date(randomMorningTime))
    log.info('Associated timestamp: ' + timestamp)
    vars.put('randomMorningTime', timestamp)
    

    Demo:

    enter image description here

    You will be able to access the generated value as ${randomMorningTime} where required.

    See JavaDoc on GregorianCalendar class for more information