具有多个采样器的Jmeter的单线程初始化 [英] Single thread initialization for Jmeter with multiple sampler
问题描述
我已经通过扩展Jmeter的AbstractJavaSamplerClient实现了自己的Sampler.在设置测试中,我想从目录中读取一堆文件并填充请求列表,然后在runTest阶段将其与所有线程/采样器实例共享.当前,我使用Lock来允许单个线程在开始时进行访问并填充它,然后其余线程就不用再重复执行任务了.代码如下:
I've implemented my own Sampler by extending AbstractJavaSamplerClient of Jmeter. In the setup test I want to read a bunch of files from a dir and populate the request List and then share it with all Threads/Sampler Instances during runTest phase. Currently I use a Lock in order to allow a single thread to access and populate it at the start, and then the rest don't bother to redo the task. The code looks like this:
@Override
public void setupTest(JavaSamplerContext context) {
...
MyLock.lock();
try {
// setup requests
if (MyFileList == null) {
// read the files and populate MyFileList
}
finally {
MyLock.unlock();
}
}
每次Sampler线程碰到此锁时,它将测试是否填充MyFileList,否则将进行填充,否则它将释放该锁.我为此使用了ReentrantLock.但是我想知道我是否可以完全避免锁定,并在开始时只有一个线程初始化阶段,然后再让其他线程继续进行.
Each time a Sampler thread hits this lock, it's gonna test if MyFileList is populated, if not it's gonna populate otherwise it's gonna release the lock. I used a ReentrantLock for this. But I was wondering if I could totally avoid the lock and have a single thread initialization phase at the beginning and then let other threads to proceed.
推荐答案
为什么不将其组织到线程组中. 您可以添加带有线程的启动线程组"来进行初始化. 然后,您可以添加标准的线程组"来完成其余工作
Why not organize this into threadgroups. You can add a "startup threadgroup" with a thread to do your init. Then you can add a standard "threadgroup" to do the rest of your stuff
在下面的图像中,您可以看到如何添加"setUp Thread Group",它将首先运行,它将完成您想要的设置 在这里,您将添加采样器以仅进行设置 然后再次单击测试计划",然后转到添加->线程(用户)->线程组 并且您将在此处添加一个常规的旧线程组,以完成所有其余工作
In the image below you can see how to add a "setUp Thread Group" this will run first, it will do the setup you want done Here you will add samplers to do just your setup Then click on your "Test Plan" again, and go to add->threads(users)->Thread group and you'll be adding a regular old threadgroup here, to do all the rest of your stuff
这篇关于具有多个采样器的Jmeter的单线程初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!