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

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

问题描述

我正在尝试使用 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,我创建了一个 jmx 文件,其中包含测试计划、线程组和 java 采样器.所有值均按情况 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 作为 hack)

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 创建的文件不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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