01jmeter-beanshell常用代码段
1.获取时间
import java.util.*; import java.text.SimpleDateFormat; String str1 = (new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")).format(new Date()); //获取当前时间 var now_date = new Date().getTime(); //获取当前时间戳 vars.put("ts",now_date.toString()); //设置为jmeter的全局变量 Calendar cal =Calendar.getInstance(); cal.add(Calendar.DATE,-1); String yesterday=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(cal.getTime()); //昨天 vars.put("d",yesterday); //设置为jmeter全局变量 引用变量:${d}
2.生成随机数
import org.apache.commons.lang3.RandomUtils; int group_no =RandomUtils.nextInt(1,999); vars.put("gp",group_no.toString());
3.打印变量到cmd控制台
System.out.println("d is:"+vars.get("d")); //打印昨天(1中的d)
涉及到beanshell的地方:
a.测试计划-鼠标右键-添加-前置处理器/后置处理器-beanshell PreProcessor/beanshell-PostProcessor
b.线程组-鼠标右键-添加-前置处理器/后置处理器-beanshell PreProcessor/beanshell-PostProcessor
c.取样器-鼠标右键-添加-前置处理器/后置处理器-beanshell PreProcessor/beanshell-PostProcessor
示例:
踩坑是成长最快的方式