当前位置:网站首页>The basic usage of JMeter BeanShell. The following syntax can only be used in BeanShell

The basic usage of JMeter BeanShell. The following syntax can only be used in BeanShell

2022-07-06 01:04:00 Amen's love

One 、 background

BeanShell By java Compiling , Is a lightweight scripting language , It is also equivalent to a small free JAVA Source interpreter , Support for object-oriented scripting language features , It can also be embedded in JAVA In the source code , Can dynamically execute JAVA Source code and extends some features of scripting language .

Two 、BeanShell Bring your own grammar

// 1. Log printing 
log.info(" This is my first beanshell");
log.error(" This is my first beanshell");
// 2. Variables within a thread   It can be done by 
vars.put("name"," Zhang San ");
String name = vars.get("name");
log.info("----- Thread variable name:"+name);
log.info(vars.get("age")==null?"20":vars.get("age"));

// 3. Add get global variables 
props.put("_name"," Li Si ");
String _name = props.get("_name");
log.info("----- Global variables _name:"+_name);
// 4.  Get the sampler in front 
String preCode = prev.getResponseCode();
String preData = prev.getResponseDataAsString();
log.info(" Get the shape code of the previous sampler ---》"+preCode);
log.info(" Get the return value of the previous sampler ---》"+preData);

3、 ... and 、 quote java Code processing complex logic

3.1 Mode one direct writing java Code

public int add(int a,int b)   {
        int count = a+b;
        return count;
}
log.info("-----------a+B="+add(1,2));

3.2 Mode two introduce .java file

//  Mode two   call .java  file 
source("D:/MainTest.java");
MainTest test = new MainTest();
log.info("-----------C+D="+test.add(2,2));

3.3 Mode three introduce .jar file

Failure !

原网站

版权声明
本文为[Amen's love]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060102330125.html