Jmeter:Delay Thread creation until needed

在用jmeter进行压测的时候,线程组里的Thread Properties参数基本是压测的核心设置参数,如下图

NewImage

Number of Threads:线程数,自行设置

Ramp-Up Period:所有设置的线程开始运行的时间范围,比如10个线程,此处配置2,那么就是2秒内开始运行10个线程

Loop Count:循环次数,也就是执行该请求的次数,如果勾forever,就会一直请求,每2秒起10个线程运行

Delay Thread creation until needed:这个勾选项才是想说的

先看下线程的几个状态(整理自互联网):

NEW:创建未启动,已经实例化,只是没有开始运行线程的Run方法

RUNNABLE:就绪状态,线程对象创建后,其他线程调用了对象的start()方法,该状态的线程位于可运行线程池中,已经准备好了只等获取CPU的使用权,然后开始运行

RUNNING:运行状态,就绪状态的线程获取了CPU使用权执行程序代码

BLOCKED:阻塞状态,线程由于某些原因放弃CPU使用权,暂时停止运行,比如IO等待导致的线程处于BLOCKED状态;直到线程进入就绪状态,才有机会变成运行状态

(1)等待阻塞:运行的线程执行wait()方法,线程进入等待池中

(2)同步阻塞:运行的线程再获取对象的同步锁时,若该同步锁被别的线程占用,也就是资源争用失败,该线程放入锁池中

(3)其他阻塞:运行的线程执行sleep()或者join()方法,或者发出了I/O请求时,该线程置为阻塞状态,当sleep()状态超时,join()等待线程终止或者超时,或者I/O处理完毕时,线程重新转入就绪状态

DEAD:死亡状态,执行完毕或者异常退出,线程生命周期就结束了

 

再看看下Delay Thread creation until needed是否勾选的区别:

勾选:设置的Number of Threads数量线程将在Ramp-Up Period时间内启动并且运行,比如图中2秒内10个线程,那么每隔1秒就启动5个线程,并运行(RUNNING状态)我们请求的Sampler

不勾选:启动了所有线程(NEW状态),但是不立即运行Sampler,而是按照Ramp-Up Period时间来运行,比如图中一开始10个线程就全部就绪了,但是每隔1秒只有5个线程来运行请求的Sampler

这么看来其实并不影响测试结果

比如勾选

2019-07-19 20:52:07,381 INFO o.a.j.e.StandardJMeterEngine: Running the test!
2019-07-19 20:52:07,382 INFO o.a.j.s.SampleEvent: List of sample_variables: []
2019-07-19 20:52:07,382 INFO o.a.j.p.j.t.JavaTest: Created class: com.lihuia.App. Uses tearDownTest: true
2019-07-19 20:52:07,382 INFO o.a.j.g.u.JMeterMenuBar: setRunning(true, *local*)
2019-07-19 20:52:07,668 INFO o.a.j.e.StandardJMeterEngine: Starting ThreadGroup: 1 : 七夕玩耍
2019-07-19 20:52:07,668 INFO o.a.j.e.StandardJMeterEngine: Starting 10 threads for group 七夕玩耍.
2019-07-19 20:52:07,668 INFO o.a.j.e.StandardJMeterEngine: Thread will continue on error
2019-07-19 20:52:07,668 INFO o.a.j.t.ThreadGroup: Starting thread group... number=1 threads=10 ramp-up=5 perThread=500.0 delayedStart=true
2019-07-19 20:52:07,668 INFO o.a.j.t.ThreadGroup: Started thread group number 1
2019-07-19 20:52:07,668 INFO o.a.j.e.StandardJMeterEngine: All thread groups have been started

不勾选

2019-07-19 20:53:01,393 INFO o.a.j.e.StandardJMeterEngine: Running the test!
2019-07-19 20:53:01,393 INFO o.a.j.s.SampleEvent: List of sample_variables: []
2019-07-19 20:53:01,393 INFO o.a.j.p.j.t.JavaTest: Created class: com.lihuia.App. Uses tearDownTest: true
2019-07-19 20:53:01,393 INFO o.a.j.g.u.JMeterMenuBar: setRunning(true, *local*)
2019-07-19 20:53:01,620 INFO o.a.j.e.StandardJMeterEngine: Starting ThreadGroup: 1 : 七夕玩耍
2019-07-19 20:53:01,620 INFO o.a.j.e.StandardJMeterEngine: Starting 10 threads for group 七夕玩耍.
2019-07-19 20:53:01,621 INFO o.a.j.e.StandardJMeterEngine: Thread will continue on error
2019-07-19 20:53:01,621 INFO o.a.j.t.ThreadGroup: Starting thread group... number=1 threads=10 ramp-up=5 perThread=500.0 delayedStart=false
2019-07-19 20:53:01,621 INFO o.a.j.t.JMeterThread: Thread started: 七夕玩耍 1-1
2019-07-19 20:53:01,621 INFO o.a.j.p.j.t.JavaTest: 参数a:1 参数b:2
2019-07-19 20:53:01,621 INFO o.a.j.p.j.t.JavaTest: 结果是:3
2019-07-19 20:53:01,621 INFO o.a.j.t.JMeterThread: Thread is done: 七夕玩耍 1-1
2019-07-19 20:53:01,621 INFO o.a.j.t.JMeterThread: Thread finished: 七夕玩耍 1-1
2019-07-19 20:53:01,622 INFO o.a.j.t.ThreadGroup: Started thread group number 1
2019-07-19 20:53:01,622 INFO o.a.j.e.StandardJMeterEngine: All thread groups have been started

除了delayedStart字段的区别,也没有其他的备注,一下子也无法通过简单的方法得到差别

总之,这个字段的意义在于,如果不勾选,那么在测试开始前就会分配所有的线程,假如测试当中有很大量的线程,也许CPU会达到100%,服务可能都会被杀掉;假如勾选了,那么线程的创建会被延迟,美其名曰按需的时候再创建,取决于配置的参数

可参考下面这段话

JMeter allocates all threads at the test start unless the Thread Group option "Delay Thread creation until needed" is selected.

In some cases where a test has a very large number of threads the CPU can spike to 100% and kill the server process if the delay thread creation option is not selected.

When selected, JMeter will allocate threads according to the ramp up times. Otherwise all threads are allocated to the JVM process the moment the test is started, regardless of ramp up time.

This has no relation to whether or not the threads are allowed to begin sending samples. That always depends on ramp-up time and and test plan timers.

来自:https://stackoverflow.com/questions/29409501/jmeter-delay-thread-creation-until-needed-and-constant-timer

发表回复