Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to generate JMeter Test Plan (jmx) With JMeter API : Mismatch between jmeter jmx file created from code and the one created by JMeter

Tags:

jmx

jmeter

I am trying to create a jmeter jmx file using the jmeter java api. This is what I have done,

  1. gui.jmx

Use the jmeter gui application to create a reference jmx file against which I can compare. To the test plan, I only add a thread group and a java sampler within the thread group. All values are default.

  1. code.jmx

Using the jmeter java api, I create a jmx file containing a test plan, thread group and java sampler. All values are set as per the case 1.

After creating the jmx file from code, I note the following differences,

1) The nodes in gui.jmx is replaced by the following in code.jmx

    <org.apache.jorphan.collections.HashTree>

Though this is not an issue, is it possible to somehow generate the following tag as the GUI saves it

    <hashTree>

2) Test element nodes contain the attributes 'guiClass' and 'testClass' in gui.jmx e.g. These attributes are not generated in code.jmx and neither did I find any API to explicitly set them -> Due to this the generated code.jmx does not open in the jmeter gui console. Which probably means that the generated jmx can be used in no console mode only. Is this by design? Is there some way by which these attributes can be added via code using the jmeter apis? (not using DOM as a hack)

3) The xml structure of gui.jmx is as follows,

    <hashTree>
    <TestPlan ...>
    ...
    </TestPlan>
    <hashTree>
    <ThreadGroup ...>
    ...
    </ThreadGroup>
    **<hashTree/>**
    </hashTree>
</hashTree>

Note the nesting of the HashTree elements. When this opens up in the JMeter GUI, the elements are nested within each other.

The xml structure of code.jmx is as follows,

<org.apache.jorphan.collections.HashTree>
    <TestPlan ...>
    ...
    </TestPlan>
    **<org.apache.jorphan.collections.HashTree/>**
    <ThreadGroup ...>
    ...
    </ThreadGroup>
    **<org.apache.jorphan.collections.HashTree/>**
</org.apache.jorphan.collections.HashTree>

Note the difference in placement of tags. There is no nesting. They are all at the same level. Why does this happen. What is the proper way to add test elements using jmx api so that the hash tree elements are nested within each other as in the first case?

like image 413
Avi Avatar asked Aug 26 '14 17:08

Avi


People also ask

How import JMX file into JMeter?

jmx file in Windows 10 operating system. Right click on valid JMeter test plan, go to Open with > Choose another app as shown below. Upon clicking on Open, JMeter will launch and open the file where you right clicked on it.

Which parameter specifies name of JMX file that contains the test plan non GUI mode?

-n – non-GUI mode – this specifies JMeter is to run in non-GUI mode. -t – JMX file – location of the test plan and the name of JMX file that contains the Test Plan.

How do I save a JMX file in JMeter?

Within the Apache JMeter application, save your JMeter test plan (File > Save) to a JMX file on a local computer.


2 Answers

Finally after looking into the jmeter source code, I figured that in addition to what I was doing, I needed to explicitly set the guiClass and testClass parameters

testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName()); testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());

similarly for other test elements like ThreadGroup, JavaSampler etc.

The full code is as follows,

package com.test;

import java.io.FileOutputStream;

import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.control.gui.LoopControlPanel;
import org.apache.jmeter.control.gui.TestPlanGui;
import org.apache.jmeter.protocol.java.control.gui.JavaTestSamplerGui;
import org.apache.jmeter.protocol.java.sampler.JavaSampler;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jmeter.threads.gui.ThreadGroupGui;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;

public class JMXCreator {
    public static void main(String[] argv) throws Exception {
        // Initialize the configuration variables
        String jmeterHome = "D:\\apache-jmeter-2.11";
        JMeterUtils.setJMeterHome(jmeterHome);
        JMeterUtils.loadJMeterProperties(JMeterUtils.getJMeterBinDir()
                + "\\jmeter.properties");
        JMeterUtils.initLogging();
        JMeterUtils.initLocale();

        // TestPlan
        TestPlan testPlan = new TestPlan();
        testPlan.setName("Test Plan");
        testPlan.setEnabled(true);
        testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
        testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());

        // ThreadGroup controller
        LoopController loopController = new LoopController();
        loopController.setEnabled(true);
        loopController.setLoops(5);
        loopController.setProperty(TestElement.TEST_CLASS,
                LoopController.class.getName());
        loopController.setProperty(TestElement.GUI_CLASS,
                LoopControlPanel.class.getName());

        // ThreadGroup
        ThreadGroup threadGroup = new ThreadGroup();
        threadGroup.setName("Thread Group");
        threadGroup.setEnabled(true);
        threadGroup.setSamplerController(loopController);
        threadGroup.setNumThreads(5);
        threadGroup.setRampUp(10);
        threadGroup.setProperty(TestElement.TEST_CLASS,
                ThreadGroup.class.getName());
        threadGroup.setProperty(TestElement.GUI_CLASS,
                ThreadGroupGui.class.getName());

        // JavaSampler
        JavaSampler javaSampler = new JavaSampler();
        javaSampler.setClassname("my.example.sampler");
        javaSampler.setEnabled(true);
        javaSampler.setProperty(TestElement.TEST_CLASS,
                JavaSampler.class.getName());
        javaSampler.setProperty(TestElement.GUI_CLASS,
                JavaTestSamplerGui.class.getName());

        // Create TestPlan hash tree
        HashTree testPlanHashTree = new HashTree();
        testPlanHashTree.add(testPlan);

        // Add ThreadGroup to TestPlan hash tree
        HashTree threadGroupHashTree = new HashTree();
        threadGroupHashTree = testPlanHashTree.add(testPlan, threadGroup);

        // Add Java Sampler to ThreadGroup hash tree
        HashTree javaSamplerHashTree = new HashTree();
        javaSamplerHashTree = threadGroupHashTree.add(javaSampler);

        // Save to jmx file
        SaveService.saveTree(testPlanHashTree, new FileOutputStream(
                "d:\\test.jmx"));
    }
}
like image 127
Avi Avatar answered Oct 10 '22 00:10

Avi


Just a comment regarding

<org.apache.jorphan.collections.HashTree>

if you set in saveservice.properties file:

hashTree=org.apache.jorphan.collections.HashTree

instead of:

hashTree=org.apache.jorphan.collections.ListedHashTree

you will get

  <hashTree>
like image 43
Piotr Boho Avatar answered Oct 10 '22 00:10

Piotr Boho