beanshell调用jmeter 属性常用语法
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.threads.JMeterVariables;
LocalDateTime time = LocalDateTime.now();
String date = time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//打印调试日志写入。jmeter.log文件
log.info("----->" + date);
//****************************vars使用********************************//
//变量生成
vars.put("tmp" , date);
//变量读取
String date1 = vars.get("tmp");
log.info("值读取测试结果1------>" + date1);
String date = "${tmp}";
log.info("值读取测试结果2------>" + date1);
//直接获取jmeter自动生成的参数变量,COOKIE__gh_sess 存放了cookies信息,是系统自动添加的
log.info(vars.get("COOKIE__gh_sess"));
//操作map类型注意事项
HashMap map = new HashMap(5);
map.put("test1", "1");
log.info(map.toString());
//**************************ctx使用*************************************//
//参考API文档:http://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterContext.html
//ctx上下文信息获取,获取上一个请求result 参考API: http://jmeter.apache.org/api/org/apache/jmeter/samplers/SampleResult.html
SampleResult result = ctx.getPreviousResult();
//获取上一个请求的响应信息
String code = result.getDataType();
log.info("ctx------>" + code );
//获取Jmeter当前运行时变量集合。参考APIhttp://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html
JMeterVariables var = ctx.getVariables();
//增加变量集合
var.put("big"," you are a big pig");
//获取变量数据,此处用法雷同vars.get();
log.info("ctx实现vars.get()用法------>" + var.get("COOKIE__gh_sess"));
//*********************************SampleResult 用法**************************//
//设置当前的sampler的code
ResponseCode = 500;
//设置当前的sampler的message
ResponseMessage = "This is a test";
//***********************prev信息*******//
String str = prev.getResponseDataAsString();
log.info("prev ------->" + str);