jmeter将人类可读的日期转换为大纪元

时间:2020-11-06 14:07:52

标签: datetime jmeter

我有一个脚本,它需要两个时间戳,一个用于02:30的“今天”,另一个用于02:30的“明天”。使用;

可以很容易地生成日期
date1=${__time("yyyy.MM.dd 02:30:00")}
date2=${__timeShift("yy.MM.dd 02:30:00",,P1D,,)}

但是POST请求需要使用Epoch格式的日期。如何将上述内容转换为纪元?

我发现使用了一个选项;

date1a=date1.getTime().toString()

但是那没有用(没有方法签名)。与此相关的是,我确实发现了jmeter 5.3错误,其中缺少dateutil.jar,但添加它没有任何作用。

有很多将Epoch转换为人类可读的选项,但它们不是可逆过程。

或者,是否有一种方法可以直接计算时代,再次针对“今天”在2:30和“明天”在2:30?

1 个答案:

答案 0 :(得分:0)

  1. 请勿按照JSR223 Sampler documentation

    将JMeter函数或变量内联到脚本中
    • The JSR223 test elements have a feature (compilation) that can significantly increase performance.
    • When using this feature, ensure your script code does not use JMeter variables or JMeter function calls directly in script code as caching would only cache first replacement. Instead use script parameters
  2. 我相信您可以使用良好的旧SimpleDateFormat(示例代码来解析您的字符串并为其添加24小时)来实现目标:

    def today = new java.text.SimpleDateFormat('yyyy.MM.dd hh:mm:ss').parse('2020.11.06 02:30:00')
    log.info('Today:    ' + today)
    def tomorrow = new Date(today.getTime() + 86400000L)
    log.info('Tomorrow: ' + tomorrow)
    
    log.info('Today epoch   : ' + today.getTime())
    log.info('Tomorrow epoch: ' + tomorrow.getTime())
    

演示:

enter image description here

有关JMeter中Groovy脚本的更多信息:Apache Groovy - Why and How You Should Use It

相关问题