Jmeter--Bean Shell用法
一、什么是Bean Shell
- BeanShell是一种完全符合Java语法规范的脚本语言,并且又拥有自己的一些语法和方法;
二、Jmeter有哪些Bean Shell
- 定时器: BeanShell Timer
- 前置处理器:BeanShell PreProcessor
- 采样器: BeanShell Sampler
- 后置处理器:BeanShell PostProcessor
- 断言: BeanShell断言
- 监听器: BeanShell Listener
三、Bean Shell常用内置变量
JMeter在它的BeanShell中内置了变量,用户可以通过这些变量与JMeter进行交互,其中主要的变量及其使用方法如下:
- log:写入信息到jmeber.log文件,使用方法:log.info(“This is log info!”);
- ctx:该变量引用了当前线程的上下文,使用方法可参考:org.apache.jmeter.threads.JMeterContext。
- vars - (JMeterVariables):操作jmeter变量,这个变量实际引用了JMeter线程中的局部变量容器(本质上是Map),它是测试用例与BeanShell交互的桥梁,常用方法:
a) vars.get(String key):从jmeter中获得变量值
b) vars.put(String key,String value):数据存到jmeter变量中
更多方法可参考:org.apache.jmeter.threads.JMeterVariables
- props - (JMeterProperties - class java.util.Properties):操作jmeter属性,该变量引用了JMeter的配置信息,可以获取Jmeter的属性,它的使用方法与vars类似,但是只能put进去String类型的值,而不能是一个对象。对应于java.util.Properties。
a) props.get("START.HMS"); 注:START.HMS为属性名,在文件jmeter.properties中定义
b) props.put("PROP1","1234");
- prev - (SampleResult):获取前面的sample返回的信息,常用方法:
a) getResponseDataAsString():获取响应信息
b) getResponseCode() :获取响应code
更多方法可参考:org.apache.jmeter.samplers.SampleResult
四、操作变量:通过使用Bean shell内置对象vars可以对变量进行存取操作
a) vars.get("name"):从jmeter中获得变量值
b) vars.put("key","value"):数据存到jmeter变量中
五、操作属性:通过使用Bean shell内置对象props 可以对属性进行存取操作
a) props.get("START.HMS"); 注:START.HMS为属性名,在文件jmeter.properties中定义
b) props.put("PROP1","1234");
示例:
1、在Test Plan中添加一个变量:hello = kitty
2、Debug sampler-1和Debug sampler-2什么都不处理,用来查询对比beahshell处理前后的结果
3、BeanShell Sampler中的脚本如下:
4、运行结果:
- Debug sampler-1中显示:hello=kitty
- BeanShell sampler中 返回结果为:success
- Debug sampler-1中显示:hello=world,jmeter=111111
文献摘自:
http://www.cnblogs.com/puresoul/p/4915350.html
https://www.cnblogs.com/puresoul/p/4949889.html