Jmeter之Beanshell使用(一)

转自贺满(http://www.cnblogs.com/puresoul/p/4915350.html)

一,什么是Bean Shell

BeanShell是一种完全符合Java语法规范的脚本语言,并且又拥有自己的一些语法和方法;

BeanShell是一种松散类型的脚本语言(类似JS);

BeanShell是用Java写成的,一个小型的、免费的、可以下载的、嵌入式的Java源代码解释器,具有脚本语言特性,非常精简的解释器jar文件大小为175k;

BeanShell执行标准的Java语句和表达式,另外包括一些脚本命令和语法。

二,Jmeter有哪些BeanShell

定时器:BeanShell Timer

前置处理器:BeanShell PreProcessor

采样器:BeanShell Sampler

后置处理器:BeanShell PostProcessor

断言:BeanShell duanyan

监听器:BeanShell Listener

三、BeanShell的用法

四、BeanShell常用内置变量

Jmeter在它的BeanShell中内置了变量,用户可以通过这些变量与Jmeter进行交互,其中主要的变量及其使用方法如下:

log:写入信息到jmeter.log文件,使用方法:log.info("This is log info!");

ctx:该变量引入了当前线程的上下文,使用方法参考官网org.apache.jmeter.threads.JMeterContext

vars(JMeterVariables):操作Jmeter变量,这个变量实引用了Jmeter线程中的局部变量容器(本质上是Map),它是测试用例与BeanShell交互的桥梁,常用方法:

1、vars.get(String key):从jmeter中获得变量值

2、vars.put(String key,String value):讲数据存到Jmeter变量中

更多方法可参考:org.apache.jmeter.threads.JMeterVariables

props(JMeterProperties-class Java.util.properties):操作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

更多方法可参考:org.apache.jmeter.samplers.SampleResult

sampler - (Sampler):gives access to the current sampler

原文地址:https://www.cnblogs.com/yay1101/p/7976218.html