Thread group , It's about JMeter The first thing that comes to mind , And I think JMeter The most difficult knowledge to understand . Because the project allows you to do a pressure test , The first is to consider concurrency , use JMeter It's multithreading To simulate multiple concurrency . But when you see the thread group edit area , The options are dense , There's no way to start . This article will introduce you to JMeter How to play the thread group .
JMeter Why can we do performance testing
We all know JMeter Using threads to simulate users , But how does it actually simulate real scenes ？ Why can it do performance testing ？
To answer these doubts , Let's start with the simplest logging in pressure test . Suppose the new project is going online , A pressure test is needed , have a look 100 Individuals log into the system at the same time , How about the performance .
Let's not use JMeter, First do it by hand . look for 100 personal , Let these people input their own account and password , Call out , Sign in ！ They just click the login button together .
So it's a performance test ？ not . And the results are not meaningful . Even if we calculate that the response time of the login interface is 0.5 s, So this 0.5 s It can't be 100 Response time for individual simultaneous login . You'll know after you've studied statistics , A single sample data is biased , Take the average value after several times of measurement , To reduce the error .
And then we use JMeter To do it . Set up 100 Threads , simulation 100 Users , continued 5 minute , Let this 100 There are threads logging in all the time , Thousands of concurrent requests are generated . If login is slow or fails , Users are sure to keep on , It's going to create constant requests .JMeter You can also set infinite iterations to simulate this situation .
After the test , And then for all the large sample data , Perform performance analysis , Get the average response time 、TPS、 Throughput and other performance indicators , To evaluate the performance of the system in the current configuration , Find the performance bottleneck , Provide basis for performance optimization .
This is it. JMeter The reason why you can do performance testing , It's also the meaning of performance testing .
I know why , Next, I'll talk about how to do it . If not set properly , There may be 100 Threads can only produce 1 Four concurrent requests .
JMeter The thread group edit area of is as follows ：
name , It's better to have business sense .
notes , Can be null .
Action to be taken after a Sampler error
After the request of a thread in the thread group fails , What to do with . Yes 5 An option ,Continue,Start Next Thread Loop,Stop Thread,Stop Test,Stop Test Now.
for example , Suppose there is 1 Threads , Include 2 A sampler , Iterative execution 2 Time ：
After the request went wrong , The thread continues to run .
Why keep running ？ When we have a large number of concurrent users , It is normal for the server to occasionally respond to errors , For example, the server has performance problems 500, We're just going to make a note of it , As a basis for performance problems . But you can still continue to request to try again , Maybe you can access the server again , So you can figure out Error rate .
such as , Login failed , Then the operation of placing an order due to login failure , Will also follow the failure .
This option is selected by default , Ensure sufficient concurrency pressure .
Start Next Thread Loop
If something goes wrong , The remaining requests in the same thread will no longer be executed , Start a new iteration directly .
such as , Login failed , Then the order will no longer be executed , restart .
If you want to reduce association request errors , You can choose this option .
This item is not normally set , It refers to when a request fails , Stop the current thread , No more execution . This leads to fewer and fewer threads to run , Finally, the load is not enough , Not enough pressure on the server , The test results are not referential .
If a request from a thread fails , Stop all thread tests .
But each thread will still stop after the current iteration . amount to Continue By the end of the current iteration .
Like threads 1 Just execute to login , There are other threads in error , Threads 1 Will also execute the order operation will stop .
Stop Test Now
If a thread's request fails , Stop all threads immediately , No more execution .
Number of Threads (users)
Set the number of threads to run , A thread corresponds to a simulated user .
Ramp-up period (seconds)
How long does it take for all threads to start running , The unit is seconds .
For example, we set the number of threads to 50, Set here 10 second , So every second it starts 50 / 10,5 Threads . If set to 0 second , be 50 Threads will start immediately . If set to 100 second , Every once in a while 100 / 50, 2 second start-up 1 Threads .
Ramp-up period The size of
Ramp-up period The size of , It's the easiest thing for beginners to worry about .
Here are 5 A diagram of threads from start to exit ：
The red framed part is really 5 Time period of concurrent requests from threads .
Suppose we set 20 Threads , Just run 1 Sub iteration , See how different startup time settings can lead to different results .
If the start time is set to 0, So the test starts with 20 Four concurrent requests , In case the server can only bear 15 concurrent , Isn't it that as soon as you come up gg 了 , What else is it .
If the start time is set to 40, Then every time 2 second start-up 1 Threads . In case the thread cannot execute 1 Seconds out of , The first 2 Threads When it starts , The first 1 Threads have exited , No, it just produced 1 Concurrent requests .
How many settings are appropriate ？ I don't know either , But combined with the information I looked up , Can give a reference opinion .
First step , Run the thread group 1 Time （ You can right-click on the thread group component and select Validate）, Get from aggregate reports to throughput （Throughput）.
The second step , Divide the number of threads by the throughput , Get the start-up time .
for example ,200 Threads , Run once to get throughput of 4/sec, The start time is 200 / 4 = 50. After this setting , The first 2 After threads start , Just in time 1 Threads finish executing and start a new iteration , Thus, a gradient incremental concurrent request is formed .
The number of iterations . You can fill in numbers to specify the number of iterations . You can also check Infinite, Represents infinite iteration , Run until the test stops or crashes .
Same user on each iteration
stay JMeter in ,user The thread , This option means that each iteration uses the same thread .
This has to start with the old version , In the past 3.x and 4.x Version of JMeter in , There is no such option . Create good 1 After a thread , Every iteration uses this thread , Until the end of the test . Its effect is , For example, login. , added HTTP Cookie After the manager , A single thread iterates many times （ Note that it's not multiple threads ） Login with the same Cookie.
5.x The version adds this option , You can control whether new threads are created in each iteration . At the same time HTTP Cookie The manager also added an option , Control whether to clean up the old Cookie：
Default this Same user on each iteration The option is checked . Because destroying and creating threads themselves will take up resources , May affect performance test results .
Delay Thread creation until needed
Follow JVM The timing of thread creation is related to , The actual use of the tick does not affect the test results , Just keep the default .
Specify Thread lifetime
The duration of the , Unit second .Loop Count Checked Infinite, It works .
Start delay , Unit second . Delay to time before running the thread .
This paper first analyzes JMeter Why we can do performance testing , Then the selection of thread group editing area is explained , The key point is to sort out Ramp-up period The size of . Besides ,JMeter Plugins Two thread group components are also provided Ultimate Thread Group And Stepping Thread Group, To meet the surge （ Wavy , Multiple peaks ） Scene . Limited author , If there is an error , Please correct me. .
Reference material ：1.《 The whole stack performance test training manual JMeter actual combat 》