如何使用程序 (JAVA CODE) 在 运行 Jmeter 测试后保存结果集?

How can I save a result set after running the Jmeter Test using a program (JAVA CODE)?

我有 运行 Jmeter 脚本,在 eclipse 中使用 Jmeter 依赖,使用 Java 代码,幸运的是,我的脚本 运行ning 很好,但现在我无法存储结果对于相同的。谁能告诉我如何实现这一目标? 请查看我尝试过的以下代码。

package com.solitera.automation.controller;

import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;

import java.io.File;
import java.io.FileInputStream;

public class JMeterFromExistingJMX {

    public static void main(String[] argv) throws Exception {
        // JMeter Engine
        StandardJMeterEngine jmeter = new StandardJMeterEngine();


        // Initialize Properties, logging, locale, etc.
        JMeterUtils.loadJMeterProperties("D:/apache-jmeter-5.1.1/bin/jmeter.properties");
        JMeterUtils.setJMeterHome("D:/apache-jmeter-5.1.1");
        JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level
        JMeterUtils.initLocale();

        // Initialize JMeter SaveService
        SaveService.loadProperties();

        // Load existing .jmx Test Plan
       /* FileInputStream in = new FileInputStream("D:/Ecllipse_project_workspace2/slt_automation/src/test/jmeter/slt_autoMa_Test.jmx");
        HashTree testPlanTree = SaveService.loadTree(in);
        in.close();*/

        HashTree testPlanTree = SaveService.loadTree(new File("D:/apache-jmeter-5.1.1/extras/slt_auto_test_java_blaze_script.jmx"));

        // Run JMeter Test
        jmeter.configure(testPlanTree);
        jmeter.run();
    }


}

我自己解决了..

if (summariserName.length() > 0) {  
    summer = new Summariser(summariserName);  
  }  

  String logFile = "D:/apache-jmeter-5.1.1/extras/resultss.xml";
  ResultCollector logger = new ResultCollector(summer);  
  logger.setFilename(logFile);
  testPlanTree.add(testPlanTree.getArray()[0], logger); 

您需要将一个 ResultCollector 实例添加到您的测试计划中,以便将 .jtl 文件编写为:

ResultCollector collector = new ResultCollector();
collector.setFilename("result.jtl");
testPlanTree.add(testPlanTree.getArray()[0], collector);

需要在jmeter.configure(testPlanTree);行前添加代码

更多信息: