Reputation: 1217
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();
}
}
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.
Upvotes: 0
Views: 1135
Reputation: 421
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
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
Upvotes: 1