Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use BeanShell script in Jmeter

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use the BeanShell script in Jmeter, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

When using jmeter for interface testing or performance testing, we need to deal with some complex requests, so we need to use beanshell scripts. BeanShell is a scripting language that fully conforms to the Java syntax specification, and has its own syntax and methods, so it can connect seamlessly with java. Beanshell cannot be debugged on some integrated code tools because of its built-in variables.

After learning some basic syntax of Java, you can write BeanShell scripts in Jmeter.

Create a BeanShell sampler under the thread group directly, and then write the java code in the script area:

Write a loop, which can't be seen here after execution, but can be seen in jmeter.bat 's windows batch:

JMeter has built-in variables in its BeanShell that allow users to interact with JMeter:

1. Log: write information to jmeter.log file, using method: log.info ("h")

2. Vars: manipulate the jmeter variable, which actually refers to the local variable container in the JMeter thread (essentially Map). It is a bridge between the test case and BeanShell.

A) vars.get (String key): get the value of a variable from jmeter

B) vars.put (String key,String value): data is stored in the jmeter variable

Add log view:

Use vars:

In thread group 2, access the vars variable and output null, which shows that vars is indeed a local variable:

3. Props: manipulate the jmeter attribute, which refers to the configuration information of JMeter and can get the properties of Jmeter. It is similar to vars, but can only put the value of String type, not an object. Corresponds to java.util.Properties.

The overall situation of props:

4. Prev: get the information returned by the previous sample. Common methods:

A) getResponseDataAsString (): get response information

B) getResponseCode (): get the response code

Send an API request first, and then prev gets the returned information:

Custom functions: use Java custom functions to deal with specific logic, combined with BeanShell built-in objects for variable access to improve the flexibility of the script.

Write a custom static function that implements the accumulation of integers:

When an error is found, it turns out that the return value of the function is of type int, while log.info () requires String. There are three ways to convert result of type int to string:

1. String.valueOf (result)

2. Integer.toString (result)

3. Result+ ""

You can choose one.

Reference an external java file: introduce the Java file in BeanShell through the source ("code path") method, and then call the method, like java, new an object, through the

Object calls the methods inside.

Write a static method, save it as a Test.java file, and put it on disk D.

Then reference Test.java in BeanShell, call function, pass parameter 100, log output:

Referencing an external jar package

First, pack:

1. Run the current Test as the running file first.

2. Right-click the project name-Export--Runnable JAR file.

3. Find the running file in Launch and put it under lib/ext under the jmeter installation directory.

4. Import the package in the BeanShell of Jmeter and call the method.

Here I reported an error, saying that the method could not be found. After groping for a long time, I tried to add a jar package to the test plan, and it was a success.

The above is all the content of the article "how to use BeanShell scripts in Jmeter". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report