Technology Sharing

Tool application: Use JMeter to implement Phpwind performance testing!

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Experiment Introduction

In the previous study, we have used JMeter to complete the protocol-level interface test for the Agileone system. We also know that the core of performance testing technology is to use multi-threading technology to send protocol requests and complete the simulation of a large number of users accessing the system. The original intention of JMeter was actually for performance testing. For example, the first step in creating a test plan is to create a thread group. This experiment will explain how to use JMeter to complete the performance test of Phpwind login and posting.

Purpose

(1) Master the usage of association search in JMeter.

(2) Master the thread design and usage in JMeter.

(3) Master the usage of test reports in JMeter.

experiment process

  1. Implementing Phpwind's Login Sampler

We have created 100 users from testuser_1 to testuser_100 for Phpwind. During the performance test, we should implement random login of users to better simulate the real scenario.

(1) Create an "HTTP Request" sampler and name it "DoLogin", and fill in the corresponding login POST request parameters.

(2) Create a "Response Assertion" and "View Result Tree" for the sampler, and the same for subsequent requests.

(3) Create a "preprocessor" for the sampler and implement a random number from 1 to 100.

(4) The final test request is as follows:

2. Use regular expressions to associate verification codes

Since a field "verify" must be specified when posting in Phpwind, and the value is a random number, we must use a regular expression to associate the corresponding value in the response. This part has been mastered in the implementation process of the interface test. Here we mainly look at how to implement it in JMeter:

(1) Add an "HTTP Request" sampler and send a GET request to "/phpwind/post.php?fid=2" to get a response.

(2) Add a postprocessor to the sampler, set it to "Regular Expression Extractor", and set it as follows:

(3) A brief description of the above input fields:

a) Reference name: The parameter name to be referenced in the next request. For example, if verifycode is entered, it can be referenced using ${verifycode}.

b) Regular expression: The content to be extracted is contained in the parentheses, which is also what we are familiar with.

c) Template: quoted with $-$. If the regular expression we set finds multiple values, the sequence number indicates which value is processed.

d) Matching number: 0 represents a random value, 1 represents all values. Usually, just fill in 0.

e) Default value: If the parameter does not get a value, a default value is given to it.

3. Add post request

(1) Set a user parameter for the sampler and use the function helper to generate a random number to be used as the random number for the post title and content.

(2) Use the verifycode obtained in the previous step as the value of the verify field in the POST request body.

(3) The final test request generated is as follows:

4. Ensure that all resources are downloaded for each request

By default, JMeter is just like the interface test, it is only responsible for downloading the current requested page, but not downloading other additional resources of the page, which is obviously not in line with the actual situation. So we need to download all the resources of the page, in the "Advanced" tab of the HTTP sampler, check "Get all included resources from HTML files".

5. Design concurrent scenarios

This time we still follow the same scenario design as the previous Java script: 50 users, 5 concurrent users every 10 seconds, and each user runs 100 times. The specific settings are as follows:

Setting the parameter "Ramp-Up Period (in seconds)" in the above settings to 100 seconds means adding a thread every two seconds. Although the frequency cannot be arbitrarily defined like a custom thread, this setting is essentially the same as having 5 concurrent users every 10 seconds.

In addition to setting the number of runs, we can also set the total run time of the performance test process. In the lower part of the above picture, check "Scheduler" and set the duration. If we need to run continuously for one hour, we only need to set the value to 3600 seconds.

6. Allow time for thinking at each step

Thinking time is a necessary setting to simulate real user scenarios. The "Timer" component provided by JMeter is used to simulate the user's thinking time. JMeter comes with many types of timers. We use the more commonly used "Gaussian Random Timer". We can follow the following steps to set the thinking time for a sampler in JMeter:

(1) Right-click a sampler, for example, click "DoLogin", and create a new "Gaussian Random Timer".

(2) Set the deviation to 2000 milliseconds and the fixed delay offset to 4000 milliseconds. This generates a random number within the range of 4 seconds plus or minus 2 seconds, that is, the random pause time is between 2 seconds and 6 seconds.

More about JMeter Timer usage

7. Set up a meeting point

The concept of rendezvous point was first proposed by the performance testing tool LoadRunner. Its function is to gather together after a group of threads send a request until all threads are synchronized to a time point and then send the request together. It is used to simulate more stringent concurrency testing. Although the use of rendezvous points does not conform to the real scene, it can put greater instantaneous pressure on the server and is mainly used for concurrent testing of the server.

In JMeter, we can use the "Synchronizing Timer" timer to complete the collection point processing. For example, the following figure shows how to implement a concurrent testing strategy for the sampler "DoPost":

(1) Number of Simulated Users to Group by: This value corresponds to the number of threads at the collection point. Here we set it to 50, which means that a collection needs to wait until 50 threads are gathered before sending the next request together. If it is set to 0, it means that all threads set by the thread group are used.

(2) Timeout in milliseconds: This is set to 10000, which means that the timeout of the collection point is 10 seconds. That is, if all threads have not waited for 10 seconds, they will not wait any longer. The threads that have completed the collection will directly send the next request.

In fact, in Java's native thread development, we can also use the two synchronization methods "wait()" and "notifyAll()" to complete the simulation of the collection point.

8. View performance test results

JMeter also has many built-in components for viewing test reports, but the most commonly used one is the "aggregate report" created for a thread group. For example, the results of the current Phpwind performance test run are shown in the following figure:

From the above figure, we can see the number of executions of each sampler, various mathematical statistics of response time (average, median, 90% value, minimum, maximum), transaction error rate, network bandwidth throughput, total response size, total request size, etc. In addition to monitoring the performance indicators on the server side, these indicators are basically enough for us to analyze the basic data of a performance test.

Thinking Exercise

(1) Be familiar with the usage of other components in JMeter.

(2) Use JMeter to complete the performance test of a project and solve the problems encountered one by one.

(3) Compare the performance tests in JMeter with the performance tests we developed ourselves using native Java to see the similarities and differences.

Finally, I would like to thank everyone who reads my article carefully. Seeing the number of fans and attention increasing all the way, I think it’s always good to reciprocate. Although it’s not something very valuable, you can take it directly if you need it! 

Software Testing Interview Document

We study in order to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet giants such as Alibaba, Tencent, and ByteDance, and ByteDance bosses have given authoritative answers. After finishing this set of interview materials, I believe everyone can find a satisfactory job.

insert image description here