尝试使用JMeter API生成JMeter测试计划(jmx):从代码创建的jmeter jmx文件与JMeter创建的jmeter jmx文件不匹配 [英] Trying to generate JMeter Test Plan (jmx) With JMeter API : Mismatch between jmeter jmx file created from code and the one created by JMeter

查看:143
本文介绍了尝试使用JMeter API生成JMeter测试计划(jmx):从代码创建的jmeter jmx文件与JMeter创建的jmeter jmx文件不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jmeter java api创建一个jmeter jmx文件.这就是我所做的,

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

  1. gui.jmx

使用jmeter gui应用程序创建一个参考jmx文件,我可以将其与之进行比较.对于测试计划,我仅在线程组中添加一个线程组和一个Java采样器.所有值都是默认值.

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

使用jmeter java api,我创建一个包含测试计划,线程组和Java采样器的jmx文件.所有值均根据情况1进行设置.

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.

从代码创建jmx文件后,我注意到以下区别,

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

1)将gui.jmx中的节点替换为code.jmx中的以下内容

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

    <org.apache.jorphan.collections.HashTree>

尽管这不是问题,但是可以在GUI保存时以某种方式生成以下标记

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

    <hashTree>

2)测试元素节点在gui.jmx中包含属性'guiClass'和'testClass',例如 这些属性不是在code.jmx中生成的,我也没有找到任何显式设置它们的API ->因此,生成的code.jmx无法在jmeter gui控制台中打开.这可能意味着生成的jmx只能在没有控制台模式的情况下使用.这是设计使然吗?是否可以通过某种方式使用jmeter api通过代码添加这些属性? (不将DOM用作黑客手段)

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)gui.jmx的xml结构如下,

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

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

请注意HashTree元素的嵌套.当在JMeter GUI中打开它时,这些元素就会相互嵌套.

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

code.jmx的xml结构如下,

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>

请注意标签放置的差异.没有嵌套.它们都处于同一级别.为什么会这样.使用jmx api添加测试元素以使散列树元素像第一种情况那样彼此嵌套的正确方法是什么?

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?

推荐答案

最后,在查看了jmeter源代码之后,我发现除了我正在做的事情之外,我还需要显式设置guiClass和testClass参数

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());

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

类似于ThreadGroup,JavaSampler等其他测试元素.

similarly for other test elements like ThreadGroup, JavaSampler etc.

完整代码如下,

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"));
    }
}

这篇关于尝试使用JMeter API生成JMeter测试计划(jmx):从代码创建的jmeter jmx文件与JMeter创建的jmeter jmx文件不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆