gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 09:24:18 27 4
gpt4 key购买 nike

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

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

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

我找到了 this java脚本的变体

初始脚本在这里:

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

你能帮帮我吗?怎么了?

最佳答案

您可以用更少的代码在 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()));

关于jmeter - 如何在 JMeter 的日期范围内生成随机固定间隔?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13955259/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com