Jmeter5.2.1

JDK1.8

directory

Jmeter series test plan, thread group, sampler (1) Jmeter series configuration components (1) Jmeter series listeners (1) Jmeter series controllers (1) Jmeter series controllers (2) Scope and execution sequence of Jmeter series Use BeanShell of Jmeter series

BeanShell is a scripting language that is fully compliant with Java syntax. It is a lightweight Java script, so BeanShell and Java can be seamlessly integrated. BeanShell features: 1. Dynamic execution of complete Java syntax, Java code snippets, and loosely typed Java and other scripts. 2. Java is very strict about formatting and must be written in a specified format to pass compilation time, such as a Java class:

public class HuMan{
	/ / property
    private Integer gender;
    
    / / method
    public Integer getGender(a) {
    	return this.gender; }}Copy the code

So if I want to print Hello World, what do I say in BeanShell and Java?

// Java[Application class name is arbitrary]
public class Application {
	public static void main(String[] args) {
    	System.out.println("Hello, world"); }}Copy the code
// BeanShell
// Note that the print is printed to the Jmeter console
System.out.println("Hello, World");
Copy the code

The above two examples clearly show that BeanShell is a lightweight Java script, and no longer adhere to various formats. At the same time, BeanShell can access Java objects and APIS, so we can use BeanShell to call some Java classes written by ourselves in Jmeter. So what bean shells are in Jmeter? 1, BeanShell Timer: 1, BeanShell PreProcessor: 3, BeanShell Sampler: 4, BeanShell PostProcessor: 5, assert: Assertion 6, BeanShell Listener

BeanShell built-in objects

Each BeanShell element has its own built-in object, and it also has a description of what the current element’s built-in objects are. For example, at the bottom of the BeanShell Sampler element there is a hint of what the element’s built-in objects are.


log Jmeter uses log4j to record logs. Generally, log.info() and log.error() are used. The printed logs are recorded to the bin/jmeter.log file
props Manipulate the Jmeter properties, that is, the configuration of the jmeter.properties file
Props. Get (” HTTPS. Use. Cached. SSL. The context “) : to obtain the corresponding attribute values
Props. The put (” HTTPS. Use. Cached. SSL. The context “, false) : save the data to the Jmeter properties, if the property does not exist is created
vars Are objects of class JMeterVariables that are used by internal methods,Please have a look at 

When manipulating Jmeter variables, note that Jmeter variables are copied to threads when they are started, and are similar to local variables of threads, so updating a variable in one thread does not affect another thread
Vars.get (“name”): Gets the variable value from the thread
Vars.put (“key”, “value”): Saves data to the thread, created if the variable does not exist
ctx Is a jMeterContext-like object that holds the context of a thread. This object is not thread safe and is recommended for single-threaded use.Please have a look at
prev Is an object of class SampleResult that holds information about the previous request, used by specific internal methods,Please have a look at 

Pay attention to

According to the last videoScope and order of executionWe know that there are components that execute before the sampler and components that execute after the sampler. For components that execute before the sampler, prev represents information about the previous request, and for components that execute after the sampler, prev represents information about the current request!
data The type is byte[], which is an array of bytes,

String STR = new String(data, “UTF-8 “)


BeanShell Sampler

Element is introduced




























SampleResult Is an object of class SampleResult that holds information about the current request and response.Please have a look at
ResponseCode The status code of the response is 200 by default
ResponseMessage Response message, default is “OK”
IsSuccess Whether the request was successful. Default is true
Label The sampler label, the Name field
FileName File name, if Script file has an external BSH file


Write a script

At this point, we know a little bit about BeanShell Sampler, so let’s start writing some scripts. First, let’s figure out what the script does. In the initial learning phase, you can start by modifying variables. Let’s assume that the thread now has a variable named username=jack. We need to change username to “Hello, jack”. Username =jack: Jmeter can set a variable in four ways: 2) User Defined Variables for the Config Element, 3) Config Element’s CSV Data Set Config, 1) Use User Defined Variables in the Test Plan before Processors. Set username=jack.








String username = vars.get("username");
String new_username = "Hello," + username;
vars.put("username", new_username);
Copy the code

4, add a Sampler –> Debug Sampler, named after 5, add a Listener –> View Results Tree



















public class BeanShellMethod {
	public static String test(String input) {
		String output = "Hello," + input;
		returnoutput; }}Copy the code

Referencing source files

Jmeter provides the source function to reference the source file. Write the following code in the BeanShell Sampler:

// Path Writes the absolute path to the beanShellMethod. Java file
source("/Users/Local/jmeter/scripts/BeanShellMethod.java");
String input = vars.get("username");

// In beanShellMethod. Java, we define the test method to be static, so we can call it directly by the class name
String output = BeanShellMethod.test(input);
vars.put("username", output);
Copy the code

Run, and you can see from after that the username variable has been modified.

References to bytecode files

Jmeter provides the addClassPath function to reference the bytecode file. First compile the Java file, the specific command can be baidu search. Then write the following code in the BeanShell Sampler:

addClassPath("/Users/zhoubihui/Local/jmeter/scripts");
import BeanShellMethod;

String input = vars.get("username");
String output = BeanShellMethod.test(input);
vars.put("username", output);
Copy the code

Run, and you can see from after that the username variable has been modified.

Reference the external JAR package

There are two methods for Jmeter to reference the external JAR package: 1) Copy the JAR package to the Jmeter /lib/ext directory. In this case, you need to restart Jmeter to load the JAR package. 2) Test Plan –> Add directory or jar to classpath First edit the Java file, then package the.class file into a JAR package, then copy the JAR package to the corresponding directory, or add it to the Test Plan. Then write the following code in the BeanShell Sampler:

import BeanShellMethod;

String input = vars.get("username");
String output = BeanShellMethod.test(input);
vars.put("username", output);
Copy the code


BeanShell PreProcessor

Let’s say we have a login HTTP interface that encrypts the password in Base64. To test the login interface, we can use the BeanShell preprocessor to stamp the review, first use the script to encrypt the password in Base64, and then use the value of the password variable in the HTTP sampler.








sampler Is an object of the Sampler class, representing the current Sampler


BeanShell PostProcessor

The BeanShell back-end processor can be used to process the response data. For example, if I have an interface that returns data, and I just need to get the ID, then I can process the response in the back-end processor first. Or the data returned by the interface is encrypted, or the response data can be decrypted in the back-end processor first.





byte[] b = "Hello,World".getBytes();
prev.setResponseData(b);
Copy the code





BeanShell Timer

A timer makes each sampler in the scope wait for a fixed amount of time before executing. If there are multiple timers in the scope, they will all execute before the sampler. Therefore, the delay time of the multiple timers is the sum of the delay time of each timer. Stamp the scope and order of execution of this review.




// The unit is milliseconds
Thread.sleep(2000);
Copy the code

From the log file bin/jmeter.log, you can see that the sampler is 2 seconds late.




BeanShell Assertion




Failure Boolean, used to set the state of the assertion. True indicates that the assertion failed
FailureMessage String to set the assertion information
SampleResult SampleResult, specific internal methods,Please have a look at
Response SampleResult, specific internal methods,Please have a look at
ResponseData Byte [] : response data
ResponseCode String, the value of status code, such as 200,404
ResponseMessage String, the response information, such as OK
ResponseHeaders String, the response header
RequestHeaders String, the request header
SampleLabel String, Name of the sampler
SamplerData String, that’s what’s in the screenshot

So, let’s change the response to “Hello, World” just as we did in the BeanShell backend, and write the following code:

byte[] b = "Hello, World".getBytes();
SampleResult.setResponseData(b);
Copy the code






if ("200".equals(ResponseCode)) {
	Failure = true;
	FailureMessage = "In a bad mood";
}
Copy the code

Assertions that fail are shown in red in the viewing result tree:




BeanShell Listener


sampleEvent SampleEvent, specific internal method,Please have a look at

I seldom use BeanShell listener, but I often use View Results Tree and Summary Report. I have not found any good articles online, so I can only write this part here first. So much for using The BeanShell, the next section will cover JSR223 and Groovy.