Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Common Beanshell Sampler methods of jmeter Learning Guide

Shulou Source: shulou.com Published: 2022-06-03 06:24:40 09月12日 Update

Video address: http://edu.51cto.com/lecturer/4626073.html

In JMeter, you can use Beanshell Sampler to write some simple scripts to achieve more functions.

1 、 Beanshell

Beanshell is a lightweight Java script written in pure Java that dynamically executes standard java syntax and some extended script syntax, similar to javaScript and perl.

2. Built-in variables

In addition to the standard java syntax available in Beanshell Sampler, there are also some well-defined variables that can be used directly.

SampleResult

ResponseCode

ResponseMessage

IsSuccess

Label

FileName

Ctx

Vars

Props

Log

Next, I will talk about the most commonly used variables according to the frequency of use.

3 、 log

Used to print logs, the most common and the simplest

Print in jmeter.log, you can set the print level, you can print strings, variables, and so on.

How to use it:

1. Print a normal string log in info form:

Log.info ("hello world"); output hello world

2. Concatenate strings and variables (where token is a jmeter local variable):

Log.info ("hello world" + "${token}"); outputs the stitching results of hello world and token variables

3. Print custom variables

Str = "12345"

Log.info (str); output 12345

4. Print a normal string log in error form:

Log.info ("hihihi")

Log.error ("aaaaa")

Differences in output:

2018-12-27 19 0515 INFO o.a.j.u.BeanShellTestElement: hihihi

2018-12-27 19 0515 ERROR o.a.j.u.BeanShellTestElement: aaaaa

Print () prints the log

In beanshell, you can also use the print () function to print logs, output strings and other information.

Print () outputs information in the console, and log () outputs information in jmeter.log by default.

4 、 vars

Used to access jmeter local variables, very commonly used, be sure to master

It is usually used to access the contents of a string, as well as objects.

Vars.get ()

String mykey = vars.get ("keyname")

Get the value of the variable named keyname and save it in mykey.

Vars.put ()

Vars.put ("keyname", "value")

Save the variable keyname (value value) to the jmeter variable.

Vars.putObject ("OBJname", new Object ())

Save an object OBJname to the jmeter variable

Vars.remove ()

Vars.remove ("keyname")

Remove keyname from the jmeter variable.

5 、 props

Static variables used to access the jmeter global

Both key and value are in the form of string

Ymd = props.get ("START.YMD")

Gets the value of the property START.YMD (the date the script started).

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

Save 1234 in the global property PROP1.

6 、 ctx

Context information for the current thread

Examples of use:

Ctx.getCurrentSampler (); get the current sampler request

Ctx.getPreviousSampler (); get the previous sampler request

Ctx.getThreadNum (); gets the sequence number of the current thread, counting from 0

Ctx.getThread (); get the current thread

Ctx.getThreadGroup (); gets the current thread group

Ctx.getProperties (); get all attributes

Ctx.getVariables (); gets all variables of the current thread

7 、 SampleResult

SampleResult.setResponseData (data)

Custom response data.

Tags: Variables characters strings output logs threads information scripts access commonly used attributes forms syntax normal global object local is in standard method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Apple Linux Shulou Information Microsoft