I have a JMeter script in which I want to count how many controllers there are in a thread. I want to count them before I run through them. I know if I run through them I can count them one by one with beanshell but I want to count them before I enter the thread or at the start of the thread above the controllers.
1 Answers
1
votes
You can add a Beanshell Sampler somewhere and use the following code in order to determine the Throughput Controllers count:
import org.apache.jmeter.control.ThroughputController;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.SearchByClass;
import java.lang.reflect.Field;
import java.util.Collection;
StandardJMeterEngine engine = ctx.getEngine();
Field test = engine.getClass().getDeclaredField("test");
test.setAccessible(true);
HashTree testPlanTree = (HashTree) test.get(engine);
SearchByClass throughputControllersSearch = new SearchByClass(ThroughputController.class);
testPlanTree.traverse(throughputControllersSearch);
Collection throughputControllers = throughputControllersSearch.getSearchResults();
log.info("Found " + throughputControllers.size() + " throughput controllers in the test plan");
Demo:
References:
ctx- stands for JMeterContextService- HashTree and SearchByClass are JMeter API classes
- others are core Java
See How to Use BeanShell: JMeter's Favorite Built-in Component for more information on using Beanshell in JMeter tests along with few examples
