Jmeter multi-user token usage problem

  • Focus on sharing useful software testing content, welcome to like Collect ?Leave a message Please correct me if there are any errors!
  • Communication and discussion: Join 1,000 people in the software testing technology learning and exchange group
  • Resource sharing: After joining ByteDance, I realized how important this information given by my senior brother is!
  • The most difficult time is when we are not far from success!

?

Background

During testing, there are often scenarios where a simulated user logs in, obtains the user token, and then requests the interface.

This simulated user login will be divided into two types, one is single user and the other is multi-user.

In daily automated testing, one user corresponding to n use cases may be enough to satisfy most scenarios;

If it is used in a stress test scenario, it may be a bit monotonous and cannot meet some real business scenarios.

For a single user, there is actually not much difference from our regular multi-interface dependent tests.

So what we mainly talk about here is the situation where multiple users generate multiple tokens.

Let’s look at a specific example to understand.

Scene interface

Here, there are only two interfaces, one is for logging in to get a token, and the other is for requesting only when you have a token.

The following is the definition of each interface

Login interface

ask:

POST http://localhost:8532/MultiToken/Login
Content-Type: application/json

{
"UserName":"catcherwong-1",
"Password":"123"
}

response:

{"code":0,"msg":"ok","data":"catcherwong-1-token"}

Business interface

ask:

GET http://localhost:8532//MultiToken/do?account=xxx
Content-Type: application/json
token: catcherwong-1-token

response:

{"code":0,"msg":"ok","data":"catcherwong-1-token"}

Login interface processing

The login interface is a pre-request, so we usually choose to put it in the setUp thread group.

We need to prepare a csv file to store the username and password that need to be logged in.

The next step is to configure this csv and define two variables account and pwd

Then Configure the login HTTP request:

Since the token will be used later, the token must be extracted first. The JSON Extractor is used here.

This is where you start paying attention! ! ! !

Since we will have multiple users logging in, this extraction operation will assign the token to the access_token variable every time, which is an overwriting operation.

In other words, every time a user logs in, the value of this access_token will be the token of the last logged in user.

Another way of thinking, every time it will be overwritten, then just store these tokens in one place, and then the business interface can retrieve them from this place.

If there is no user login step, and the token is given directly, then we will directly put the token into the csv file, and then let jmeter recycle the token inside.

Then what we need to do is actually very certain, which is to write the token into a csv file after extracting the token.

To achieve this step, you need to add a post-processing behind the login interface.

String p1 = System.getProperty("user.dir");
String p2 = System.getProperty("file.separator");
String p3 = "user_token.csv";
String path = p1 + p2 + p3;

FileWriter fileWriter = new FileWriter(new File(path), true);
BufferedWriter writer = new BufferedWriter(fileWriter);
writer.append(vars.get("acout") + "," + vars.get("access_token") + "\\
");
writer.close();
fileWriter.close();

The meaning of this code is to write the user name and the extracted access_token into a csv file. The location of this file is the directory of jmeter.

The file path is processed here and can be adapted to all operating systems. It will not appear that if you specify a path to the windows system, and then put it under the linux system, it will not run.

The most important thing is to modify the properties of the setUp thread group and change the number of cycles to 3. Because there are 3 users in the previous csv file, it will trigger three logins.

Business interface processing

The business interface should be placed in a normal thread group, independent of the setUp thread group.

As mentioned earlier, there will be a csv file after logging in, so the first thing to do here is to configure the csv.

The screenshot above uses the file path ${__P(user.dir,)}${__P(file.separator,)}user_token.csv, which can be passed through Jmeter in the local environment. , but it does not work on some cloud services, such as Alibaba Cloud PTS.

You can ignore the previous path here and just fill in user_token.csv directly. Fill in these two and the resulting file paths will be the same, one is an absolute path and the other is a relative path.

Then it’s time to configure the HTTP request

PS: Don’t forget to configure the request header, so I won’t take a screenshot here.

After two test runs here, you can find that the business request interface, its token request header is different every time, and changes alternately, which is in line with expectations.

However, you will find that the data in the generated csv file will be repeated, and the data generated last time will not be automatically cleared. If the token generated last time has expired, then use these expired tokens === cool.

Therefore, it is necessary to add a tearDown thread group here to delete this file every time the script is run.

String p1 = System.getProperty("user.dir");
String p2 = System.getProperty("file.separator");
String p3 = "user_token.csv";
String path = p1 + p2 + p3;

log.info("Preparing to delete file:" + path);

File file = new File(path);
if (!file.exists()) {
  log.info("Failed to delete file:" + path + "Does not exist!");
} else {
  file.delete();
}

At this time, there is basically no problem running the script.

If you want to exchange experiences on software testing, interfaces, automation, performance testing, test development, and interviews. If you are interested, you can add skirt 485187702. The group will distribute free information links from time to time. These information are collected and compiled from various technical websites. If you have good learning materials, you can privately Send me a message and I will indicate the source and share it with you.

Finally: The following are supporting learning materials. For those who are doing [software testing], it should be the most comprehensive and complete preparation warehouse. This warehouse has also accompanied me through the most difficult journey. I hope it can also help you. ! [100% free without any tricks]

?

?

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill treeHomepageOverview 389213 people are learning the system