如何在JMeter中的日期范围内生成随机固定间隔?

时间:2012-12-19 15:08:53

标签: jmeter

我有StartDate(例如2011-01-01)和EndDate(例如2011-12-31)。 我的所有数据都放在这个范围内。

在测试计划中,我需要生成具有固定持续时间的随机间隔 (4例:1,3,7天和1个月) 放在此日期范围内[2011-01-01; 2011-12-31]。 每种情况都必须有定义的权重。

如何获得具有随机开始日期和随机结束日期的固定间隔(并将这些随机日期放入2个变量中)?

我找到了java脚本的this变体

初始脚本在这里:

var startDate = new Date();
startDate.setDate(1);
startDate.setMonth(0);
startDate.setYear(1991);
var startDateTime = startDate.getTime();

var endDate = new Date();
endDate.setDate(31);
endDate.setMonth(11);
endDate.setYear(2003);
var endDateTime = endDate.getTime();

var randomDate = new Date();
var randomDateTime = startDateTime+Math.random()*(endDateTime-startDateTime);
randomDate.setTime(randomDateTime);

var rndDate = randomDate.getDate();
var rndMonth = randomDate.getMonth() + 1;
var rndYear = randomDate.getFullYear();

if (rndDate.toString().length == 1)
rndDate = "0" + rndDate;
if (rndMonth.toString().length == 1)
rndMonth = "0" + rndMonth;

rndDate + "/" + rndMonth + "/" + rndYear;

但是我需要生成固定间隔的随机开始日期(我称之为$ {RandomStartDate}),然后添加间隔的长度以获得它的结束日期(这个日期我叫$ {RandomEndDate})。

然后我将脚本更改为1天的间隔(1天是86400秒):

var startDate = new Date();
startDate.setDate(01);
startDate.setMonth(01);
startDate.setYear(2011);
var startDateTime = startDate.getTime();

var endDate = new Date();
endDate.setDate(31);
endDate.setMonth(12);
endDate.setYear(2011);
var endDateTime = endDate.getTime();

var randomSDate = new Date();
var randomSDateTime = startDateTime+Math.random()*((endDateTime - 86400) -startDateTime );
randomSDate.setTime(randomSDateTime);

var randomEDate = new Date();
var randomEDateTime = (randomSDateTime +  86400);    //add 1 day long interval (86400 s)
randomEDate.setTime(randomEDateTime);   //convert number format to string format of date
var rndSDate = randomSDate.getDate();
var rndSMonth = randomSDate.getMonth()+1 ;
var rndSYear = randomSDate.getFullYear();

var rndEDate = randomEDate.getDate();      
var rndEMonth = randomEDate.getMonth()+1 ;
var rndEYear = randomEDate.getFullYear();

if (rndSDate.toString().length == 1)      
rndSDate = "0" + rndSDate;
if (rndSMonth.toString().length == 1)
rndSMonth = "0" + rndSMonth;

if (rndEDate.toString().length == 1)      
rndEDate = "0" + rndEDate;
if (rndEMonth.toString().length == 1)
rndEMonth = "0" + rndEMonth;

var RandomStartDate = rndSYear + "-" + rndSMonth + "-" + rndSDate;
vars.put ("RandomStartDate", RandomStartDate);

var RandomEndDate = rndEYear + "-" + rndEMonth + "-" + rndEDate;
vars.put ("RandomEndDate", RandomEndDate);

但是这个脚本生成的RandomEndDate等于RandomStartDate。 如果我单独生成RandomStartDate(没有与RandomEndDate连接的代码部分),脚本运行良好。

你能帮助我吗?有什么问题?

3 个答案:

答案 0 :(得分:4)

您可以使用少量代码在Beanshell中执行此操作:

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

calendar = Calendar.getInstance();
calendar.set(2011, 0, 1);
startTime = calendar.getTimeInMillis();
calendar.set(2012, 11, 31);
endTime = calendar.getTimeInMillis();
randomTime1 = startTime + (long)(Math.random()*(endTime-startTime));
randomTime2 = randomTime1 + (long)(Math.random()*(endTime - randomTime1)+86400000);

formatter = new SimpleDateFormat("yyyy-MM-dd");
calendar.setTimeInMillis(randomTime1);
vars.put("start", formatter.format(calendar.getTime()));
calendar.setTimeInMillis(randomTime2);
vars.put("end", formatter.format(calendar.getTime()));

答案 1 :(得分:1)

对于这种情况,我会选择将复杂性放在jMeter之外,并创建一个perl脚本,根据您的要求生成100对日期,并使用CSV Data Set Config将其读入jMeter变量。

答案 2 :(得分:0)

我找到了解决方案!

这是正确的脚本,它允许模拟具有固定长度(1天长间隔)的随机间隔,并在开始和结束时获得2个变量 - $ {RandomStartDate}和$ {RandomEndDate}:

var startDate = new Date();
startDate.setDate(01);
startDate.setMonth(01);
startDate.setYear(2011);
var startDateTime = startDate.getTime();

var endDate = new Date();
endDate.setDate(31);
endDate.setMonth(12);
endDate.setYear(2011);
var endDateTime = endDate.getTime();

var randomSDate = new Date();
var randomSDateTime = startDateTime+Math.random()*((endDateTime - 86400000) -startDateTime );
randomSDate.setTime(randomSDateTime);

var randomEDate = new Date();
var randomEDateTime = (randomSDateTime + 86400000);    //add 1 day long interval (86400000 ms)
randomEDate.setTime(randomEDateTime);   //convert number format to string format of date
var rndSDate = randomSDate.getDate();
var rndSMonth = randomSDate.getMonth()+1 ;
var rndSYear = randomSDate.getFullYear();

var rndEDate = randomEDate.getDate();      
var rndEMonth = randomEDate.getMonth()+1 ;
var rndEYear = randomEDate.getFullYear();

if (rndSDate.toString().length == 1)      
rndSDate = "0" + rndSDate;
if (rndSMonth.toString().length == 1)
rndSMonth = "0" + rndSMonth;

if (rndEDate.toString().length == 1)      
rndEDate = "0" + rndEDate;
if (rndEMonth.toString().length == 1)
rndEMonth = "0" + rndEMonth;

var RandomStartDate = rndSYear + "-" + rndSMonth + "-" + rndSDate;
vars.put ("RandomStartDate", RandomStartDate);

var RandomEndDate = rndEYear + "-" + rndEMonth + "-" + rndEDate;
vars.put ("RandomEndDate", RandomEndDate);

我的错误是我认为时间是,但它是毫秒