(13) Use of JMeter Bean Shell (ii)

Source: Internet
Author: User
Tags vars

This article from: http://www.cnblogs.com/puresoul/p/5092628.html

Previous jmeter Bean Shell Use (a) a brief introduction of the bean shell in the next jmeter, this article is a supplement to the above, the main summary of the several commonly used scenarios and methods, Believe that these basic can cover most of the demand. This section reads as follows:

First, the operation variable

Second, Operation Properties

Iii. Custom Functions

Iv. referencing external java files

V. Referencing an external class file

Vi. referencing external jar packages

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

One, the action variable: the variable can be accessed by using the bean shell built-in object VARs

A) vars.get ("name"): Gets the value of the variable from the JMeter

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

Operation Properties: Property can be accessed by using the bean shell built-in object props

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

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

Third, Custom functions:

In BeanShell, we can use the Java language Custom function to handle the specific logic, combining the BeanShell built-in objects for variable access, so that we test to improve the flexibility of the script.

Example:

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

2, Debug sampler-1 and debug Sampler-2 Nothing to deal with, used to query comparison Beahshell processing results before and after

3. The script in BeanShell Sampler is as follows:

4. Operation Result:

    • Debug sampler-1 display: hello=kitty
    • The result returned in BeanShell Sampler is: Success
    • Debug sampler-1 display: hello=world,jmeter=111111

Iv. Referencing external java files:

Is there any way to think that the custom function in the above (c) is too cumbersome and not beautiful? And if we already have a ready-made Java source file or class file, is there any way we can reference it directly in Jemter? This is what this section is going to cover, directly on the example:

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

Package test;  Class myclass{    int Add (intreturn a + b;}}      

2. The Bean shell uses the following code:

Introduce Java into the bean Shel by means of the source ("code path") method, and then call the method like Java, new class, and then call the Add method inside.

3. Operation Result:

V. Referencing an external class file:

Now you know how to reference external files, sometimes what if we only have class files? In fact, you can also refer to the class file directly in JMeter, for example:

1, directly to the example of the Java file compiled into a class file, how to compile please self-Baidu.

2. The Bean shell uses the following code:

Using the addclasspath ("d:\\") method to introduce the class file, import the package and class with import, and then you can call it like Java

3. Operation Result:

Vi. referencing an external jar package:

The above four or five describes how to reference external Java and class files, if the file is a long way we can make them into a jar package and then call in Jemter, how to use can be seen in my previous article has introduced: JMeter Bean Shell Use (a).

One thing to add here is the way the jar is introduced in JMeter:

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

2. Add the jar packages that need to be referenced directly at the bottom of the test plan's right panel, such as:

Vii. Other uses:

1. Define the following three variables in test plan:

2. The Bean shell can be scripted as follows:

A, bean shell can accept incoming parameters, such as: ${u1} ${U2} ${u3}

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

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

3. Operation Result:

In 1, enter these two sentence settings:

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

In 2, enter these two sentence settings:

Log.info (Parameters);

Log.info (Label);

Website:

Http://jmeter.apache.org/usermanual/component_reference.html#BeanShell_Sampler

(13) Use of JMeter Bean Shell (ii)

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.