从 Java 代码调用时 Jmeter 函数不执行 [英] Jmeter functions don't executing when calling from java code

查看:38
本文介绍了从 Java 代码调用时 Jmeter 函数不执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 jmx 文件中使用了一些 jmeter 函数.当我用jmeter工具从命令行运行它时,它运行正常,执行函数并返回一个值.

I'm using some jmeter functions in my jmx file. When I run it from command line with jmeter tool, its running normally, executing the function and return a value.

当我运行加载相同 jmx 文件的 java 代码时,它会将函数作为字符串并且不返回值.

When I run the java code , which load the same jmx file its taking a function as a string and don't return a value.

我测试了几个函数,结果都是一样的.在 jtl 文件中,我看到类似这样的内容 - ${__time(yyyy-MM-dd HH:mm:ss)}

I have tested with several functions, Results are the same. In jtl file I see something like this- ${__time(yyyy-MM-dd HH:mm:ss)}

<httpSample t="663" it="0" lt="631" ts="1492989803759" s="true" lb="User-getapikey   ${__time(yyyy-MM-dd HH:mm:ss)} " rc="200" rm="" tn="Thread Group 1-1" dt="text" de="UTF-8" by="528" ng="1" na="1">

这是我的java代码,当我没有函数时它正常工作:

Its my java code, its working normally when I don't have a functions:

StandardJMeterEngine jmeter = new StandardJMeterEngine();
JMeterUtils.loadJMeterProperties(jmeterPropertiesPath);
JMeterUtils.setJMeterHome(jmeterHomePath);
JMeterUtils.initLogging();
JMeterUtils.initLocale();

SaveService.loadProperties();
HashTree testPlanTree = null;
                try{
                    log.info("loading testPlanTree");
                    testPlanTree = SaveService.loadTree(jmxFile);
                    JMeter.convertSubTree( testPlanTree );
                }catch(IOException ex){
                    log.info("error loading file as tree");
                    log.info(ex.getMessage());
                    return;
                }
                jmeter.configure(testPlanTree);
                jmeter.run();

推荐答案

您的代码看起来不错,只需仔细检查您的项目类路径中是否有 ApacheJMeter_functions.jar.该文件位于 JMeter 安装的lib/ext"文件夹下.

Your code looks good, just double check you have ApacheJMeter_functions.jar in your project classpath. The file is located under "lib/ext" folder of your JMeter installation.

您可能还想添加 ResultCollector将您的测试执行结果存储到 .jtl 文件中,例如:

You might also want to add ResultCollector to store your test execution results into .jtl file like:

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

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

查看五种方法不使用 JMeter GUI 启动 JMeter 测试,了解有关执行 JMeter 测试的不同方法的更多信息,包括使用 Java API 启动和开发测试.

Check out Five Ways To Launch a JMeter Test without Using the JMeter GUI for more information on different approaches to JMeter test execution including using Java API to kick off and develop tests.

这篇关于从 Java 代码调用时 Jmeter 函数不执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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