JMeter BeanShell详解

一、BeanShell介绍

下面直接引用官网的介绍译文:

BeanShell是一个小型,免费,可嵌入的Java源代码解释器,具有用Java编写的对象脚本语言功能。 BeanShell动态执行标准Java语法,并使用常见的脚本编写方便性扩展它,例如松散类型,命令和方法闭包,如Perl和JavaScript中的那些。

官网地址:http://www.beanshell.org/

BeanShell Github:https://github.com/beanshell/beanshell

 

二、BeanShell组件

JMeter有如下BeanShell组件:

  • BeanShell Timer(定时器)

  • BeanShell PreProcessor(前置处理器)

  • BeanShell Sampler(采样器)

  • BeanShell PostProcessor(后置处理器)

  • BeanShell  Assertion(断言)

  • BeanShell Listener(监听器)

一般我们用得最多的就是BeanShell PreProcessor、Sampler、PostProcessor和Assertion。

 

三、JMeter BeanShell的基础用法

这里以BeanShell PreProcessor为例,为接口数据添加随机数。

首先添加线程组,在线程组下添加BeanShell Sampler和查看结果树,再在Sampler下添加BeanShell PreProcessor,BeanShell PreProcessor的脚本内容如下:

vars.put("mobile", "123456789");

BeanShell Sampler内容如下:

String randomMobile = "${mobile}";
return randomMobile;

BeanShell执行到return时,就会把return的值作为responseData并且脚本执行结束。

四、JMeter BeanShell的内置变量

JMeter在内嵌BeanShell时默认创建了一些内置变量,以方便使用者调用,以下是一些常用的BeanShell内置变量:

  • vars
  • props
  • ctx
  • log
  • Label / SampleLabel
  • data / SamplerData
  • IsSuccess
  • prev / SampleResult
  • ResponseData
  • ResponseCode
  • ResponseMessage
  • Failure
  • FailureMessage
  • Response.setStopThread(boolean)
  • Response.setStopTest(boolean)

请注意,以上变量并不是每个BeanShell组件都有,也并不是每个变量都支持读和写,具体请查看组件的详细说明,接下来将一一讲解。

1)vars

vars可以说是最常用的内置变量,其类型为JMeterVariables,其作用是添加或获取JMeter线程变量:

// 添加JMeter线程变量
vars.put("key", "name");

// 获取JMeter线程变量
vars.get("key");

// 在脚本文本中引用变量
${key}

JavaDoc:http://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html

2)props

vars是不能跨线程组使用的,即在线程组A中put的值在线程组B中时get不到的,props就是为了解决上述问题:

// 添加属性变量
props.put("key", "name");

// 获取属性变量
props.get("key");

// 在脚本文本中引用变量
${__P(key)}

JavaDoc:https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html

3)ctx

ctx是当前线程的上下文,其类型为JMeterContext,该变量能做的事非常多,一般需要的动作在其他内置变量里都有提供,除非要做一些复杂的线程控制,详细查看JavaDoc。

JavaDoc:http://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterContext.html

4)log

log就是输出不同等级的日志:

log.debug("xxx");
log.info("xxx");
log.warn("xxx");
log.error("xxx");

JavaDoc:https://www.slf4j.org/api/org/slf4j/Logger.html

5)data和SamplerData

data和SamplerData就是sampler data(请求数据),其类型为byte []:

String samplerData = new String(data);

6)Label和SampleLabel

Label和SampleLabel是sampler的标题,其类型是String。

7)IsSuccess

IsSuccess表示sampler的成功失败,其类型为boolean。

8)prev和SampleResult

prev和SampleResult是当前sampler的结果,其类型为SampleResult,它可以读写sampler的信息和控制sampler的行为。

JavaDoc:http://jmeter.apache.org/api/org/apache/jmeter/samplers/SampleResult.html

9)ResponseData

ResponseData就是sampler response data(响应数据),其类型为byte []:

String samplerData = new String(ResponseData);

10)ResponseCode和ResponseMessage

ResponseCode和ResponseMessage是响应报文的响应码和响应信息,其类型为String。

11)Failure和FailureMessage

Failure和FailureMessage是BeanShell Assertion组件独有的内置变量,其作用是设置当前sampler的测试结果(成功或失败),Failure的类型是boolean,FailureMessage的类型是String。

// 设置sampler为测试通过
Failure = true;

// 设置sampler为测试失败
Failure = false;
FailureMessage = "测试失败";

12)Response

Response其实就是SampleResult,为啥这样设计我也不懂,估计是满足不同的语意吧。

在BeanShell Assertion组件,有两个常用的方法Response.setStopThread(boolean)、Response.setStopTest(boolean),其作用是停止执行当前sampler。他们的区别是:

  • setStopThread(boolean)只停止sampler所在的线程
  • setStopTest(boolean)停止整个jmeter测试
Failure = false;
FailureMessage = "测试失败";
Response.setStopThread(true)

一般应用的场景是使用BeanShell Assertion组件进行复杂的数据断言时,如断言失败则停止继续执行测试。

 

 

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JMeter是一款功能强大的负载测试工具,而Beanshell则是JMeter中一个非常重要的组成部分。Beanshell是一个基于Java语言的脚本语言,它允许你在JMeter中使用Java代码来进行各种操作,包括读取文件、发送邮件、执行数据库操作等等。 以下是JMeter Beanshell的安装使用步骤: 1. 下载Beanshell 在官网下载最新的Beanshell版本,解压到任意目录。 2. 将Beanshell添加到JMeter的classpath中 打开JMeter的bin目录下的jmeter.bat(Windows)或jmeter.sh(Linux),找到如下代码: set CLASSPATH=%CLASSPATH%;%JMETER_HOME%\lib\ext\ApacheJMeter_core.jar;%JMETER_HOME%\lib\jorphan.jar 将其修改为: set CLASSPATH=%CLASSPATH%;%JMETER_HOME%\lib\ext\ApacheJMeter_core.jar;%JMETER_HOME%\lib\jorphan.jar;%BEANSHELL_HOME%\bsh-2.0b4.jar 其中,BEANSHELL_HOME为Beanshell的安装目录。 3. 在JMeter中使用BeanshellJMeter中使用Beanshell非常简单。只需要添加一个Beanshell Sampler,然后在Sampler中编写Beanshell脚本即可。 例如,以下是一个简单的Beanshell脚本,用于输出当前时间: import java.text.SimpleDateFormat; import java.util.Date; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentTime = format.format(new Date()); log.info("Current time is " + currentTime); 在Sampler中选择Beanshell Sampler,然后将以上脚本复制到Script框中即可。运行测试计划后,可以在JMeter的日志中看到输出的当前时间。 总结 JMeter BeanshellJMeter中非常重要的组成部分,它可以让你在测试中使用Java代码来进行各种操作。安装和使用Beanshell非常简单,只需要下载Beanshell,将其添加到JMeter的classpath中,然后在Sampler中编写Beanshell脚本即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值