jmeter write test script Daquan

Table of contents

1. Background

2. Divide by function

2.1 Encryption processing and signature verification processing

2.2 jmeter uses beanshell to write scripts

2.3 jmeter script error report

2.4 jmeter print log

2.5 jmeter pressure test java code

2.6 jmeter reads data to ensure that the data obtained by each thread is unique

2.7 jmeter sets the number of concurrent threads as a variable

2.8 beanshell writes data to local txt

2.9 beanshell read local txt

3. According to the scene division

3.1 Login Scenario

3.1.1 Background

3.1.2 Overall script structure

3.1.3 Parameterization

3.1.4 HTTP header manager 

3.1.5 Request /xxx/xxxx/getSession to get token and key

3.1.6 Request /xxx/xxx/getVerificationCode?token=${token} to get the verification code

3.1.7 /xxx/xxx/login 

3.2 Query product details

3.2.1 Background

3.2.2 Overall structure

3.2.3 Header Management 

3.2.4 Parametric processing

3.3 Obtain batch tokens and write them to local files

3.3.1 Background

3.3.2 Script structure

3.3.3 User-defined variables

3.3.4 Loop Controller

3.3.5 Login Request

3.4 Query personal information of different users

3.4.1 Background

3.4.2 Overall structure

3.4.3 Loop Controller

3.4.4 Header Management


1. Background

When using jmeter to write scripts, there are many precautions, and many blogs have been written, but they are scattered.

Let's unify this time.

2. Divide by function

2.1 Encryption processing and signature verification processing

jmeter script handles encrypted signature verification

2.2 jmeter uses beanshell to write scripts

 jmeter uses beanshell to write scripts

2.3 jmeter script error report

jmeter write script error report Daquan_jmeter recording error report_Do test meow sauce's blog-CSDN blog

2.4 jmeter print log

jmeter script debugging

2.5 jmeter pressure test java code

jmeter pressure test java code_jmeter pressure test java code_do test meow sauce's blog-CSDN blog

2.6 jmeter reads data to ensure that the data obtained by each thread is unique

Jmeter reads the data to ensure that the data obtained by each thread is unique - Programmer Sought

2.7 jmeter sets the number of concurrent threads as a variable

jmeter sets the number of concurrent threads as a variable - Programmer Sought

2.8 beanshell writes data to local txt

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;

FileWriter fileWriter = new FileWriter(new File(vars.get("data_file_path_token")), true);
BufferedWriter writer = new BufferedWriter(fileWriter);


String TOKEN = vars.get("token");
writer.append(TOKEN+"\r\n");
writer.close();
fileWriter.close();

data_file_path_token is the path of the local file 

2.9 beanshell read local txt

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;        
String data_file_path = "/xxx/user.txt";
   
      
        FileReader filereader = new FileReader(new File(data_file_path));
        BufferedReader reader = new BufferedReader(filereader);




        String line = reader.readLine();
        reader.close();
        filereader.close();

3. According to the scene division

3.1 Login Scenario

3.1.1 Background

The entire login scenario is composed of three interfaces.

1. Obtain token and key

/xxx/xxxx/getSession

2. Get the picture verification code

You need to hold the token in 1 to get the picture verification code.

/xxx/xxx/getVerificationCode?token=${token}

3. Put the token obtained in 1 into the header.

Use the key obtained in 1 to encrypt the password

In this request header, a signature verification also needs to be generated.

/xxx/xxx/login

3.1.2 Overall script structure

3.1.3 Parameterization

Parameterized processing: loop controller + CSV data file settings

Jmeter reads the data to ensure that the data obtained by each thread is unique - Programmer Sought

3.1.4 HTTP header manager 

Many of the information headers are variables. token, key, and signature verification sign are all variables.

3.1.5 Request /xxx/xxxx/getSession to get token and key

Go directly to /xxx/xxxx/getSession, and then extract the token and key from the return value

 1. http request

2. Extract token

3. Extract key

3.1.6 Request /xxx/xxx/getVerificationCode?token=${token} to get the verification code

When the development will log in, the verification code is hard-coded with a fixed value.

getVerificationCode?token=${token} request, need to bring the token extracted from the previous request

1. http request

3.1.7 /xxx/xxx/login 

Log in /xxx/xxx/login 

1. The parameters in the login request need to be encrypted.

So set the parameter as the message body. The message body is a variable ${param} 

2. Preprocessing to process header and pass parameters

The information required by the header and the information required for passing parameters. All in preprocessing, processing. Finally, spit out the token, sign, and param we need.

jmeter script handles encrypted signature verification

3.2 Query product details

3.2.1 Background

Query the details of the product to avoid caching problems. Use parameterization to query product details according to different product ids

/xxx/xxx/commodity?id=${id}

3.2.2 Overall structure

3.2.3 Header Management 

We use the same user information to query the details of different products. So it’s good to write the information header to death here.

3.2.4 Parametric processing

Jmeter reads the data to ensure that the data obtained by each thread is unique - Programmer Sought

3.3 Obtain batch tokens and write them to local files

3.3.1 Background


I need to get the tokens of 20,000 users after they log in, and write them into a local txt file.

3.3.2 Script structure

3.3.3 User-defined variables

This defines the path we write to the txt document

3.3.4 Loop Controller

To get 20,000 tokens, 20,000 different users need to log in. Here the loop controller is used to read 20000 different user information.

Jmeter reads the data to ensure that the data obtained by each thread is unique - Programmer Sought

3.3.5 Login Request

 

 1. Information header manager

Note that the location of this information header manager must be in the login request.

 

2. Preprocessor

Handle login encryption, signature verification, etc. Do not check the script compilation cache.

3. Extract token

4. The post-processor writes the obtained token into a local file 

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;

FileWriter fileWriter = new FileWriter(new File(vars.get("data_file_path_token")), true);
BufferedWriter writer = new BufferedWriter(fileWriter);


String TOKEN = vars.get("token");
writer.append(TOKEN+"\r\n");
writer.close();
fileWriter.close();

Notice:

To avoid caching effects, select True at the end.

3.4 Query personal information of different users

3.4.1 Background

You need to use tokens of 2000 users, and then query personal information.

The normal logic is to log in first, and then obtain personal information after successful login.

But in this case, the tps of the personal information interface will be affected by the performance of the login interface.

The login can only support 30 concurrency, but the query personal information interface needs to test 100 concurrency.

Therefore, we can only write the information after successful login to the local, and then read the token of the local login to request the personal information interface.

3.4.2 Overall structure

3.4.3 Loop Controller

Read 2000 tokens, and ensure that the tokens obtained by each thread are unique.

3.4.4 Header Management

The information header manager must be inside the request.

Guess you like

Origin blog.csdn.net/qq_39208536/article/details/130059136