java JMeter:如何在每次迭代的日期范围内将日期增加 +1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25569388/
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-11-02 08:14:48  来源:igfitidea点击:

JMeter : How to increment dates by +1 within date ranges for every iteration

javadategroovyjmeter

提问by user3523837

I want to increment by 1 day 2 dates at every iteration using Jmeter i.e.

我想在每次迭代中使用 Jmeter 增加 1 天 2 日期,即

  • Date1 : 2014/01/01 (iteration1 = 2014/01/02) , (iteration1 = 2014/01/03) etc

  • Date 2 : 2014/09/01 (iteration1 = 2014/09/02) , (iteration1 = 2014/01/03) etc

  • Date1 : 2014/01/01 (iteration1 = 2014/01/02) , (iteration1 = 2014/01/03) 等等

  • 日期 2 : 2014/09/01 (iteration1 = 2014/09/02) , (iteration1 = 2014/01/03) 等

How can I do it ?

我该怎么做 ?

回答by Dmitri T

For JMeter specifics:

对于 JMeter 细节:

  1. Add User Defined Variableselement to set initial date values like

    • DATE1=2014/01/01
    • DATE2=2014/09/01
  2. Add a Beanshell PreProcessoras a child of the request where you need these updated dates. Put the following code into the PreProcessor's "Script" are:

    import java.text.SimpleDateFormat; // necessary
    import java.util.Calendar; // imports
    import java.util.Date;
    
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); // define date format
    Date date1 = sdf.parse(vars.get("DATE1")); // get DATE1 from User Defined variables
    Date date2 = sdf.parse(vars.get("DATE2")); // get DATE2 from UDV
    
    Calendar cal = Calendar.getInstance(); // get Calendar instance
    cal.setTime(date1); // set Calendar's time to be DATE1
    cal.add(Calendar.DAY_OF_YEAR,1); // add 1 day to DATE1
    date1 = cal.getTime(); // set the new value for date1
    vars.put("DATE1",sdf.format(date1)); // update DATE1 JMeter variable
    
    cal.setTime(date2); // set Calendar time to DATE2
    cal.add(Calendar.DAY_OF_YEAR,1); // add 1 day
    date2 = cal.getTime(); // set the new value for date2
    vars.put("DATE2",sdf.format(date2)); // update DATE2 JMeter variable
    
    log.info("DATE1=" + vars.get("DATE1")); // print value of DATE1 to jmeter.log
    log.info("DATE2=" + vars.get("DATE2")); // print value of DATE2 to jmeter.log 
    
  1. 添加用户定义的变量元素以设置初始日期值,例如

    • DATE1=2014/01/01
    • DATE2=2014/09/01
  2. 添加一个Beanshell PreProcessor作为您需要这些更新日期的请求的子项。将以下代码放入预处理器的“脚本”中:

    import java.text.SimpleDateFormat; // necessary
    import java.util.Calendar; // imports
    import java.util.Date;
    
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); // define date format
    Date date1 = sdf.parse(vars.get("DATE1")); // get DATE1 from User Defined variables
    Date date2 = sdf.parse(vars.get("DATE2")); // get DATE2 from UDV
    
    Calendar cal = Calendar.getInstance(); // get Calendar instance
    cal.setTime(date1); // set Calendar's time to be DATE1
    cal.add(Calendar.DAY_OF_YEAR,1); // add 1 day to DATE1
    date1 = cal.getTime(); // set the new value for date1
    vars.put("DATE1",sdf.format(date1)); // update DATE1 JMeter variable
    
    cal.setTime(date2); // set Calendar time to DATE2
    cal.add(Calendar.DAY_OF_YEAR,1); // add 1 day
    date2 = cal.getTime(); // set the new value for date2
    vars.put("DATE2",sdf.format(date2)); // update DATE2 JMeter variable
    
    log.info("DATE1=" + vars.get("DATE1")); // print value of DATE1 to jmeter.log
    log.info("DATE2=" + vars.get("DATE2")); // print value of DATE2 to jmeter.log 
    

Being executed for 3 times it provides the following output:

执行 3 次后,它提供以下输出:

2014/08/30 10:11:13 INFO  - jmeter.util.BeanShellTestElement: DATE1=2014/01/02 
2014/08/30 10:11:13 INFO  - jmeter.util.BeanShellTestElement: DATE2=2014/09/02 
2014/08/30 10:11:13 INFO  - jmeter.util.BeanShellTestElement: DATE1=2014/01/03 
2014/08/30 10:11:13 INFO  - jmeter.util.BeanShellTestElement: DATE2=2014/09/03 
2014/08/30 10:11:13 INFO  - jmeter.util.BeanShellTestElement: DATE1=2014/01/04 
2014/08/30 10:11:13 INFO  - jmeter.util.BeanShellTestElement: DATE2=2014/09/04 
  1. Refer to the variables as ${DATE1}and ${DATE2}where required.
  1. 参考变量${DATE1}${DATE2}所需的地方。

For more information on Beanshell scripting in Apache JMeter check out How to use BeanShell: JMeter's favorite built-in componentguide

有关 Apache JMeter 中 Beanshell 脚本的更多信息,请查看如何使用 BeanShell:JMeter 最喜欢的内置组件指南

回答by UBIK LOAD PACK

In JMeter:

在 JMeter 中:

0/ First install Groovy:

0/ 首先安装Groovy

  • Download it from http://groovy.codehaus.org/, current version as of today is 2.3.6

  • Unzip the file and find folder embeddableand copy groovy-all-2.3.6.jar in jmeter/lib folder

  • Restart JMeter

  • That's it !

  • http://groovy.codehaus.org/下载,截至今天的当前版本是 2.3.6

  • 解压文件,找到可嵌入文件夹,将 groovy-all-2.3.6.jar 复制到 jmeter/lib 文件夹中

  • 重启 JMeter

  • 而已 !

1/ Add User Defined Variableselement to set initial date values like this:

1/ 添加用户定义的变量元素来设置初始日期值,如下所示:

- DATE1=2014/01/01
- DATE2=2014/09/01

2/ The recommended way to do this for REDUCED SYNTAX, PERFORMANCE and LANGUAGE POWERis to use Groovy+ JSR223 PreProcessor, configure it this way:

2/ 为REDUCED SYNTAX, PERFORMANCE 和 LANGUAGE POWER执行此操作的推荐方法是使用Groovy+ JSR223 PreProcessor,按以下方式配置:

enter image description here

在此处输入图片说明

3/ Paste this code in JSR 223 PreProcessor:

3/ 将此代码粘贴到 JSR 223 预处理器中:

 final String DATE_PATTERN = "yyyy/MM/dd";  
 def d1Plus1 = Date.parse(DATE_PATTERN, vars["DATE1"] ) + 1;
 def d2Plus1 = Date.parse(DATE_PATTERN, vars["DATE2"] ) + 1;
 vars.put("DATE1", d1Plus1.format(DATE_PATTERN));
 vars.put("DATE2", d2Plus1.format(DATE_PATTERN));

4/ Refer to your variables as ${DATE1}and ${DATE2}

4/ 将您的变量称为${DATE1}${DATE2}

5/ Running this Plan show this result:

5/ 运行这个计划显示这个结果:

enter image description here

在此处输入图片说明

For why Groovy is your best choice read this:

关于为什么 Groovy 是您的最佳选择,请阅读以下内容:

回答by troig

Here is an example:

下面是一个例子:

  • Function to add a number of days to a Calendar:

    void addDay(Calendar calendar, int offsetDay) {
        calendar.add(Calendar.DAY_OF_YEAR, offsetDay);
    }
    
  • Example with 5 iterations (from 2014/01/01):

    Calendar calendar = Calendar.getInstance();
    calendar.set(2014, Calendar.JANUARY, 1);
    
    for (int i = 0 ; i < 5; i++) {
        addDay(calendar, 1);
    
        // Only for preview sample (if sdf = new SimpleDateFormat("yyyy/MM/dd");)
        System.out.println(sdf.format(calendar.getTime()));
    }
    
  • 将天数添加到 a 的函数Calendar

    void addDay(Calendar calendar, int offsetDay) {
        calendar.add(Calendar.DAY_OF_YEAR, offsetDay);
    }
    
  • 5 次迭代的示例(从 2014/01/01 开始):

    Calendar calendar = Calendar.getInstance();
    calendar.set(2014, Calendar.JANUARY, 1);
    
    for (int i = 0 ; i < 5; i++) {
        addDay(calendar, 1);
    
        // Only for preview sample (if sdf = new SimpleDateFormat("yyyy/MM/dd");)
        System.out.println(sdf.format(calendar.getTime()));
    }
    

The output of the execution of this code would be:

执行此代码的输出将是:

2014/01/02
2014/01/03
2014/01/04
2014/01/05
2014/01/06

Hope it helps you.

希望对你有帮助。

回答by injecteer

as simple as with Groovy's goodness:

就像 Groovy 的优点一样简单:

Date end= new Date() + 5 // from now till +5 days

for( Date curr = new Date(); curr < end; curr++ ){
  println curr.format( 'dd.MM' )
}

gives

29.08
30.08
31.08
01.09
02.09