First, what is the bean Shell
BeanShell is a scripting language that conforms fully to the Java syntax specification and has its own syntax and methods. BeanShell is a loosely typed scripting language (this is similar to JS); BeanShell is written in Java, a small, free, downloadable, embedded Java source code interpreter with object scripting language features and a very streamlined interpreter jar file size of 175k. BeanShell executes standard Java statements and expressions, plus some scripting commands and syntax.
Official website: http://www.BeanShell.org/
Ii. What Bean Shell does JMeter have?
Timer: BeanShell Timer
Front Processor: BeanShell preprocessor
Sampler: BeanShell Sampler
Post Processor: BeanShell postprocessor
Assertion: BeanShell Assertion
Listener: BeanShell Listener
Third, the use of BeanShell
In this introduction, the use of BeanShell preprocessor, other beahshell can be analogous. In this we use the Beahshell call ourselves to write the tool class, the tool class implements the password encryption, decryption function:
1, write the code in Eclipse, and then make the class into a jar package (right-click on the class->export->jar file)
2. Put the jar package into the JMeter directory \apache-jmeter-2.13\lib\ext
3. Open JMeter, add an HTTP sampler (call Login interface), add a BeanShell under sampler preprocessor
4, in BeanShell Preprocessor import our jar package, call inside the Add, solve the password method, the results are stored in the JMeter variable, the following two methods are the most commonly used in BeanShell:
- Vars.get (String paramstr): Get variable Value
- Vars.put (String key,string value): To save the data to the JMeter variable
Import com.pingan.ff.account.user.utils.*;//EncryptSystem. out. println ("* * * * ENCRYPTION * * *"); String Password="123123"; String encode=securityutils.getkey (password);//Invoke the method in the tool class to encrypt the system. out. println ("Set My encode"); Vars.put ("encode", encode);//Save the value to the JMeter variable encode string Getencode=vars.Get("encode"); System. out. println ("Get my encode:"+ Getencode);
5, the encrypted password is stored in the JMeter variable, and then in the HTTP sampler can be used through ${encode} :
6. Execute script:
Four, Bean shell common built-in variables
JMeter has built-in variables in its BeanShell that allow users to interact with JMeter, the main variables and how to use them as follows:
Log: Write information to Jmeber.log file, using method:log.info ("This is Log info!");
CTX: This variable refers to the context of the current thread and is used in the following way: Org.apache.jmeter.threads.JMeterContext.
VARs -(jmetervariables): This variable actually refers to the local variable container in the JMeter thread (essentially map), which is the bridge between the test case and BeanShell interaction, common methods:
A) Vars.get (String key): Gets the value of the variable from the JMeter
b) Vars.put (String key,string value): Data is stored in the JMeter variable
More ways to see: org.apache.jmeter.threads.JMeterVariables
Props -(Jmeterproperties-class java.util.Properties): This variable refers to the JMeter configuration information, which is used in a similar way to VARs, But it can only be put in a string-type value, not an object. Corresponds to Java.util.Properties
prev -(Sampleresult): Gets the information returned by the previous sample, common methods:
A) getresponsedataasstring (): Get response information
b) Getresponsecode (): Get Response code
More ways to see: Org.apache.jmeter.samplers.SampleResult
Website:
Http://jmeter.apache.org/usermanual/component_reference.html#BeanShell_Sampler
Http://jmeter.apache.org/usermanual/component_reference.html#BeanShell_PreProcessor
JMeter's Bean Shell uses