欢迎, 游客
用户名 密码: 记住我
Jmeter 性能测试工具专版
  • 页:
  • 1

主题: JMeter中的Beanshell使用

JMeter中的Beanshell使用 2014-10-21 01:23 #1

  • summer
  • 离线
  • 板块老大
  • 帖子: 54
  • 声望: 0
1 Introduction

1.1 Overview

BeanShell是一个小的,免费的,用Java写的,面向对象的嵌入式的Java资源解释器。它动态的执行标准的Java语法,像Perl和JavaScript的脚本一样,是松散的类型,命令和方法关闭。

BeanShell is a small, free, embeddable Java source interpreter with object scripting language features, written in Java. BeanShell dynamically executes standard Java syntax and extends it with common scripting conveniences such as loose types, commands, and method closures like those in Perl and JavaScript.

1.2 Features

· Dynamic execution of the full Java syntax, Java code fragments, as well as loosely typed Java and additional scripting conveniences.

· Transparent access to all Java objects and APIs.

· The interpreter is small (150K jar file).

· Pure Java.

· Free.

2 Scenario

When we sign in JIRA, the HTTP request has a parameter named ‘st’. ‘st’ is changed when we login JIRA, so we can parameterized the st. However, the value of st should be transferred from Hexadecimal values to special characters. We need transfer it by beanshell.

我们设计了这样一个场景,当我们登陆JIRA,HTTP请求有一个参数叫st。st这个参数每次登陆的值都不一样,所以我们可以使st参数化。然而,st的值不能直接拿来用,有部分要从十八进制变成特殊的字符。我们需要这部分的转换使用Beanshell来完成。

3 Example

In this section, we use Beanshell Sampler as instance.在这一章节,我们使用了Beanshell Samplers作为例子。


Figure 4.1 Beanshell script

In above figure we can see following configuration:

Name: Descriptive name for this controller that is shown in the tree. The name is stored in the script variable Label.

Name是这个Beanshell Sampler的名字。相当于给Label(在上面的红框中变量)这个变量赋值。

Reset bsh.Interpreter before each call: If this option is selected, then the interpreter will be recreated for each sample. This may be necessary for some long running scripts.

如果这个选项是勾上的,解释器在使用到这个Beanshell Sampler的时候都会重新创建。如果是性能测试会跑很久的话,可以勾上这个选项。

Parameters: Parameters to pass to the BeanShell script. This is intended for use with script files; for scripts defined in the GUI, you can use whatever variable and function references you need within the script itself. The parameters are stored in the following variables:

参数可以在Beanshell语句中使用。如有个参数,名字叫st_login1,则可以在这里使用${st_login1}来取值,作为beanshell语句的参数的值。在beanshell语句中使用Parameters 或是bsh.args[0]来使用。

· >> Parameters - string containing the parameters as a single variable。

如果Parameters中只有一个参数,则可以Parameters为参数名来使用这个参数。

· >> bsh.args - String array containing parameters, split on white-spac。

如果是很多个参数,则参数作bsh.args这个数组的中的值,用bsh.args[number]来调用。每个参数之间用空格分开。

如:${st_login1} ${st_login2} ${st_login3} 相对用的,使用的时候就是bsh.args[0],bsh.args[1],bsh.args[2]

Script file: A file containing the BeanShell script to run. The file name is stored in the script variable FileName. (e.g. C:\Documents and Settings\wendy.xu\My Documents\JMeter\Demo\New Folder\Beanshell script.txt)

Script file配的是需要跑的包含Beanshell语句的文件的文件名。这样的话Script可以使空的。

Script: The BeanShell script to run. The return value (if not null) is stored as the sampler result.

sceipt是写Beanshell语句的。如果返回值不为空的话,会赋值给sampler result这个参数。

In purple rim is script. BeanShell understands standard Java statements, expressions, and method declarations. Statements and expressions are all of the normal things that you'd say inside a Java method such as variable declarations and assignments, method calls, loops, and conditionals.

在紫色的框中,Beanshell理解标准的Java语法,表达式,方法声明。

In red rim we can see below figure.

BeanShell in JMeter(Performance Test) —— JMeter中的Beanshell使用 - wendy - wendy的博客


Figure 4.1Variables Which We Can Use in Beanshell

上图中时可以在Beanshell中直接使用的变量。不需要声明,可以直接用的。

The full list of BeanShell variables that is set up is as follows:

· log - the Logger

· Label - the Sampler label

· FileName - the file name, if any

· Parameters - text from the Parameters field

· bsh.args - the parameters, split as described above

· SampleResult - pointer to the current SampleResult

· ResponseCode = 200 默认为200

· ResponseMessage = "OK" 默认为OK,可以通过ResponseMessag修改,如:ResponseMessage= “OKAY”

· IsSuccess = true 默认为true

· ctx - JMeterContext

· vars - JMeterVariables - e.g. vars.get("VAR1"); vars.put("VAR2","value"); vars.remove("VAR3"); vars.putObject("OBJ1",new Object()); 这是JMeter的变量,通过get来得到,put来添加,remove来删除。

· props - JMeterProperties - e.g. props.get("START.HMS"); props.put("PROP1","1234"); 这是JMeter的属性,通过get方法等到JMeter的属性,或是通过put方法添加属性。

Tips:

Priority of log (from high to low): DEBUG --> INFO --> WARN -->ERROR

Variables map to Object:下面是变量所对应的类名,可以通过链接看对象有哪些可以用的方法。

· log : org.apache.log.Logger - excalibur.apache.org/apidocs/org/apache/log/Logger.html

· Label : java.lang.String

· FileName : java.lang.String

· SampleResult : org.apache.jmeter.samplers.SampleResult - jmeter.apache.org/api/org/apache/jmeter/...rs/SampleResult.html

· ResponseCode : bsh.Primitive - www.beanshell.org/javadoc/bsh/Primitive.html

· ResponseMessage : java.lang.String

· IsSuccess : bsh.Primitive - www.beanshell.org/javadoc/bsh/Primitive.html

· ctx : org.apache.jmeter.threads.JMeterContext - jmeter.apache.org/api/org/apache/jmeter/...s/JMeterContext.html

· vars : org.apache.jmeter.threads.JMeterVariables - jmeter.apache.org/api/org/apache/jmeter/...JMeterVariables.html

· props : java.util.Properties

4 Develop Script in Beanshell

When we develop Beanshell script, we can see the error message in View Result Tree. In below Figure 4.2 Beanshell script we can see wrong statement with red underline.

当在开发Beanshell的时候,如果有错误信息,会显示在View Result Tree中。下图中红线标出的是语法错误的语句。



Figure 4.2 Beanshell script

When we run the script, in tab of Sampler result in View Ressult Tree we can see error message like Figure 4.3 Error Message in red rim.

当我们运行脚本的时候,在Sampler result标签下可以看到错误信息。



Figure 4.3 Error Message

5 Special Character to Hexadecimal

· + %2B
· / %2F

· ? %3F

· % %25

· # %23

· & %26

· = %3D

· : %3A

6 Reference

· Apache JMeter API:

jmeter.apache.org/api/org/apache/jmeter/...package-summary.html

· Beanshell Commands Document:

www.beanshell.org/manual/bshcommands.html

· Beanshell Manual:

www.beanshell.org/manual/bshmanual.pdf

· JMeter API:

· excalibur.apache.org/apidocs/overview-summary.html
没有无缘无故的爱,没有无缘无故的恨
  • 页:
  • 1