JMeter of Bsf,beanshell (reprint)

Source: Internet
Author: User
Tags vars

JMeter cannot handle javascript on its own, but it can be used with its own BSF preprocessor (BSF: Java-oriented scripting language, JavaScript support)

(Use this before you put the Bsh-2.0b2.jar bag into the JMeter Lib)
For the sale of the consignment information, for example:
The page has a totalcommission variable whose value is calculated based on the value of price and amount, where a pre-processor can be added to the Sampler Request page that publishes the consignment: Bsfpreprocessor
Such as:


One of the script scripts is:
Vars.put (' Price ', ${price});
var priceinput =vars.get (' price ');
(Here are the variables to be declared, as here:Vars.put (' Price ', ${price}), you directly use the Log.info (price) words will be an error, first declare it: Varpriceinput =vars.get (' price ');And then Log.info (priceinput) words will be OK, do not know why: The world's big, wonders!! )
Vars.put (' Amount ', ${amount})
var quantity =vars.get (' Amount ');
var commissionrate=vars.get (' Commissionrate ');
Vartotalcommission=vars.get (' totalcommission ');(note here to define vartotalcommission, if the direct use of vars.get (' totalcommission ') will be an error, as to why is not too clear)
Log.info ("Before total:" +priceinput);
Log.info (' commissionrate= ' +commissionrate);

var additionalcommission=vars.get ("Additionalcommission");

Log.info (' additionalcommission= ' +additionalcommission);

var Commissiontext;
Commissiontext = priceinput*commissionrate;
Commissiontext = commissiontext.tofixed (3);

Log.info (' commissiontext= ' +commissiontext);

var temp = parseint (commissiontext);

Log.info (' temp= ' +temp);

if (Temp<commissiontext)
{temp+=1;}


Commissiontext=parseint (temp) +parseint (additionalcommission);
Log.info (' commissiontext1= ' +commissiontext);

Vars.put ("Totalcommission", Commissiontext);

Log.info ("After Total:" +vars.get (' totalcommission '));
------------------------------------------------------------------------------------------------



If there is a problem in the processing, you can go to the JMeter bin to see Jmeter.log see where the problem is, the corresponding can solve it
--------------------------------------------------------------------------------------------------
The above problems can also be solved with BeanShell preprocessor:
BeanShell is a small, free, embeddable, Java code interpreter with object-oriented scripting language features. It is written in the Java language. It executes standard Java statements and expressions, and comes with simple script commands and syntax. It treats programming objects as a simple method, much like Perl and JavaScript. You can use BeanShell when writing Java tests or debugging, or you can use it as a script for your application. In short, BeanShell can dynamically interpret the Java language. Which means that BeanShell is useful to Java in many ways, like tcl/ TK is useful for C: BeanShell is an embeddable---you can call BeanShell from your application at run time to execute Java code dynamically or to provide script extensions for your application. Instead, you can call your application and its objects from BeanShell, which allows Java objects and APIs to run dynamically. Because BeanShell is written in Java, it can be run in the same JVM space as your application, and you are free to pass the reference (References) of the real-time object into the script code and return as a result.
It's like BSF. Create a predecessor Processor: BeanShell Preprocessor
The code is as follows:
var priceinput=${price};
Log.info ("price=" +priceinput);
var quantity=${amount};
Log.info ("quantity=" +quantity);

var commissionrate=vars.get ("Commissionrate");
Float abc=float.parsefloat (commissionrate);
Log.info ("commissionrate=" +ABC);

float COMMISSIONTEXT=PRICEINPUT*ABC;
Log.info ("commissiontext=" +commissiontext);

int temp= (int) commissiontext+1;
Log.info ("temp=" +temp);

var additionalcommission=vars.get ("Additionalcommission");
Log.info ("additionalcommission=" +additionalcommission);
Commissiontext=temp +integer.parseint (additionalcommission);
Log.info ("=" +commissiontext);

Vars.put ("Totalcommission", String.valueof (Commissiontext));
Because the variable definition of BeanShell is relatively loose, it is easy to make mistakes when converting, so be careful here.

. BeanShell Script Processing: If the BeanShell script is used in the test case, you will need to copy the BeanShell package to the JMeter Bin directory. BeanShell is a lightweight scripting language compatible with the Java language, which may be used frequently in JMeter scripts for log processing, regular expression post-processing (post-process), and so on. If you use Mail Visualiser, mail Reader, and Web Services (SOAP) sampler in your test cases, you will need to copy the mail package to the JMeter Bin directory. If you use JMS-related sampler in your test case, you will need to copy the JMS package to the JMeter Bin directory.
Http://www.ibm.com/developerworks/cn/webservices/1004_sunch_jmeter_soap/index.html

JMeter of Bsf,beanshell (reprint)

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.