Jmeter (20) BeanShell or JSR223

Source: Internet
Author: User
Tags response code

The section on BeanShell and JSR223 components, long ago wanted to write a large essay to record, struggling to describe the contents of these two parts, has been revising the essay.

Introduce BeanShell:

BeanShell is lightweight Java, supports object-style scripting language features, can be embedded in Java source code, dynamically executes Java source code and extends some of the scripting language features, such as JavaScript and Perl weak type, imperative, Closure functions, and so on, are a cinch. (Soaring Portal: http://www.beanshell.org/)

BeanShell has a fairly strong position in JMeter, known as the JMeter scripting language of King. But a while ago saw an article in Blazemeter----"is BeanShell Dead?" ", read it carefully, The general idea is that since Jmeter3.1, JSR223 's scripting language has been the default for groovy, and developers like groovy more than BeanShell, but for us, is BeanShell or groovy chosen? I don't feel the need to discuss such a topic at this stage. But I think the appropriate change is necessary, blindly follow suit that is absolutely taboo! Interested can go to see what the root cause is. Jin Yong's father is such a description of solitary seeking defeat: grass and bamboo stone can be swords. Since then, gradually into a sword-free environment. Sword God is not how superb, but the skill is too deep, grass and bamboo stone can be a sword! BeanShell, groovy can be a sword! Haha, look at the BeanShell and JSR223 in JMeter and the functions they can achieve!

First, the BeanShell and JSR223 components are components involved in the Code section, and as for the use, the energy resides in one by one records:

BeanShell and JSR223 are involved in almost every part of the JMeter:

1, Sampler

2, preprocessor

3, Postprocessor

4, Timers

5, assertions

6, Listeners

Then first add a BeanShell Sampler:

  

There are five parts of the picture, almost all except the name and comment, which is very important. To do one by one reading:

Post Official documents first:

  

Rest BSH. Interpreter before each call: (check box) do not explain, see the examples given in the official documentation (or the best Practices-beanshell seripting)

Parameters (String Parameters and string "" Bah.args): Pass parameters that pass the Parameters parameters created in the GUI script to the BeanShell script. References in BeanShell scripts are instantiated using Bsh.args "X".

Script file: Import the BeanShell script to run the files. The file name is stored in the script variable name.

Script: Scripting place. (BeanShell syntax)

And the bottom of the paragraph, I was deliberately framed, many people may be ignored, but the variables enumerated in this paragraph are defined for the BeanShell script:

Sampleresult, Responsecode, Responsemessage, issuccess, Label, FileName, CTX, VARs, props, log

Used people are certainly familiar with the variables here, the specific purpose of the same name!

OK, the last simple example:

  

Random functions are used as random response data.

  

Save the response result of the dummy sampler and the response data to the "response_data" variable.

Add BeanShell Sampler To use this variable:

  

Print the variable information of the response_data to the log;

Print the label to the log;

Defines the response status code and response information for the sampler.

For intuitive, add a debug sampler to view variable reads:

  

The random number of responses is 56

  

At the top of the BeanShell sampler defines the response code and message.

  

The variable read for Debug sampler is correct.

  

The response data and the label name are printed in the log panel.

Of course, logical judgments and the introduction of external jar packages are still supported if necessary.

A small demo that writes data to a file:

    

Determine if there is no such file in the directory (the default is/bin directory), if it exists, delete it, create a new file

  

Dummy Sampler Custom Write request and response information. (involving functions __uuid, __iterationnum)

  

The ID, name, and password of the response information are extracted and defined as the new variable.

  

Writes the extracted data to the file.

OK, I've run it all over again to see if writing data is successful.

  

My/bin directory is the file that was generated, open data is written:

  

You can see that the write was successful.

Setup Group Thread does the work of creating files and writing data.

Bzm-concurrency Thread group----------This is roughly the data process in a test setup thread group.

  

The generated file is read in a parameterized manner under the thread group:

  

  

Ok,beanshell is used almost to deal with complex business logic, such as generating random phone numbers, random ID numbers, encryption, decryption and other services. The flexible point is that you can instantiate external Java files.

  

Jmeter (20) BeanShell or JSR223

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.