dasun_001
dasun_001

Reputation: 169

Throughput Controller automation in JMeter test?

I'm working on a project in which I have to automate the JMeter test as a backend service. My test plan is like this

Thread Group
 -Throughput Controller1
       --sampler1

 -Throughput controller2
       --sampler2

One of my sample throughput implementations is like this. Here what I'm doing is setting the number of threads to count 10 initially then I want to control it to the thread count of 3 and other 7 for another sampler.

TestPlan testPlan = new TestPlan();
testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());

SetupThreadGroup threadGroup = new SetupThreadGroup();
threadGroup.setRampUp(1);
threadGroup.setNumThreads(10);

ThroughputController throughputController = new ThroughputController();
throughputController.setComment("Through Put");
throughputController.setName("Through put");
throughputController.setMaxThroughput(3);
throughputController.setStyle(1);
   :
   :
   :
HTTPSamplerProxy examplecomSampler = loadTestSampler(urlDetails);

ListedHashTree testPlanTree = new ListedHashTree();
HashTree tpConfig = testPlanTree.add(testPlan);
HashTree tgConfig = tpConfig.add(threadGroup);
tgConfig.add(throughputController);
tgConfig.add(examplecomSampler)

When I'm running this all the 10 threads calling to examplecomSampler. but I want to limit that to 3 and other 7 to another sampler Why is that?

Thanks.

Upvotes: 0

Views: 243

Answers (1)

Dmitri T
Dmitri T

Reputation: 168197

I don't think your way of initializing the Throughput Controller is correct, moreover it's hard to say what else is wrong without seeing the full code of your class.

Be aware that you should be creating JMeter test plans using JMeter GUI, other ways are not officially supported, however you can try using wrapper tools like Taurus

In general JMeter jmx scripts are basically XML files so you can compare the test plan you generate using JMeter API with the test plan which comes from JMeter GUI, identify the differences and amend your code to eliminate them.


If you still want to go into that direction of programmatic creation of a JMeter script, an example test plan with 2 throughput controllers and 2 samplers configured for 30% and 70% of executions correspondingly would look something like:

HashTree testPlanTree = new HashTree();

HTTPSamplerProxy examplecomSampler = new HTTPSamplerProxy();
examplecomSampler.setDomain("example.com");
examplecomSampler.setPort(80);
examplecomSampler.setPath("/");
examplecomSampler.setMethod("GET");
examplecomSampler.setName("Open example.com");
examplecomSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
examplecomSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());

HTTPSamplerProxy blazemetercomSampler = new HTTPSamplerProxy();
blazemetercomSampler.setDomain("blazemeter.com");
blazemetercomSampler.setPort(80);
blazemetercomSampler.setPath("/");
blazemetercomSampler.setMethod("GET");
blazemetercomSampler.setName("Open blazemeter.com");
blazemetercomSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
blazemetercomSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());

LoopController loopController = new LoopController();
loopController.setLoops(1);
loopController.setFirst(true);
loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
loopController.initialize();

ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setName("Example Thread Group");
threadGroup.setNumThreads(1);
threadGroup.setRampUp(1);
threadGroup.setSamplerController(loopController);
threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());

ThroughputController throughputController3 = new ThroughputController();
throughputController3.setComment("Through Put 3");
throughputController3.setName("Through put 3");
FloatProperty throughput3 = new FloatProperty();
throughput3.setName("ThroughputController.percentThroughput");
throughput3.setValue(30.0f);
throughputController3.setProperty(throughput3);
throughputController3.setProperty("ThroughputController.style", 1);
throughputController3.setProperty("ThroughputController.perThread", false);
throughputController3.setProperty(TestElement.TEST_CLASS, ThroughputController.class.getName());
throughputController3.setProperty(TestElement.GUI_CLASS, ThroughputControllerGui.class.getName());

ThroughputController throughputController7 = new ThroughputController();
throughputController7.setComment("Through Put 7");
throughputController7.setName("Through put 7");
FloatProperty throughput7 = new FloatProperty();
throughput7.setName("ThroughputController.percentThroughput");
throughput7.setValue(70.0f);
throughputController7.setProperty(throughput7);
throughputController7.setProperty("ThroughputController.style", 1);
throughputController7.setProperty("ThroughputController.perThread", false);
throughputController7.setProperty(TestElement.TEST_CLASS, ThroughputController.class.getName());
throughputController7.setProperty(TestElement.GUI_CLASS, ThroughputControllerGui.class.getName());

HashTree logicHashTree = new HashTree();

HashTree throughputController3Tree = new HashTree();
HashTree exampleComHashTree = new HashTree();

exampleComHashTree.add(examplecomSampler);
throughputController3Tree.add(throughputController3);
throughputController3Tree.add(throughputController3, exampleComHashTree);

HashTree throughputController7Tree = new HashTree();
HashTree blazemeterComHashTree = new HashTree();

blazemeterComHashTree.add(blazemetercomSampler);
throughputController7Tree.add(throughputController7);
throughputController7Tree.add(throughputController7, blazemeterComHashTree);


logicHashTree.add(throughputController3Tree);
logicHashTree.add(throughputController7Tree);

TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());

testPlanTree.add(testPlan);
HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
threadGroupHashTree.add(logicHashTree);

Check out Five Ways To Launch a JMeter Test without Using the JMeter GUI article for more information on various ways of launching a JMeter test including creating one from scratch using Java language.

Upvotes: 1

Related Questions