您可以使用合适的JSR223 Test Element 和Groovy language 计算给定范围内的随机时间戳。
生成当天 9 到 14 小时之间的随机时间并将其存储到 randomMorningTime JMeter Variable 的示例代码类似于:
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)
演示:
您将能够在需要时以${randomMorningTime} 的形式访问生成的值。
有关更多信息,请参阅GregorianCalendar 类的 JavaDoc