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

Common Beanshell Sampler methods of jmeter Learning Guide

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

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.

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

Internet Technology

Wechat

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

12
Report