一、纵横并发
Jmeter设计并发事件,这应该是一项必备技能。
首先来看并发的概念。
通常在性能测试中会涉及到并发用户数的概念,有关并发用户数(系统用户数)的详解后续再记。
(有关并发、并行的概念参考https://blog.csdn.net/qq_33290787/article/details/51790605)
并发:本质为一个CPU(或多个CPU)在若干道程序(或线程)之间的多路复用。
参考知乎大神的讲法:并发指的是该系统有处理多事务的能力,不一定是同时。
啃了啃虫师的一篇博客http://www.cnblogs.com/fnng/archive/2012/06/25/2562943.html--并发用户。
文中对并发用户做了很详细的解释。其中更是对“真正意义上的并发不存在”一理论进行了很详细的阐述。推荐认真一阅。
那么在Jmeter中是如何进行模拟高仿并发的呢?
有人说了,用集合点,对,集合点确实达到并发目的的一个点。
还有人说了,不用集合点也可以,其实,对后者的观点,本人认为也是正确的,只是在某种意义上,设计该种场景的话要考虑的因素比较多。
废话不多说,先一一来上操作。
线程组加入线程数2,斜坡时间1s,循环一次。
1s启动2个线程。(2个线程并发)
这是一种原始的Jmeter的设计方法。
那么再看一下集合点的设计方法。
定时器-->Synchronizing Timer原件。
看到Synchronizing这个单词是不是有点熟悉。译为同步化。
java中使用synchronized为锁的关键字。那么再看看其帮助文档。
The purpose of the SyncTimer is to block threads until X number of threads have been blocked, and then they are all released at once. A SyncTimer can thus create large instant loads at various points of the test plan.
Control Panel
Parameters
Attribute | Description | Required |
Name |
Descriptive name for this timer that is shown in the tree. |
No |
Number of Simultaneous Users to Group by |
Number of threads to release at once. Setting it to 0 is equivalent to setting it to Number of threads in Thread Group. |
Yes |
Timeout in milliseconds |
If set to 0, Timer will wait for the number of threads to reach the value in "Number of Simultaneous Users to Group". If superior to 0, then timer will wait at max "Timeout in milliseconds" for the number of Threads. If after the timeout interval the number of users waiting is not reached, timer will stop waiting. Defaults to 0 |
No |
If timeout in milliseconds is set to 0 and number of threads never reaches "Number of Simultaneous Users to Group by" then Test will pause infinitely. Only a forced stop will stop it. Setting Timeout in milliseconds is an option to consider in this case. |
Synchronizing timer blocks only within one JVM, so if using Distributed testing ensure you never set "Number of Simultaneous Users to Group by" to a value superior to the number of users of its containing Thread group considering 1 injector only. |
|
|
|
The purpose of SycTimeType is to stop threads until X threads are blocked, and they are immediately released. Therefore, synchronous timer can create large immediate load at every point of the test plan.
SycTimeType的目的是阻止线程,直到X个线程被阻塞,然后它们都被立即释放。因此,同步计时器可以在测试计划的各个点创建大的即时负载。(百度翻译)
其目的为阻塞线程,什么意思呢?就好似过桥一样,先在桥头将人挡住,直到人数阻塞到一定数量,释放障碍物,让人一起从桥上通过(速度恒定)
该元件只有两个指标:Number of Simultaneous Users to Group by:每次释放的线程数
Timeout in milliseconds:超时时间,超时时间后达不到设置的线程数时,会丢弃继续请求
逐一翻译一下下方的两段注意语:
#If timeout in milliseconds is set to 0 and number of threads never reaches "Number of Simultaneous Users to Group by" then Test will pause infinitely. Only a forced stop will stop it. Setting Timeout in milliseconds is an option to consider in this case.
如果以毫秒为单位的超时设置为0,并且线程的数量永远达不到“同时分组的用户数”,那么测试将无限地暂停。只有强制停止才能阻止它。在这种情况下,以毫秒为单位设置超时是一个考虑的选项。
#Synchronizing timer blocks only within one JVM, so if using Distributed testing ensure you never set "Number of Simultaneous Users to Group by" to a value superior to the number of users of its containing Thread group considering 1 injector only.
仅在一个JVM内同步计时器块,因此如果使用分布式测试确保从未将“同时用户数”分组为一个值,其值仅考虑其包含1个喷射器的线程组的用户数。
看看效果:
设置Synchronizing Timer
sampler都是以50、50的进行请求。
二、限制QPS
内容来自<软件性能测试案例剖析-第二版-段念>
书中提到了限制QPS的步骤。
用到组件:定时器-->Constant Throughput Timer(恒定吞吐量定时器)
This timer introduces variable pauses, calculated to keep the total throughput (in terms of samples per minute) as close as possible to a give figure. Of course the throughput will be lower if the server is not capable of handling it, or if other timers or time-consuming test elements prevent it.
N.B. although the Timer is called the Constant Throughput timer, the throughput value does not need to be constant. It can be defined in terms of a variable or function call, and the value can be changed during a test. The value can be changed in various ways:
- using a counter variable
- using a JavaScript or BeanShell function to provide a changing value
- using the remote BeanShell server to change a JMeter property
See Best Practices for further details.
Note that the throughput value should not be changed too often during a test - it will take a while for the new value to take effect. |
Control Panel
Parameters
Attribute | Description | Required |
Name |
Descriptive name for this timer that is shown in the tree. |
No |
Target Throughput |
Throughput we want the timer to try to generate. |
Yes |
Calculate Throughput based on |
- this thread only - each thread will try to maintain the target throughput. The overall throughput will be proportional to the number of active threads.
- all active threads in current thread group - the target throughput is divided amongst all the active threads in the group. Each thread will delay as needed, based on when it last ran.
- all active threads - the target throughput is divided amongst all the active threads in all Thread Groups. Each thread will delay as needed, based on when it last ran. In this case, each other Thread Group will need a Constant Throughput timer with the same settings.
- all active threads in current thread group (shared) - as above, but each thread is delayed based on when any thread in the group last ran.
- all active threads (shared) - as above; each thread is delayed based on when any thread last ran.
|
|
|
|
GUI界面有两个选项:
1、期望吞吐量
2、以xx为基础控制吞吐量
下拉框选择项:
This thread only :分别控制每个线程的吞吐量,选择这种模式时,总的吞吐量为设置的 target Throughput 乘以线程的数量。
如果我们这里选择此种模式,然后设定了是10个线程,那么我们前面的Target throughput就应该填写1200/10=120 或 20/10*60=120了;
All active threads : 设置的target Throughput 将分配在每个活跃线程上,每个活跃线程在上一次运行结束后等待合理的时间后再次运行。活跃线程指同一时刻同时运行的线程。
如果我们这里选择此种模式,然后设定了是10个线程,那么我们前面的Target throughput就应该填写1200/10=120 或 20/10*60=120了;
All active threads in current thread group :设置的target Throughput将分配在当前线程组的每一个活跃线程上,当测试计划中只有一个线程组时,该选项和All active threads选项的效果完全相同。
All active threads (shared ):与All active threads 的选项基本相同,唯一的区别是,每个活跃线程都会在所有活跃线程上一次运行结束后等待合理的时间后再次运行。
All cative threads in current thread group (shared ):与All active threads in current thread group 基本相同,唯一的区别是,每个活跃线程都会在所有活跃线程的上一次运行结束后等待合理的时间后再次运行。
OK,再加入书中的例子:限制20QPS的情况下,查看其响应时间等信息。
加入网上些许分析:(http://ydhome.blog.51cto.com/8948432/1869970)
1、这里的20 QPS应该是指Jmeter发送请求的QPS,而不是服务器处理的QPS;--因为假如我们以20 QPS的速度向服务器发送请求,但是服务器每秒最多只能处理8个请求,那么我们无论如何都无法测得服务器在20 QPS的情况下的性能数据;
2、难点在于让Jmeter【稳定地】以20 QPS的速度向服务器发送请求;
使用组件Constant Throughput Timer。
因为单位为minute(分钟),因此,需要20*60=1200。
加入listener,进行查看响应时间等信息。