繁体   English   中英

基于jmeter api从java构造一个复杂的jmx文件

[英]Construct a complex jmx file from java based on jmeter api

我正在从Java构建jmeter GUI的.jmx文件。 .jmx仅由http代理采样器和线程组组成。 线程组和代理采样器都是基于列表构建的,因此,它们按特定顺序排列。 但是,我的问题是,与列表进行比较时,构造的.jmx的顺序完全错误。

我已阅读并尝试“不使用JMeter GUI即可启动JMeter测试的五种方法”。 我所做的是尝试将“用Java创建一个新的JMeter测试”概括为多个线程组和http代理采样器。 这是我参与的最小代码:

public static void createJmx(ArrayList<ArrayList<String>> calls, ArrayList<HashMap<String, String>> httpSamplerList) throws Exception {
        // get log file length
        int numberOfCalls = calls.size();
        int numberOfhttpSampler = httpSamplerList.size();

        // Engine
        StandardJMeterEngine jm = new StandardJMeterEngine();
        String jmeterHome = "D:\\Jenkins-Global-Workspace\\apache-jmeter-5.1.1";
        // jmeter.properties
        JMeterUtils.setJMeterHome(jmeterHome);
        JMeterUtils.loadJMeterProperties("D:\\Jenkins-Global-Workspace\\apache-jmeter-5.1.1\\bin\\jmeter.properties");

        JMeterUtils.initLogging();
        JMeterUtils.initLocale();

        // Create HashTree structure

        HashTree testPlanTree = new HashTree();
        TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
        testPlanTree.add(testPlan);
        HTTPSamplerProxy examplecomSampler = null;
        LoopController loopController;
        ThreadGroup threadGroup = null;
        HashTree threadGroupHashTree = null;
        int j=0;
        for (int i = 0; i < numberOfCalls; i++) {
            if (calls.get(i).size() == 3) {
                System.out.println(calls.get(i));
                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 = new ThreadGroup();
                threadGroup.setName(calls.get(i).get(1));
                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());
                testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
                testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
                testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());

                threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
            }
            else {
                System.out.println(calls.get(i));
                examplecomSampler = new HTTPSamplerProxy();
                examplecomSampler.setDomain("example.com");
                examplecomSampler.setPort(80);
                examplecomSampler.setPath("/");
                examplecomSampler.setMethod("GET");
                examplecomSampler.setName(Integer.toString(j)); // currently use a integer number naming as test
                examplecomSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
                examplecomSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
                threadGroupHashTree.add(examplecomSampler);
                j++;
            }
        }
//    // save generated test plan to JMeter's .jmx file format
    SaveService.saveTree(testPlanTree, new FileOutputStream(".\\test.jmx"));
//        
    }

我使用变量j来跟踪http采样器的顺序,但是一些http采样器以错误的顺序完成。

有人知道为什么以及如何解决它?

干杯

对于那些偶然发现这篇文章的人。 如果哈希树中有多个模块,并且您希望它们与将它们添加到树中的顺序相同。 请用

org.apache.jorphan.collections.ListedHashTree;

代替

org.apache.jorphan.collections.HashTree;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM