JMeter Bean Shell Use (full)

Source: Internet
Author: User
Tags access properties response code vars

What is a bean Shell BeanShell is a scripting language that conforms fully to the Java syntax and has some of its own syntax and methods; BeanShell is a loosely typed scripting language (this is similar to JS); BeanShell is written in Java, a small, free, downloadable, embedded Java source code interpreter, with Object scripting language features, a very compact interpreter jar file size of 175k. BeanShell executes standard Java statements and expressions, plus some script commands and syntax.

Official website: http://www.BeanShell.org/

Second, jmeter what Bean Shell

Timer: BeanShell Timer

Front Processor: BeanShell preprocessor

Sampler: BeanShell Sampler

Post Processor: BeanShell postprocessor

Assertion: BeanShell Assertion

Listener: BeanShell Listener

Iii. the use of BeanShell

This article describes the use of BeanShell preprocessor, other beahshell can be analogy. Here we use Beahshell to invoke our own written tool class, the tool class implements the encryption and decryption function of the password:

1, write the code in Eclipse, and then make the class into a jar package (right-click on the class->export->jar file)

2, put the jar bag under the JMeter directory \apache-jmeter-2.13\lib\ext

3, open JMeter, add an HTTP sampler (Invoke login interface), add a BeanShell under sampler preprocessor

4, in BeanShell Preprocessor import our jar package, call inside the Add, solve the password method, the result is saved in the JMeter variable, the following two methods are BeanShell of our most commonly used: Vars.get (String PARAMSTR): Get variable Value vars.put (String key,string value):, saving data to jmeter variable

Import com.pingan.ff.account.user.utils.*;

Encryption
SYSTEM.OUT.PRINTLN ("* * * * * * * * * * *");
String password = "123123";
String encode = securityutils.getkey (password);//Invoke Methods in the tool class for encryption
System.out.println ("Set my Encode");
Vars.put ("encode", encode);//save value to jmeter variable encode
("Getencode=vars.get");
System.out.println ("Get Me Encode:" + getencode);

5, the encrypted password to save the JMeter variable, and then in the HTTP sampler can be used through the ${encode}:

6. Execute script:

Four, Bean shell common built-in variables

JMeter has built-in variables in its BeanShell that allow users to interact with JMeter through these variables, with the main variables and how they are used as follows:

LOG: Write information to Jmeber.log file, using the method: Log.info ("This is log info!");

CTX: This variable refers to the context of the current thread, using the method reference: Org.apache.jmeter.threads.JMeterContext.

VARs-(Jmetervariables): Manipulate the JMeter variable, which actually refers to the local variable container (essentially map) in the JMeter thread, which is the bridge between the test case and the BeanShell interaction, commonly used methods:

A) Vars.get (String key): Get variable value from JMeter

b) Vars.put (String key,string value): Data is stored in the JMeter variable

More methods can be referred to: Org.apache.jmeter.threads.JMeterVariables

Props-(Jmeterproperties-class java.util.Properties): Operation JMeter Property, which refers to jmeter configuration information, can get the properties of JMeter, and it is used in a similar way to VARs. But it can only be put in a string of values, not an object. Corresponds to Java.util.Properties.

A) Props.get ("START").  HMS "); Note: Start.hms is the property name, defined in the file jmeter.properties

b) Props.put ("PROP1", "1234");

Prev-(Sampleresult): Get the information returned by the previous sample, common methods:

A) getresponsedataasstring (): Get response information

b) Getresponsecode (): Getting response code

More methods can be referred to: Org.apache.jmeter.samplers.SampleResult

Sampler-(Sampler): Gives access to the current sampler This section reads as follows:

The operation of a variable

Second, the operation of the property

Three, Custom function

Iv. referencing external java files

V. Referencing the external class file

Vi. referencing external jar packs

Vii. other uses (accept parameters, log, etc.)

Operation variable: The variable can be accessed by using the Bean Shell built-in object VARs

A) vars.get ("name"): Get variable value from JMeter

b) vars.put ("Key", "value"): Data is stored in the JMeter variable

Operational properties: Props can access properties by using the Bean Shell built-in object

A) Props.get ("START").  HMS "); Note: Start.hms is the property name, defined in the file jmeter.properties

b) Props.put ("PROP1", "1234");

Three, Custom function:

In BeanShell, we can use Java language custom functions to handle specific logic, with BeanShell built-in objects for variable access, convenient for us to test and improve the flexibility of the script.

Example:

1. Add a variable to test plan: Hello = Kitty

2, Debug sampler-1 and Debug sampler-2 do nothing, to query the results before and after the comparison Beahshell processing

3, BeanShell Sampler in the following script:

4, the results of the operation: Debug sampler-1 display: Hello=kitty BeanShell Sampler returned the result is: Success Debug sampler-1 display: hello=world,jmeter=111111

Iv. referencing external java files:

There is no way to feel that the custom function in (c) above is too cumbersome and unattractive. And if we already have out-of-the-box Java source files or class files, is there any way we can refer directly to Jemter? That's what this section is about, directly on the example:

1, if I have a Java source file, named: Myclass.java, the code is as follows:

Package test;

public class Myclass
{public
    int Add (int a, int b)
    {return
        a + b;
    }    
}

2, the Bean shell uses the following code:

In the bean Shel, you introduce Java through the source ("code path") method, and then call the method and Java, new class, and then call the Add method inside.

3. Operation Result:

V. Referencing the external class file:

Now I know how to reference external files, sometimes if we only have class files. In fact, you can also refer to the class file directly in JMeter, as shown in the following example:

1, directly to the example in the Java file compiled into class files, how to compile your own Baidu.

2, the Bean shell uses the following code:

Use the Addclasspath ("d:\\") method to introduce the class file, import the packages and classes with import, and then invoke the Java-like

3. Operation Result:

Vi. referencing external jar packages:

Above four or five describes how to reference external Java and class files, if the file is more than we can make them into a jar package and then call in the Jemter, how to use it can be seen in my previous introduction: JMeter Bean Shell Use (i).

One thing to add here is the way in which jars are introduced in JMeter:

1, in the previous article has been used: put the jar bag under the JMeter directory \apache-jmeter-2.13\lib\ext

2, add the jar package that you want to reference directly at the bottom of the right panel of test plan, as follows:

Vii. Other uses:

1. In test plan, define the following three variables:

2, the Bean shell can be scripted as follows:

A, the bean shell can accept incoming parameters, as shown in the following figure: ${u1} ${u2} ${u3}

b, parameters can be extracted in order by bsh.args[]

c, the bean shell provides a built-in variable parameters to hold the collection of parameters

3. Operation Result:

The two sentences entered in the following figure 1 are set:

Responsecode = 500;
Responsemessage = "This is a test";

The two sentences entered in the following Figure 2 are set:

Log.info (Parameters);

Log.info (Label);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.