在接口测试中,请求参数中涉及时间的参数可能不是固定死的,因此jmeter想通过beanshell中脚本实现随机获取某天(statusTimeEnd(“yyyy-MM-dd HH:mm:ss”))前1周,一个月,一个季度,半年的时间0点,其中statusTimeEnd 的值在用户参数中已配置。
参考 JMeter 性能测试实战的方法 :http://literalink.top/resource/detail/7185031586667040768,
代码如下:
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;
// 设定日期格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 获取 'statusTimeEnd' 的值
String statusTimeEndStr = vars.get("statusTimeEnd");
Calendar statusTimeEnd = Calendar.getInstance();
statusTimeEnd.setTime(sdf.parse(statusTimeEndStr));
// 生成一个随机日期
Random rand = new Random();
long oneMonthMillis = 30L * 24 * 60 * 60 * 1000; // 一个大约为一个月的毫秒数
long randomTimeMillis = (long) (rand.nextDouble() * oneMonthMillis);
Calendar randomDate = Calendar.getInstance();
randomDate.setTimeInMillis(statusTimeEnd.getTimeInMillis() - randomTimeMillis);
// 计算前1天
Calendar calDayBefore = (Calendar) randomDate.clone();
calDayBefore.add(Calendar.DAY_OF_MONTH, -1);
calDayBefore.set(Calendar.HOUR_OF_DAY, 0);
calDayBefore.set(Calendar.MINUTE, 0);
calDayBefore.set(Calendar.SECOND, 0);
calDayBefore.set(Calendar.MILLISECOND, 0);
String dayBeforeStr = sdf.format(calDayBefore.getTime());
vars.put("dayBefore", dayBeforeStr);
// 计算前1周
Calendar calWeekBefore = (Calendar) randomDate.clone();
calWeekBefore.add(Calendar.WEEK_OF_YEAR, -1);
calWeekBefore.set(Calendar.HOUR_OF_DAY, 0);
calWeekBefore.set(Calendar.MINUTE, 0);
calWeekBefore.set(Calendar.SECOND, 0);
calWeekBefore.set(Calendar.MILLISECOND, 0);
String weekBeforeStr = sdf.format(calWeekBefore.getTime());
vars.put("weekBefore", weekBeforeStr);
// 计算前1个月
Calendar calMonthBefore = (Calendar) randomDate.clone();
calMonthBefore.add(Calendar.MONTH, -1);
calMonthBefore.set(Calendar.HOUR_OF_DAY, 0);
calMonthBefore.set(Calendar.MINUTE, 0);
calMonthBefore.set(Calendar.SECOND, 0);
calMonthBefore.set(Calendar.MILLISECOND, 0);
String monthBeforeStr = sdf.format(calMonthBefore.getTime());
vars.put("monthBefore", monthBeforeStr);
// 计算前3个月
Calendar calThreeMonthsBefore = (Calendar) randomDate.clone();
calThreeMonthsBefore.add(Calendar.MONTH, -3);
calThreeMonthsBefore.set(Calendar.HOUR_OF_DAY, 0);
calThreeMonthsBefore.set(Calendar.MINUTE, 0);
calThreeMonthsBefore.set(Calendar.SECOND, 0);
calThreeMonthsBefore.set(Calendar.MILLISECOND, 0);
String threeMonthsBeforeStr = sdf.format(calThreeMonthsBefore.getTime());
vars.put("threeMonthsBefore", threeMonthsBeforeStr);
// 计算前6个月
Calendar calSixMonthsBefore = (Calendar) randomDate.clone();
calSixMonthsBefore.add(Calendar.MONTH, -6);
calSixMonthsBefore.set(Calendar.HOUR_OF_DAY, 0);
calSixMonthsBefore.set(Calendar.MINUTE, 0);
calSixMonthsBefore.set(Calendar.SECOND, 0);
calSixMonthsBefore.set(Calendar.MILLISECOND, 0);
String sixMonthsBeforeStr = sdf.format(calSixMonthsBefore.getTime());
vars.put("sixMonthsBefore", sixMonthsBeforeStr);
// 随机选择一个变量
String[] keys = {"dayBefore", "weekBefore", "monthBefore", "threeMonthsBefore", "sixMonthsBefore"};
int randomIndex = rand.nextInt(keys.length);
String randomKey = keys[randomIndex];
String randomDateStr = vars.get(randomKey);
log.info("随机选择的日期变量: " + randomDateStr);
vars.put("randomDate", randomDateStr);
标签:set,String,vars,mm,dd,randomDate,statusTimeEnd,ss,Calendar
From: https://blog.csdn.net/weixin_43946945/article/details/142096204