beanshell使用
jmeter的BeanShell
定时器: BeanShell Timer
前置处理器:BeanShell PreProcessor
采样器: BeanShell Sampler
后置处理器:BeanShell
PostProcessor
断言: BeanShell断言
监听器: BeanShell Listener
Bean Shell常用内置对象
log(Logger):打印日志信息,写入信息到jmeber.log文件,
使用方法,log.info("info")可参考:http://excalibur.apache.org/apidocs/org/apache/log/Logger.html
ctx(JMeterContext):该变量引用了当前线程的上下文,
使用方法可参考:http://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterContext.html
vars(JMeterVariables):操作jmeter变量,这个变量实际引用了JMeter线程中的局部变量容器(本质上是Map),它是测试用例与BeanShell交互的桥梁,常用方法:
1).vars.get(String
key):从jmeter中获得变量值
2). vars.put(String
key,String value):数据存到jmeter变量中
可参考:http://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html
props(JMeterProperties):操作jmeter属性,该变量引用了JMeter的配置信息,可以获取Jmeter的属性,它的使用方法与vars类似,但是只能put进去String类型的值。
1)
props.get("START.HMS"); 注:START.HMS为属性名,在文件jmeter.properties中定义
2) props.put("PROP1","1234");
prev(SampleResult):获取前面的sample返回的信息,常用方法:
1)
getResponseDataAsString():获取响应信息
2 getResponseCode() :获取响应code
可参考:http://jmeter.apache.org/api/org/apache/jmeter/samplers/SampleResult.html
sampler(Sampler):gives access to the current sampler
可参考:http://jmeter.apache.org/api/org/apache/jmeter/samplers/Sampler.html
threadName - String containing the current thread name
应用实例:
有一个socket接口的性能测试,服务器启动了多个端口。由于需要做链路鉴权,所以要保证每一个jmeter线程始和固定的端口通讯
每个线程都有一个线程号,可以考虑通过线程号关联固定端口。
//线程号取模 int tmp= ctx.getThreadNum() % 5; //每个线程对应的端口 tmp = tmp + 9000; //将端口号保存到变量port vars.put("port", tmp.toString());
在TCP取样器配置的端口号就可以通过${port}进行使用了:
http://jmeter.apache.org/usermanual/functions.html#__BeanShell