This article only briefly introduces the download, installation and most basic use of Jmeter.
1. First introduction to Jmeter
Some time ago, during the self-test of the client app, there was a problem that the data returned by requesting a certain interface was empty. The leader asked me to loop through the request for this interface 100 times to see if there was any problem with the result being empty. I heard from my colleagues that there is a professional testing tool of Jmeter, and it happened that I was not very busy, so I started using Jmeter to test this interface.
What is Jmeter?
Apache Jmeter is a Java-based stress testing tool developed by the Apache organization. It is used for stress testing software. It was originally designed for web application testing and was later expanded to other testing areas.
What is Jmeter used for?
- Can be used to test static and dynamic resources, such as static files, Java servlets, Java objects, databases, etc.;
- You can simulate huge loads on servers, networks or objects, test their strength and analyze overall performance under different stress levels;
- Can do functional/regression testing of applications;
Advantages of Jmeter
Fully portable and 100% pure Java, very cross-platform;
2. Jmeter download and installation
Go to the home page of Apache Jmeter official website https://jmeter.apache.org/, click Download Releases on the left
Click the compressed package under Binaries to download, the latest version is 5.4.1
After the download is successful, decompress and use it directly without installation, so decompress the compressed package to the commonly used software folder
Open the terminal and go to the bin directory under the unzipped folder
Select the apache-jmeter-5.4.1 folder, right click - service - create a new terminal window located in the folder location
Enter the command: sh jmeter to open the Jmeter software
Software startup page
Software setting Chinese, Options-Choose Language-Chinese(Simplified)
3. Basic use of Jmeter
When using Jmeter, I am demand-oriented [here, the specified interface is cycled 100 times], with the goal of completing the demand, and learning Jmeter according to the demand.
1. Create a thread group
Basic configuration of thread group
My configuration of the thread group here is 1 thread number, 1 second interval, and 5 loops.
2. Add http request
Under the new thread group, add-sampler-Http request
If an interface is: https://app.test.com/app_login?userName=hahaha&password=7844112b3344112b34f7
1.https请求需要在协议里明确填写https,
2.服务器路径或ip项填写:app.test.com【注意不用填写https://,末尾也没有/】
3.Http请求项根据当前接口的请求方式填写,一般为post或get
4.路径项填写:/app_login?userName=hahaha&password=7844112b3344112b34f7
5.端口号:https方式请求,端口号需要填写443,http方式不用填写
6.请求参数,根据参数字段值进行添加
After the interface parameters are configured, we add a viewing result tree and we can test it.
3. Add viewing result tree
Select the http request, right-click-Add-Listener-View Result Tree
Create a csv file on the computer to save the test results, and then configure the file in the viewing results tree
Then you can test and run this interface, select the thread group, and right-click to start
You will be prompted to check that the file in the result tree already exists. Whether to overwrite the file or append to the existing file can be selected according to your needs.
Click to view the result tree. You will see the requested results in the list on the right. The green check mark indicates that the interface request is successful.
The response data results can be viewed in multiple ways.
View the file data saved in the result tree locally. There are some basic test result data available for viewing.
At this point, it is basically sufficient to stress test a certain interface 100 times. However, it is troublesome to view the results. You need to manually click on each interface one by one to view the results. For example, the interface in my current project returns a status code field ret. Ret=0 means that the interface is normal and data is returned. ret=-2 means that the interface is normal and no data is returned. Manually clicking 100 times to view the results is too inefficient. You can write a script to save a certain field returned by the interface to a local file, and then view the results directly in the file. This will be much more convenient.
Then Jmeter’s regular expression extractor and BeanShell post-processor are used
4. Add regular expression extractor
The regular expression extractor extracts the specified data in the response result
For example, if you want to filter the ret field value { "ret": 0 } returned by the interface, the configuration is as follows
5. Add BeanShell post-processor
The script program is as follows, which saves the data matched by the regular expression to a local file.
The source code is as follows:
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import org.apache.jmeter.samplers.*;
import org.apache.jmeter.config.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
FileWriter fstream = new FileWriter("/Users/xxx/Desktop/666.csv",true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(vars.get("ret")+"\n");
out.close();
fstream.close();
var ret=vars.get("ret");
if(ret!=0){
log.info("ssssssssssssssssssssssssssssssssssssssssssssssssssssss,ret:"+ret);
}
log.info can print logs to the console
6. Run the test
Select the http request and right-click to start
1. First check the log operation results
It was found that for 10 runs, the return value of the ret field was all 0.
2. Check whether the local file saves the ret field value
You can see that the 666.csv file has saved the ret value. We can more easily filter the test results we want by viewing the 666.csv file.
4. Summary
I am familiar with the basic functions of the Jmeter test interface, but it also has many powerful functions. Since I have not used them at present, I have not studied them further. I will continue to learn when I use them in the future.