Java Jmeter - 未来时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21846846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:01:43  来源:igfitidea点击:

Jmeter - Future timestamp

javajmeter

提问by Demoric

im trying to create a parameter in Jmeter that gives the current timestamp + 5 minutes. Does anyone know how to do this? To generate the current timestamp i have this: ${__time(HH:mm:ss,TIMESTAMP)}

我试图在 Jmeter 中创建一个参数,给出当前时间戳 + 5 分钟。有谁知道如何做到这一点?要生成当前时间戳,我有这个: ${__time(HH:mm:ss,TIMESTAMP)}

采纳答案by Dmitri T

I'm afraid that __time()function doesn't provide enough flexibility. You'll need to calculate this date value via Beanshell Sampler or Beanshell Pre Processor

恐怕__time()函数没有提供足够的灵活性。您需要通过Beanshell Sampler 或 Beanshell Pre Processor计算此日期值

Relevant Beanshell code will look like

相关的 Beanshell 代码看起来像

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

Date now = new Date(); // get current time
Calendar c = Calendar.getInstance(); // get Java Calendar instance
c.setTime(now); // set Calendar time to now
c.add(Calendar.MINUTE, 5); // add 5 minutes to current time
Date now_plus_5_minutes = c.getTime(); // get Date value for amended time
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); // create a formatter for date       
String mydate = sdf.format(now_plus_5_minutes); // format date as string
vars.put("mydate",mydate); // save date to JMeter variable named "mydate"

You'll be able to refer that mydatevalue as

您将能够将该mydate值称为

  • ${mydate}
  • ${__V(mydate)}
  • ${我的日期}
  • ${__V(我的日期)}

In the place you'll need to provide that updated date.

在您需要提供更新日期的地方。

Hope this helps.

希望这可以帮助。

回答by eugene.polschikov

had to use but for future date in terms of days:

必须使用但以天为单位的未来日期:

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

    Date date = new Date(); 
    date.setDate(date.getDate()+5); 
    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
    // or: SimpleDateFormat df = new SimpleDateFormat("MM-dd-yyyy"); 
    String formattedDate = df.format(date); 
    vars.put("myFutureDate",formattedDate); 

the block of code above you are supposed to put into BeanShellPreprocessor: enter image description here

您应该将上面的代码块放入BeanShellPreprocessor在此处输入图片说明

To call a variable date in appropriate place -

在适当的地方调用可变日期 -

${myFutureDate} 

回答by Richard Friedman

You can use the __javaScript command as your parameter http://jmeter.apache.org/usermanual/functions.html#__javaScript. This will give you the timestamp in seconds since epoch.

您可以使用 __javaScript 命令作为参数http://jmeter.apache.org/usermanual/functions.html#__javaScript。这将为您提供自纪元以来以秒为单位的时间戳。

${__javaScript( Math.floor( ( Date.now() + ( 5 * 60 * 1000 ) ) / 1000 ) )}

${__javaScript( Math.floor( ( Date.now() + ( 5 * 60 * 1000 ) ) / 1000 ) )}

Using __javaScript as parameter

使用 __javaScript 作为参数

Example of JMeter Test with JMX File

使用 JMX 文件JMeter 测试示例

回答by user1053510

Here's a one-line solution I suggest:

这是我建议的单行解决方案:

${__groovy(use(groovy.time.TimeCategory) { (new Date() + 5.minutes).format('yyyyMMddHHmmssSSS') })}

Time offset is based on Groovy's TimeCategoryhttp://docs.groovy-lang.org/latest/html/api/groovy/time/TimeCategory.htmland is super easy to read and use (e.g. you can replace 5.minuteswith 1.dayor so).

时间偏移基于 Groovy 的TimeCategoryhttp://docs.groovy-lang.org/latest/html/api/groovy/time/TimeCategory.html并且非常易于阅读和使用(例如,您可以替换5.minutes1.day左右)。

Comparing to Dmitri's and Eugene's answers, the solution above is much shorter, doesn't require defining any additional config and is evaluated every time (so it can be used multiple times in the JMeter script).

与 Dmitri 和 Eugene 的答案相比,上面的解决方案要短得多,不需要定义任何额外的配置并且每次都进行评估(因此它可以在 JMeter 脚本中多次使用)。

JavaScript in Richard's answer is not recommended to be used in JMeter: http://jmeter.apache.org/usermanual/functions.html#__javaScript

不建议在 JMeter 中使用 Richard 回答中的 JavaScript:http: //jmeter.apache.org/usermanual/functions.html#__javaScript

Groovy is also better in terms of date manipulation (thanks to TimeCategory) and date formatting.

Groovy 在日期操作(感谢TimeCategory)和日期格式方面也更好。

Tested on JMeter 3.2

在 JMeter 3.2 上测试