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

梦想的初衷 提交于 2020-01-24 19:19:48

问题


I have run the Jmeter Script using Jmeter dependency in eclipse using Java code, fortunately, my script is running fine but now I'm unable to store the result for the same. Can anyone please tell me how can I achieve this ? please see the following code that I have tried.

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


}

回答1:


You need to add a ResultCollector instance to your Test Plan in order to get the .jtl file written like:

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

the code needs to be added before the jmeter.configure(testPlanTree); line

More information:

  • Five Ways To Launch a JMeter Test without Using the JMeter GUI
  • jmeter-from-code



回答2:


I have solved it by myself..

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


来源:https://stackoverflow.com/questions/59834827/how-can-i-save-a-result-set-after-running-the-jmeter-test-using-a-program-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!