Search code examples
javajmeter

JMeter Plugin development: add standard ThreadGroup to Testplan programmatically


I am trying to implement a new JMeter plugin myself. What I reached so far is adding a top-level menu entry and when clicking on it to start my plugin workflow.

I want to achieve to add a deafult ThreadGroup to the root Testplan. I try it like this:

public void addNewThreadGroupWithSamplers() {
    GuiPackage guiPackage = GuiPackage.getInstance();
    JMeterTreeNode testPlanNode = (JMeterTreeNode) guiPackage.getTreeModel().getRoot();

    // Create a new Thread Group
    ThreadGroup threadGroup = new ThreadGroup();
    threadGroup.setName("New Thread Group");

    // Add the new Thread Group to the Test Plan and get its node
    JMeterTreeNode threadGroupNode;
    try {
        threadGroupNode = guiPackage.getTreeModel().addComponent(threadGroup, testPlanNode);
    } catch (IllegalUserActionException e) {
        e.printStackTrace();
    }

    // Refresh the JMeter GUI
    guiPackage.getMainFrame().repaint();
}

Unfortunately I get a null pointer exception from JMeterTreeModel, that states

Uncaught Exception java.lang.NullPointerException: Cannot invoke "org.apache.jmeter.gui.JMeterGUIComponent.clearGui()" because "guicomp" is null in thread Thread[AWT-EventQueue-0,6,main]. See log file for details.

and the exception occurs at this line of JMeter:

JMeterGUIComponent guicomp = guiPackage.getGui(component);

of addComponent()

Something with the UI seems to be uninitialized or what could be the reason. Can you please pin-point me to a lcation with an example or provide a solution?


Solution

  • You're missing some obvious mandatory elements like Loop Controller which is an integral part of the Thread Group , and properties provided via TestElement.

    If this is really what you're looking for you can use the following code:

    import org.apache.jmeter.threads.ThreadGroup;
    import org.apache.jmeter.threads.gui.ThreadGroupGui;
    import org.apache.jmeter.gui.GuiPackage
    import org.apache.jmeter.gui.tree.JMeterTreeNode
    import org.apache.jmeter.exceptions.IllegalUserActionException
    import org.apache.jmeter.control.LoopController;
    import org.apache.jmeter.control.gui.LoopControlPanel;
    import org.apache.jmeter.testelement.TestElement;
    
    GuiPackage guiPackage = GuiPackage.getInstance();
    JMeterTreeNode testPlanNode = (JMeterTreeNode) guiPackage.getTreeModel().getRoot();
    
    
    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());
    
    
    
    JMeterTreeNode threadGroupNode;
    try {
       threadGroupNode = guiPackage.getTreeModel().addComponent(threadGroup, testPlanNode);
    } catch (IllegalUserActionException e) {
       e.printStackTrace();
    }
    
    // Refresh the JMeter GUI
    guiPackage.getMainFrame().repaint();
    

    Demo:

    enter image description here

    More information:

    However it's better to take a look at Building a Test Plan Programmatically user manual chapter