How to use Jmeter to stress test the HTTP interface?

We should not be limited to a certain tool. There are many tools that can be used for performance testing, and choosing the one that suits you is the best. The author has used Loadrunner for many years of practical experience in project performance testing, and it is considered a small success. Any performance test (such as stress test, load test, fatigue strength test, etc.) can use this tool. But I don't encourage this. We should use the most appropriate tool based on the current situation, based on the object to be tested, time and cost considerations. Gossip less, what I want to share with you today is to use Jmeter to carry out the stress test of the HTTP interface. The actual interface test can also use tools such as Tsung and SoapUI, but based on various considerations, Jmeter is finally used.

Compared with Loadrunner, Jmeter is lighter and easier to install. If there is not much collection of process data and the test scenario is not complicated, it can be given priority. The version of the tool used by the author is Apache JMeter3.0. If you are using other versions, the interface of this version may be different.

Let's talk about the specific steps of using Jmeter for HTTP interface stress testing:

1. First add a thread group (that is, user group: one thread simulates a user behavior, if you want to simulate multiple users, you can achieve it by setting multi-threading)

2. Because it is a stress test for the HTTP interface, it is necessary to add an HTTP request under the thread group (by right mouse button->add->Sampler->HTTP request)  

3. The next step is to set the HTTP request, mainly setting the server name or IP, port number, connection, response time, protocol used, request type, content encoding, path, and finally set the request content.

Note that the path is the content of the URL except http://XXX.XX.X. If the URL is http://www.51testing.com/?action-login, the server name or IP should be filled with http://www .51testing.com, the default port is 80, the path should be filled with "/?action-login"

 4. Because the request content here uses a json string, it needs to be declared in the header, otherwise the request will report an error. So you need to add an information header to the HTTP request (by right mouse button -> Add -> Configuration Component -> HTTP Information Header Manager)

 

5. Configure the HTTP header information, add the corresponding name and value, here add "Content-Type", the value is "application/json;charset=utf-8"; if the header needs to add other information, such as id, token, etc., also should be configured like this

6. At this point, it seems to be ok, but we can't see the execution result when we execute it. So you should also add a result listener "View Result Tree" for this request (by right mouse button -> Add -> Listener -> View Result Tree) 

According to the above steps, you can add "aggregate report" (statistics of overall thread execution results), graphical results (statistical graph of overall thread execution results), "view results in table" (table statistics of each thread execution), etc. in turn.

PS (If an error is found during non-interface testing, such as page testing, it may be that the cookie information is missing, so you can add a Cookie header to the thread group, which can be done through the right mouse button -> configuration element -> HTTP Cookie Manager)

7. If there is no problem with the HTTP request, then if we want to perform a performance test, we need to set an assertion (that is, a checkpoint in Loadrunner), that is, as long as the specified return value is checked (such as the return result contains the character "success"), it means execution succeed. Adding an assertion is done by right-clicking the request->add->assertion->response assertion.

 OK, at this point, our script work is nearly complete, but we are doing performance testing. In order to make the test results more accurate and avoid single-point restrictions, we should also parameterize the requested value.

8. Parameterization, here mainly parameterize through CSV files.

1) First, we create a new CSV file, and then enter the information we want to parameterize.

We can also create a new notepad, and then enter the parameterized content information. Multiple parameter values ​​​​are separated by English commas, press enter and line feed, such as:

Username1,password1

Username2,password2

Click Save, and then change the file name of Notepad to test.csv, note that csv here is the file extension.

2) Create a new CSV Data Set Config element in the HTTP request (by right mouse button -> Add -> Configuration Element -> CSV Data Set Config)

3) Configure the CSV Data Set Config component. Here you mainly need to specify the path of the parameterized value file (that is, the CSV file we created earlier), and the parameter name (for example, there are two parameter values ​​in the CSV file above. We can use username and passwd as the parameter name, the parameter name is similar to the variable name defined in the program)

 

Finally, I would like to thank everyone who has read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, you can take it away if you need it:

These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of  thousands of test engineers through the most difficult journey, and I hope it can help you! Partners can click the small card below to receive

Guess you like

Origin blog.csdn.net/kk_lzvvkpj/article/details/130582974