I am trying to create a jmeter jmx file using the jmeter java api. This is what I have done,
- 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.
- 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?
Just a comment regarding
if you set in saveservice.properties file:
hashTree=org.apache.jorphan.collections.HashTree
instead of:
hashTree=org.apache.jorphan.collections.ListedHashTree
you will get
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,
Creating JMeter test from Java Api is not really supported method and you expose your test plan to changes in JMeter.
I would not do it.
I seems you are doing this to dynamically create test scripts based on some chosen variables, this is not the right way to do it.
I JMeter to do what you want you use:
User Defined Variables
Property function which enables you to pass properties on command line
Also have a look at Jenkins and Maven for examples :
http://anttikoivisto.blogspot.fi/2014/04/real-world-jmeter-03-running-with-jenkins.html
http://www.stevefenton.co.uk/Content/Blog/Date/201408/Blog/Switching-JMeter-Between-Integration-And-Load-Testing/