JMeter multiple thread group and queue example
We will create a JMeter script with multiple thread group.
You can pass variables from one Thread Group to another with your custom blocking queue.
First A thread group will read data from CSV file and put data into A queue.
B thread group will read the data that thread group A has put into queue
C thread group will read the data that thread group B has put into queue.
D thread group will read the data that thread group C has put into queue.
1) Open JMeter.Right click testplan
2)Adds->Threads->setUP Thread Group.
3)Right click thread group.
4)Add->Sampler->BeanShell Sampler.
5)Now we have 4 thread group and beanshell sampler.(We will write java code in beanshell sampler after create custom queue jar)
CREATING CUSTOM JAR
1)Create a class in your IDE.I create a jar from intellij IDE.
public class ABlockingQueue { private static LinkedBlockingQueue<Object> q = new LinkedBlockingQueue(); public static void put(Object o) throws Exception { q.put(o); } public static Object get(long timeout) throws Exception { return q.poll(timeout, TimeUnit.MILLISECONDS); } public static Object take() throws Exception { return q.take(); } public static void clear() throws Exception { q.clear(); } public static int size() throws Exception { return q.size(); } }
2)To create a jar from intellij IDE.Build->Build Artifacts in intellij IDE and create jar.
3)Paste the JAR(test.jar) into Jmeter Path : <JMeter Path>/lib folder.
4)Restart jmeter.
CSV FILE CONFIG
1)Right click A setUp Thread Group.Add->Config Element->CSV Data Set Config
2)Create a CSV file.(Path of file CSV must be the same as path of file JMX.)
3)Add data to CSV file.A thread Group will read data from CSV file.
WRITING JAVA CODE IN BEANSHELL SAMPLER
We have 4 beanshell sampler.
1)A beanshell sampler code
import tarik; String data=vars.get("data"); //read data from CSV. tarik.ABlockingQueue.put(data);
2)B beanshell sampler code
import tarik; String dataFromAQueue=tarik.ABlockingQueue.take();// Retrieves and removes the head of this queue, waiting if necessary until an element becomes available. print("DATA FROM A QUEUE " + dataFromAQueue ); tarik.BBlockingQueue.put(dataFromAQueue);
3)C beanshell sampler code
import tarik;
String dataFromBQueue=tarik.BBlockingQueue.take();
print("DATA FROM B QUEUE " + dataFromBQueue );
tarik.CBlockingQueue.put(dataFromBQueue);
4)D beanshell sampler code
import tarik;
String dataFromCQueue=tarik.CBlockingQueue.take();
print("DATA FROM C QUEUE " + dataFromCQueue );
tarik.DBlockingQueue.put(dataFromCQueue);
Script is ready to run.Finally I add a Thread group to clean all queue.
Now.We can run this script.Disable clear All Queue thread group and start.
You can give different number of thread for each Thread group.
JMeter will run all thread group concurrently.
Result...
Comments
Post a Comment