Jmeter元件BeanShell 取样器调用Java代码处理时间

    xiaoxiao2022-06-24  193

    在测试过程中经常会遇到一些接口中的参数需要进行处理时间格式,比如当前时间、当前日期,当月一号,下个月一号等等,下面我们就使用Jmeter元件BeanShell 取样器调用Java代码进行处理,首先在jmeter的目录D:\apache-jmeter-5.1.1\bin下新建DateUtil.java,如下图: 代码如下: import java.text.Format; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date;

    public class DateUtil { public static String getdodyTime() { Date date=new Date();

    Format f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String todaytime=f.format(date); return todaytime; } public static String getTodyData() { Date date=new Date(); Format f = new SimpleDateFormat("yyyy-MM-dd"); String today=f.format(date); return today; } public static String getTomorrowDate() { Date date=new Date(); Format f = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.DAY_OF_MONTH, 1);// 今天+1天 Date tomorrow_date = c.getTime(); String tomorrow=f.format(tomorrow_date); return tomorrow; } public static String getYesterdayDate() { Date date=new Date(); Format f = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.DAY_OF_MONTH, -1);// 今天-1天 Date yesterday_date = c.getTime(); String yesterday=f.format(yesterday_date); return yesterday; } public static String getTomorrowTime() { Date date=new Date(); Format f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.DAY_OF_MONTH, 1);// 今天+1天 Date tomorrow_date = c.getTime(); String tomorrowtime=f.format(tomorrow_date); return tomorrowtime; } public static String getTomorrowTimeSubtract() { Date date=new Date(); Format g = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar e = Calendar.getInstance(); e.setTime(date); e.add(Calendar.DAY_OF_MONTH, -60);// 今天-60天 Date tomorrow_date_subtract = e.getTime(); String tomorrowtimesubtract=g.format(tomorrow_date_subtract); return tomorrowtimesubtract; } public static String getTomorrowTimeSubtractDate() { Date date=new Date(); Format g = new SimpleDateFormat("yyyy-MM-dd"); Calendar e = Calendar.getInstance(); e.setTime(date); e.add(Calendar.DAY_OF_MONTH, -60);// 今天-60天 Date tomorrow_date = e.getTime(); String tomorrowtimesubtractdate=g.format(tomorrow_date); return tomorrowtimesubtractdate; } public static String getTomorrowTimeadd() { Date date=new Date(); Format g = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar e = Calendar.getInstance(); e.setTime(date); e.add(Calendar.DAY_OF_MONTH, 10);// 今天+10天 Date tomorrow_date = e.getTime(); String tomorrowtimeadd=g.format(tomorrow_date); return tomorrowtimeadd; } public static String getnowYear() { Date date=new Date(); Format year = new SimpleDateFormat("yyyy");//当前年 Calendar e = Calendar.getInstance(); e.setTime(date); Date year_date = e.getTime(); String nowYear=year.format(year_date ); return nowYear; } public static String getnowMonth() { Date date=new Date(); Format month = new SimpleDateFormat("MM");//当前月 Calendar e = Calendar.getInstance(); e.setTime(date); Date month_date = e.getTime(); String nowMonth=month.format(month_date); return nowMonth; } public static String getDateMonth() { Date date=new Date(); Format month_format = new SimpleDateFormat("yyyy-MM-01"); Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.DAY_OF_MONTH,0);本月第一天 Date month = cal.getTime(); String month_date=month_format.format(month); return month_date; } public static String getNextDateMonth() { Date date=new Date(); Format month_next_format = new SimpleDateFormat("yyyy-MM-01"); Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.MONDAY,1);//下个月初 Date month_next = cal.getTime(); String month_next_date=month_next_format.format(month_next); return month_next_date; } public static String getDateWeek() { Date date=new Date(); Format week_format = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.DAY_OF_MONTH,-7);//当前日期-7天 Date week = cal.getTime(); String week_date=week_format.format(week); return week_date; } public static String getDay() { Date date=new Date(); Format day_format = new SimpleDateFormat("dd");//当前日 Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.getTime();//当前日 Date day = cal.getTime(); String day_date=day_format.format(day); return day_date; }

    }

    现在我们就可以在jmeter中调用BeanShell 取样器,如下图: 接下来在BeanShell 中引用DateUtil.java,代码如下: source(“D:\apache-jmeter-5.1.1\bin\DateUtil.java”); 变量名不太规范,不要学我哦!

    最后我们在接口中引入BeanShell 的变量,如下图 OK,至此BeanShell 取样器调用Java代码就结束了,谢谢


    最新回复(0)