如何从Java代码执行JMeter测试用例 [英] How to execute JMeter test case from Java code

查看:244
本文介绍了如何从Java代码执行JMeter测试用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从Java代码运行JMeter测试用例?

How do I run a JMeter test case from Java code?

我已按照示例此处来自Blazemeter.com

I have followed the example Here from Blazemeter.com

我的代码如下:

public class BasicSampler {

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

    // Initialize Properties, logging, locale, etc.
    JMeterUtils.loadJMeterProperties("/home/stone/Workbench/automated-testing/apache-jmeter-2.11/bin/jmeter.properties");
    JMeterUtils.setJMeterHome("/home/stone/Workbench/automated-testing/apache-jmeter-2.11");
    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("/home/stone/Workbench/automated-testing/apache-jmeter-2.11/bin/examples/CSVSample.jmx");
    HashTree testPlanTree = SaveService.loadTree(in);
    in.close();

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

}

但是我一直在控制台中收到以下消息,并且我的测试从未执行.

but I keep getting the following messages in the console and my test never executes.

INFO 2014-09-23 12:04:40.492 [jmeter.e]():启用运行版本后将启动侦听器 INFO 2014-09-23 12:04:40.511 [jmeter.e]():要恢复到以前的行为,请定义jmeterengine.startlistenerslater = false

INFO 2014-09-23 12:04:40.492 [jmeter.e] (): Listeners will be started after enabling running version INFO 2014-09-23 12:04:40.511 [jmeter.e] (): To revert to the earlier behaviour, define jmeterengine.startlistenerslater=false

我还尝试了jmeter.properties文件中未注释的jmeterengine.startlistenerslater = false

I have also tried uncommented jmeterengine.startlistenerslater=false from jmeter.properties file

推荐答案

  1. 您怎么知道您的测试永远不会执行"?
  2. jmeter.log文件中的内容(应位于项目的根目录中).或者,也可以在JMeterUtils.initLogging()行中添加注释,以在STDOUT中查看完整的输出
  3. 您是否已在获取用户详细信息" CSV数据集配置中更改了相对路径CSVSample_user.csv,因为它可能会解析为
  1. How do you know that your "test never executes"?
  2. What is in jmeter.log file (it should be in the root of your project). Or alternatively comment JMeterUtils.initLogging() line to see the full output in STDOUT
  3. Have you changed relative path CSVSample_user.csv in "Get user details" CSV Data Set Config as it may resolve into a different location as it recommended in Using CSV DATA SET CONFIG
  4. Is CSVSample.jtl file generated anywhere (again it should be in the root of your project by default)? What is in it?

代码看起来不错,我很确定问题出在CSVSample_user.csv文件的路径上,并且您的日志中有类似java.io.FileNotFoundException的内容.请再次检查CSVSample.jmx文件是否包含CSVSample_user.csv的有效完整路径.

The code looks good and I'm pretty sure that the problem is with the path to CSVSample_user.csv file and you have something like java.io.FileNotFoundException in your log. Please double check that CSVSample.jmx file contains valid full path to CSVSample_user.csv.

更新评论中的问题

    默认情况下,
  1. jmeter.log文件应位于Eclipse工作区文件夹中
  2. 查看CSVSample.jmx,有一个View Resulst in Table侦听器,该侦听器配置为将结果存储在~/CSVSample.jtl
  3. 如果要查看摘要程序消息和经典" .jtl报告,请在jmeter.configure(testPlanTree);节之前添加下几行

  1. jmeter.log file should be under your Eclipse workspace folder by default
  2. Looking into CSVSample.jmx there is a View Resulst in Table listener which is configured to store results under ~/CSVSample.jtl
  3. If you want to see summarizer messages and "classic" .jtl reporting add next few lines before jmeter.configure(testPlanTree); stanza

Summariser summer = null;
String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
if (summariserName.length() > 0) {
    summer = new Summariser(summariserName);
}

String logFile = "/path/to/jtl/results/file.jtl";
ResultCollector logger = new ResultCollector(summer);
logger.setFilename(logFile);
testPlanTree.add(testPlanTree.getArray()[0], logger);

这篇关于如何从Java代码执行JMeter测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆