簡體   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