1

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

在测试计划中,我需要生成具有固定持续时间的随机间隔(对于 4 个案例:1、3、7 天和 1 个月长),它们放置在此日期范围 [2011-01-01; 2011-12-31]。这些案例中的每一个都必须具有定义的权重。

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

我发现了这个java-script 的变体

初始脚本在这里:

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 连接的代码部分),则脚本运行良好。

请问你能帮帮我吗?怎么了?

4

3 回答 3

6

你可以用更少的代码在 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()));
于 2012-12-21T20:25:32.320 回答
1

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

于 2012-12-20T16:47:12.157 回答
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);

我的错误是我认为时间是以为单位的,但它是以毫秒为单位的!

于 2012-12-21T15:16:11.010 回答