如何用Java代码运行JMeter JMX文件?

时间:2019-05-20 09:59:19

标签: java spring-boot jmeter jmeter-plugins

我正在尝试通过我的Spring Boot应用程序从Java代码运行JMeter MS SQL数据库测试计划,但显示以下错误:

screenshot of error message screenshot of error message

我已将插件管理器加载到JMeter中,并将其放在jmeter / lib / ext文件夹中,并安装了所有必需的插件。

运行JMeter测试用例的Java代码:

package com.example.demofin;

import java.io.File;

import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.reporters.Summariser;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.example.demofin.config.Properties;

@Component
public class Demofin implements CommandLineRunner {

    @Autowired
    private Properties properties;

    private Summariser result;

    @Override
    public void run(String... args) throws Exception {
        for(String i : args)
            System.out.println(i);
          // String j = properties.getPropertyByKey("JMETER_HOME");
           String j="D:\\apache-jmeter-4.0";
//         System.out.println(j);
           //System.out.println("Jmeter home path: " + properties.getPropertyByKey("JMETER_HOME"));

               StandardJMeterEngine jmeter = new StandardJMeterEngine();       
//           // Initialize Properties, logging, locale, etc.        
             JMeterUtils.setJMeterHome(j);
             JMeterUtils.loadJMeterProperties(j+"/bin/jmeter.properties");
//          
//           // you can comment this line out to see extra log messages of i.e. DEBUG level  
             JMeterUtils.initLogging();  
             JMeterUtils.initLocale();  

             // Initialize JMeter SaveService  
             SaveService.loadProperties();  

             // Load existing .jmx Test Plan  
             HashTree testPlanTree = SaveService.loadTree(new File("D:\\apache-jmeter-4.0\\" + "bin\\JDBC Connection Configuration.jmx"));  

             Summariser summer = null;  
             String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");  

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

         ResultCollector logger = new ResultCollector(summer);  
         testPlanTree.add(testPlanTree.getArray()[0], logger);  
             // Run JMeter Test  
             jmeter.configure(testPlanTree);  
         jmeter.run();  
         result = summer;
    }
}

1 个答案:

答案 0 :(得分:0)

通常,我们将Java代码编译为jar并将其推送到jmeter/lib/ext并在Jmeter中使用。

如果您尝试在Java中使用Jmeter,建议您下载JMeter的源代码并合并到您的项目中。